public static Task CancellableSearch(CancellationToken ct, int search_type) { //https://arghya.xyz/articles/task-cancellation/ return(Task.Factory.StartNew(async() => { int iterative_depth = Option.depth_of_search; while (true) { if (search_type == 0)//Negamax { double x = await AI.negamax_id(GameState.game_state, iterative_depth, double.NegativeInfinity, double.PositiveInfinity, ct); } if (search_type == 1) { double x = await AI.pvs_id(GameState.game_state, iterative_depth, double.NegativeInfinity, double.PositiveInfinity, ct); } if (search_type == 2) { double x = await AI.minimax_alpha_beta_pruning_id(GameState.game_state, Option.depth_of_search, double.NegativeInfinity, double.PositiveInfinity, true, ct); } AI.set_ai_move_iterative(AI.ai_move); string info = "(" + AI.get_ai_move_iterative().row.ToString() + "," + AI.get_ai_move_iterative().column.ToString() + "," + "value:" + AI.ai_state_iterative_deepening.value.ToString() + ")"; AI.ai_state_iterative_deepening = AI.ai_state; iterative_depth += 1; depth_deepening = iterative_depth; } }, ct)); }
private void Form1_Load(object sender, EventArgs e) { set_labels(); set_timer(); GameStatic.all_hexes = new List <Hexagon>(); GameStatic.hexes_in_board = new List <Hexagon>(); GameStatic.hexes_board_dict = new Dictionary <Tuple <int, int>, Hexagon>(); GameState.game_history = new Stack(); set_all_hexes(Option.first_center_x, Option.first_center_y); GameStatic.hexes_in_board = set_hexes_board(GameStatic.all_hexes); //var span = new Span<Hexagon>(hexes_board.ToArray()); for (int i = 0; i < GameStatic.hexes_in_board.Count; i++) { GameStatic.hexes_board_dict.Add(Tuple.Create(GameStatic.hexes_in_board[i].row, GameStatic.hexes_in_board[i].column), GameStatic.hexes_in_board[i]); } if (Option.isplayer2_turn) { Hexagon player1_hex = GameStatic.hexes_in_board.Find(hex => hex.row == Option.row_init_coin && hex.column == Option.col_init_coin); set_initial_state(player1_hex); } else { Hexagon player2_hex = GameStatic.hexes_in_board.Find(hex => hex.row == Option.row_init_coin && hex.column == Option.col_init_coin); set_initial_state(player2_hex); } txtbox_number_possible_hexes.Text = GameState.game_state.possible_hexes.Count.ToString(); GameState.game_history.Push(GameState.game_state); if (Option.i_play_second) { make_ai_move(is_iterative_depth); depth_game++; if (is_iterative_depth) { GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, AI.get_ai_move_iterative()); } else { GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, AI.ai_move); } Option.isplayer1_turn = true; Option.isplayer2_turn = false; label_player_turn.Text = "Player 1"; label_player_turn.ForeColor = Color.RoyalBlue; } //Zobrist.generate_zobrist_table(false); }
public async void make_ai_move(bool is_iterative_deepening) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); double value; //value = AI.negamax(GameState.game_state, Option.depth_of_search, double.NegativeInfinity, double.PositiveInfinity); //value = AI.pvs(GameState.game_state, 3, double.NegativeInfinity, double.PositiveInfinity); if (is_iterative_deepening) { run_iterative_deepening(0); } else { if (checkBox1.Checked) { value = AI.minimax(GameState.game_state, Option.depth_of_search, true); } if (checkBox2.Checked) { value = AI.minimax_alpha_beta_pruning(GameState.game_state, Option.depth_of_search, double.NegativeInfinity, double.PositiveInfinity, true); } if (checkBox3.Checked) { value = AI.negamax(GameState.game_state, Option.depth_of_search, double.NegativeInfinity, double.PositiveInfinity); } if (checkBox4.Checked) { value = AI.pvs(GameState.game_state, Option.depth_of_search, double.NegativeInfinity, double.PositiveInfinity); } } if (!checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked && !checkBox4.Checked) { value = AI.negamax(GameState.game_state, Option.depth_of_search, double.NegativeInfinity, double.PositiveInfinity); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTimeString = string.Format("{0:00}:{1:00}:{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds / 10); if (is_iterative_depth) { label_ai_move_result.Text = "(" + AI.get_ai_move_iterative().row.ToString() + "," + AI.get_ai_move_iterative().column.ToString() + "," + "value:" + AI.ai_state_iterative_deepening.value.ToString() + ")"; label_ai_move_stats.Text = elapsedTimeString + ", depth of iterative deepening = " + depth_deepening.ToString(); } else { label_ai_move_result.Text = "(" + AI.ai_move.row.ToString() + "," + AI.ai_move.column.ToString() + "," + "value:" + AI.ai_state.value.ToString() + ")"; label_ai_move_stats.Text = elapsedTimeString + ", depth of iterative deepening = " + depth_deepening.ToString(); } }
public void picGrid_MouseClick(object sender, MouseEventArgs e) { //MessageBox.Show(GameState.game_state.depth.ToString()); List <float> distances = new List <float>(); for (int i = 0; i < GameStatic.hexes_in_board.Count; i++) { distances.Add((float)Math.Sqrt(Math.Pow(GameStatic.hexes_in_board[i].center.Y - e.Y, 2) + Math.Pow(GameStatic.hexes_in_board[i].center.X - e.X, 2))); } float min_distance = distances.Min(); int index_min_distance = distances.IndexOf(min_distance); int row_clicked = GameStatic.hexes_in_board[index_min_distance].row; int col_clicked = GameStatic.hexes_in_board[index_min_distance].column; Hexagon move = GameStatic.hexes_in_board[index_min_distance]; //bool result; //when you click, if (!Option.isplayer1_turn && Option.isplayer2_turn)//Human TURN == p2 { int number_hexes_player2_before = GameState.game_state.state_player2_hexes.Count; if (GameState.game_state.possible_hexes.Any(hex => hex.Equals(move))) { depth_game++; GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, move); if (GameState.game_state.is_game_over) { MessageBox.Show("Game Over. Player 2 (Red) wins !"); } GameState.game_history.Push(GameState.game_state); make_ai_move(is_iterative_depth); depth_game++; if (is_iterative_depth) { GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, AI.get_ai_move_iterative()); } else { GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, AI.ai_move); } //stop timer if (GameState.game_state.is_game_over) { MessageBox.Show("Game Over. Player 1 (Blue) wins !"); } GameState.game_history.Push(GameState.game_state); } if (GameState.game_state.state_player1_hexes.Count > number_hexes_player2_before) { //Option.isplayer1_turn = true; //label_player_turn.Text = "Player 1"; //label_player_turn.ForeColor = Color.RoyalBlue; //Option.isplayer2_turn = false; Option.isplayer1_turn = false; Option.isplayer2_turn = true; label_player_turn.Text = "Player 2"; label_player_turn.ForeColor = Color.Crimson; txtbox_number_possible_hexes.Text = GameState.game_state.possible_hexes.Count.ToString(); picGrid.Refresh(); } else { MessageBox.Show("Impossible move ! Please choose another place"); return; } } else//AI Turn == p1 { int number_hexes_player1_before = GameState.game_state.state_player1_hexes.Count; if (GameState.game_state.possible_hexes.Any(hex => hex.row == row_clicked && hex.column == col_clicked)) { depth_game++; GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, move); if (GameState.game_state.is_game_over) { MessageBox.Show("Game Over. Player 1 (Blue) wins !"); } GameState.game_history.Push(GameState.game_state); make_ai_move(is_iterative_depth); depth_game++; if (is_iterative_depth) { GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, AI.get_ai_move_iterative()); } else { GameState.game_state = GameState.game_state.get_state_after_move(GameState.game_state, AI.ai_move); } if (GameState.game_state.is_game_over) { MessageBox.Show("Game Over. Player 2 (Red) wins !"); } GameState.game_history.Push(GameState.game_state); } if (GameState.game_state.state_player1_hexes.Count > number_hexes_player1_before) { //Option.isplayer1_turn = false; //Option.isplayer2_turn = true; //label_player_turn.Text = "Player 2"; //label_player_turn.ForeColor = Color.Crimson; Option.isplayer1_turn = true; Option.isplayer2_turn = false; label_player_turn.Text = "Player 1"; label_player_turn.ForeColor = Color.RoyalBlue; txtbox_number_possible_hexes.Text = GameState.game_state.possible_hexes.Count.ToString(); picGrid.Refresh(); } else { MessageBox.Show("Impossible move ! Please choose another place"); return; } } //dataGridView1.Rows.Clear(); }