From 5d5892c66fe446ee4469045d697e50909712aa2d Mon Sep 17 00:00:00 2001 From: yukirij Date: Sun, 18 Aug 2024 18:26:50 -0700 Subject: [PATCH] Potential fix for promotion detection. --- game/src/game/mod.rs | 12 +++++++++--- game/src/util/hex.rs | 20 +++++++++++++++++++- www/js/interface.js | 2 ++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/game/src/game/mod.rs b/game/src/game/mod.rs index 66e1f13..867ea56 100644 --- a/game/src/game/mod.rs +++ b/game/src/game/mod.rs @@ -70,6 +70,12 @@ impl Game { if piece.player == target.player { swap = true; target.tile = play.from; + + // Check for target promotion. + let hex = Hex::from_tile(play.to); + if !target.promoted && !Hex::is_back(hex.x(), hex.y(), target.player) { + target.promoted = true; + } } // Add captured piece to pool. @@ -82,6 +88,7 @@ impl Game { if !swap { self.board.pieces[tid as usize] = None; } } + // Set tile/piece associations. if swap { self.board.tiles[play.from as usize].piece = self.board.tiles[play.to as usize].piece; } else { @@ -92,10 +99,9 @@ impl Game { self.board.pieces[pid as usize] = Some(piece); - // Check if piece is promoted. + // Check for piece promotion. let hex = Hex::from_tile(play.from); - let offset = -(piece.player as i8 * 2) + 1; - if !piece.promoted && !Hex::is_valid(hex.x() as i8, hex.y() as i8 + offset) { + if !piece.promoted && !Hex::is_back(hex.x(), hex.y(), piece.player) { if let Some(piece) = &mut self.board.pieces[pid as usize] { piece.promoted = true; } diff --git a/game/src/util/hex.rs b/game/src/util/hex.rs index 0710ad5..480f832 100644 --- a/game/src/util/hex.rs +++ b/game/src/util/hex.rs @@ -41,7 +41,7 @@ impl Hex { } } - pub fn is_valid(x:i8, y:i8) -> bool + /*pub fn is_valid(x:i8, y:i8) -> bool { const COLUMNS :[(i8, i8); 9] = [ (0, 4), @@ -60,6 +60,24 @@ impl Hex { let (min, max) = COLUMNS[x as usize]; y >= min && y <= max } else { false } + }*/ + + pub fn is_back(x:u8, y:u8, player:u8) -> bool + { + const COLUMNS :[[u8; 2]; 9] = [ + [0, 4], + [0, 5], + [0, 6], + [0, 7], + [0, 8], + [1, 8], + [2, 8], + [3, 8], + [4, 8], + ]; + if x > 0 && x < 9 { + y == COLUMNS[x as usize][1 ^ player as usize] + } else { false } } pub fn x(&self) -> u8 { self.x } diff --git a/www/js/interface.js b/www/js/interface.js index abaa4e0..a26789e 100644 --- a/www/js/interface.js +++ b/www/js/interface.js @@ -771,6 +771,8 @@ const INTERFACE = { }, message(code, data) { + if(data === null) { return; } + switch(code) { case OpCode.GameState: { INTERFACE_DATA.player = data.player;