Tweaks to AI for correctness

This commit is contained in:
Stephen Seo 2022-03-10 16:39:26 +09:00
parent 2a44cf810e
commit d8377d280a
2 changed files with 26 additions and 17 deletions

View file

@ -216,7 +216,7 @@ fn get_block_amount(player: Turn, idx: usize, amount: usize, board: &BoardType)
}
// check right
count = 0;
// count = 0; // don't reset count, since horizontal may pass through idx
temp_idx = idx;
while temp_idx % (COLS as usize) < (COLS - 1) as usize {
temp_idx += 1;
@ -260,6 +260,21 @@ fn get_block_amount(player: Turn, idx: usize, amount: usize, board: &BoardType)
}
}
// check diagonal right up
// count = 0; // don't reset count as diagonal may pass through idx
temp_idx = idx;
while temp_idx % (COLS as usize) < (COLS - 1) as usize && temp_idx / (COLS as usize) > 0 {
temp_idx = temp_idx + 1 - COLS as usize;
if board[temp_idx].get() == opposite.into() {
count += 1;
if count >= amount {
return true;
}
} else {
break;
}
}
// check diagonal right down
count = 0;
temp_idx = idx;
@ -278,7 +293,7 @@ fn get_block_amount(player: Turn, idx: usize, amount: usize, board: &BoardType)
}
// check diagonal left up
count = 0;
// count = 0; // don't reset count as diagonal may pass through idx
temp_idx = idx;
while temp_idx % (COLS as usize) > 0 && temp_idx / (COLS as usize) > 0 {
temp_idx = temp_idx - 1 - COLS as usize;
@ -292,21 +307,6 @@ fn get_block_amount(player: Turn, idx: usize, amount: usize, board: &BoardType)
}
}
// check diagonal right up
count = 0;
temp_idx = idx;
while temp_idx % (COLS as usize) < (COLS - 1) as usize && temp_idx / (COLS as usize) > 0 {
temp_idx = temp_idx + 1 - COLS as usize;
if board[temp_idx].get() == opposite.into() {
count += 1;
if count >= amount {
return true;
}
} else {
break;
}
}
// exhausted all possible potential wins, therefore does not block a win
false
}

View file

@ -197,6 +197,15 @@ pub fn new_empty_board() -> BoardType {
]
}
pub fn board_deep_clone(board: &BoardType) -> BoardType {
let cloned_board = new_empty_board();
for i in 0..board.len() {
cloned_board[i].replace(board[i].get());
}
cloned_board
}
pub type PlacedType = [Rc<Cell<bool>>; 56];
pub fn new_placed() -> PlacedType {