Potential fix for promotion detection.

This commit is contained in:
yukirij 2024-08-18 18:26:50 -07:00
parent ffa5d45d31
commit 5d5892c66f
3 changed files with 30 additions and 4 deletions

View File

@ -70,6 +70,12 @@ impl Game {
if piece.player == target.player { if piece.player == target.player {
swap = true; swap = true;
target.tile = play.from; 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. // Add captured piece to pool.
@ -82,6 +88,7 @@ impl Game {
if !swap { self.board.pieces[tid as usize] = None; } if !swap { self.board.pieces[tid as usize] = None; }
} }
// Set tile/piece associations.
if swap { if swap {
self.board.tiles[play.from as usize].piece = self.board.tiles[play.to as usize].piece; self.board.tiles[play.from as usize].piece = self.board.tiles[play.to as usize].piece;
} else { } else {
@ -92,10 +99,9 @@ impl Game {
self.board.pieces[pid as usize] = Some(piece); 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 hex = Hex::from_tile(play.from);
let offset = -(piece.player as i8 * 2) + 1; if !piece.promoted && !Hex::is_back(hex.x(), hex.y(), piece.player) {
if !piece.promoted && !Hex::is_valid(hex.x() as i8, hex.y() as i8 + offset) {
if let Some(piece) = &mut self.board.pieces[pid as usize] { if let Some(piece) = &mut self.board.pieces[pid as usize] {
piece.promoted = true; piece.promoted = true;
} }

View File

@ -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] = [ const COLUMNS :[(i8, i8); 9] = [
(0, 4), (0, 4),
@ -60,6 +60,24 @@ impl Hex {
let (min, max) = COLUMNS[x as usize]; let (min, max) = COLUMNS[x as usize];
y >= min && y <= max y >= min && y <= max
} else { false } } 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 } pub fn x(&self) -> u8 { self.x }

View File

@ -771,6 +771,8 @@ const INTERFACE = {
}, },
message(code, data) { message(code, data) {
if(data === null) { return; }
switch(code) { switch(code) {
case OpCode.GameState: { case OpCode.GameState: {
INTERFACE_DATA.player = data.player; INTERFACE_DATA.player = data.player;