Basic impl. of AI (enough completed to use/test)
This commit is contained in:
parent
ea2625cb48
commit
1936e5ce22
1 changed files with 43 additions and 13 deletions
|
@ -142,18 +142,48 @@ fn get_utility_for_slot(player: Turn, slot: SlotChoice, board: &BoardType) -> Op
|
||||||
idx += COLS as usize;
|
idx += COLS as usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if placing a token here blocks a win
|
// check if placing a token here is a win
|
||||||
if get_block_win(player, idx, board) {
|
if get_block_amount(player.get_opposite(), idx, 3, board) {
|
||||||
return Some(1.0);
|
return Some(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO more impl here
|
// check if placing a token here blocks a win
|
||||||
|
if get_block_amount(player, idx, 3, board) {
|
||||||
|
return Some(0.9);
|
||||||
|
}
|
||||||
|
|
||||||
Some(0.0)
|
let mut utility: f64 = 0.5;
|
||||||
|
|
||||||
|
// check if placing a token here connects 2 pieces
|
||||||
|
if get_block_amount(player.get_opposite(), idx, 2, board) {
|
||||||
|
utility *= 1.5;
|
||||||
|
if utility >= 0.8 {
|
||||||
|
utility = 0.8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if placing a token here blocks 2 pieces
|
||||||
|
if get_block_amount(player, idx, 2, board) {
|
||||||
|
utility *= 1.2;
|
||||||
|
if utility >= 0.8 {
|
||||||
|
utility = 0.8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if placing a token here connects 1 piece
|
||||||
|
if get_block_amount(player.get_opposite(), idx, 1, board) {
|
||||||
|
utility *= 1.09;
|
||||||
|
if utility >= 0.8 {
|
||||||
|
utility = 0.8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(utility)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if placing a token at idx will block the opposite player from winning
|
/// Returns true if placing a token at idx will block the opposite player that
|
||||||
fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
/// has "amount" in a line (horizontally, vertically, and diagonally).
|
||||||
|
fn get_block_amount(player: Turn, idx: usize, amount: usize, board: &BoardType) -> bool {
|
||||||
let opposite = player.get_opposite();
|
let opposite = player.get_opposite();
|
||||||
|
|
||||||
// setup for checks
|
// setup for checks
|
||||||
|
@ -165,7 +195,7 @@ fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
||||||
temp_idx -= 1;
|
temp_idx -= 1;
|
||||||
if board[temp_idx].get() == opposite.into() {
|
if board[temp_idx].get() == opposite.into() {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count >= 3 {
|
if count >= amount {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -180,7 +210,7 @@ fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
||||||
temp_idx += 1;
|
temp_idx += 1;
|
||||||
if board[temp_idx].get() == opposite.into() {
|
if board[temp_idx].get() == opposite.into() {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count >= 3 {
|
if count >= amount {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -195,7 +225,7 @@ fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
||||||
temp_idx += COLS as usize;
|
temp_idx += COLS as usize;
|
||||||
if board[temp_idx].get() == opposite.into() {
|
if board[temp_idx].get() == opposite.into() {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count >= 3 {
|
if count >= amount {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -210,7 +240,7 @@ fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
||||||
temp_idx = temp_idx - 1 + COLS as usize;
|
temp_idx = temp_idx - 1 + COLS as usize;
|
||||||
if board[temp_idx].get() == opposite.into() {
|
if board[temp_idx].get() == opposite.into() {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count >= 3 {
|
if count >= amount {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -227,7 +257,7 @@ fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
||||||
temp_idx = temp_idx + 1 + COLS as usize;
|
temp_idx = temp_idx + 1 + COLS as usize;
|
||||||
if board[temp_idx].get() == opposite.into() {
|
if board[temp_idx].get() == opposite.into() {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count >= 3 {
|
if count >= amount {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -242,7 +272,7 @@ fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
||||||
temp_idx = temp_idx - 1 - COLS as usize;
|
temp_idx = temp_idx - 1 - COLS as usize;
|
||||||
if board[temp_idx].get() == opposite.into() {
|
if board[temp_idx].get() == opposite.into() {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count >= 3 {
|
if count >= amount {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -257,7 +287,7 @@ fn get_block_win(player: Turn, idx: usize, board: &BoardType) -> bool {
|
||||||
temp_idx = temp_idx + 1 - COLS as usize;
|
temp_idx = temp_idx + 1 - COLS as usize;
|
||||||
if board[temp_idx].get() == opposite.into() {
|
if board[temp_idx].get() == opposite.into() {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count >= 3 {
|
if count >= amount {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue