From 38e7772d4f78601ba570bfca8083989a9be91f36 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 10 Mar 2022 16:44:01 +0900 Subject: [PATCH] More AI tweaks --- front_end/src/ai.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/front_end/src/ai.rs b/front_end/src/ai.rs index 1c0d825..8640f5d 100644 --- a/front_end/src/ai.rs +++ b/front_end/src/ai.rs @@ -1,8 +1,9 @@ use std::collections::BTreeMap; use crate::constants::{AI_EASY_MAX_CHOICES, AI_NORMAL_MAX_CHOICES, COLS, ROWS}; +use crate::game_logic::check_win_draw; use crate::random_helper::get_seeded_random; -use crate::state::{BoardState, BoardType, Turn}; +use crate::state::{board_deep_clone, BoardState, BoardType, Turn}; #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum AIDifficulty { @@ -190,6 +191,18 @@ fn get_utility_for_slot(player: Turn, slot: SlotChoice, board: &BoardType) -> Op } } + // check if placing a token here allows the opposing player to win + if idx >= COLS as usize { + let cloned_board = board_deep_clone(board); + cloned_board[idx].replace(player.into()); + cloned_board[idx - (COLS as usize)].replace(player.get_opposite().into()); + if let Some((state, _)) = check_win_draw(&cloned_board) { + if state == player.get_opposite().into() { + utility *= 0.1; + } + } + } + Some(utility) }