]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
More AI tweaks
authorStephen Seo <seo.disparate@gmail.com>
Thu, 10 Mar 2022 07:44:01 +0000 (16:44 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 10 Mar 2022 07:44:01 +0000 (16:44 +0900)
front_end/src/ai.rs

index 1c0d825437ff8bfe72753f528cc8babdecf4bd3b..8640f5d3d4132fa599819e4eabd59f49b6ea3406 100644 (file)
@@ -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)
 }