public reversi make_move(possible_move move) { reversi result = new reversi(this.black, this.white); result.parent = this; var self = result.black; var enemy = result.white; if (move.color) //white { self = result.white; enemy = result.black; } int st = 0; if (move.color) { st = 1; } result.state[move.pos] = st; self.Add(move.pos); foreach (int x in move.flipped) { result.state[x] = st; self.Add(x); enemy.RemoveAt(enemy.FindIndex(a => a == x)); } return(result); }
public possible_move ai_make_move(int limit) { var frontier = get_frontier(false); int[] h = new int[frontier.Count]; int i = 0; foreach (var m in frontier) { reversi child = make_move(m); h[i] = alphabeta(child, limit, -10000, 10000, false); ++i; } possible_move result = frontier[0]; int g = h[0]; for (int j = 1; j < frontier.Count; ++j) { if (h[j] > g) { result = frontier[j]; g = h[j]; } else if (h[j] == g) { Random random = new Random(); if (random.NextDouble() > 0.5) { g = h[j]; } } } return(result); }
private void cellClick(object sender, EventArgs e) { if (!humans_turn) { return; } PictureBox p = sender as PictureBox; int tag = (int)p.Tag; if (frontier.Find(f => f.pos == tag).flipped == null) // not a valid move { return; } foreach (var x in frontier) { board[x.pos].BackgroundImage = null; board[x.pos].Refresh(); } game = game.make_move(frontier.Find(f => f.pos == tag)); display_state(); computer_score.Text = game.black.Count.ToString(); your_score.Text = game.white.Count.ToString(); Computer: humans_turn = false; turn.Text = "Computer's turn!"; turn.ForeColor = Color.Maroon; turn.Invalidate(); turn.Update(); computer_move(); display_state(); computer_score.Text = game.black.Count.ToString(); your_score.Text = game.white.Count.ToString(); frontier = game.get_frontier(true); if (frontier == null) { if (game.game_over()) { gameover(); return; } MessageBox.Show("you have no moves"); //you have no moves goto Computer; } foreach (var x in frontier) { board[x.pos].BackgroundImage = Properties.Resources.hint; } humans_turn = true; turn.Text = "Your turn!"; turn.ForeColor = Color.Green; }
int alphabeta(reversi st, int depth, int α, int β, bool maximizing_player) { var frontier = st.get_frontier(maximizing_player); if (depth == 0 || frontier == null) { return(st.eval_state(maximizing_player)); } possible_move bm; if (!maximizing_player) { int v = -100000; foreach (var pos in frontier) { bm = pos; reversi child = st.make_move(pos); v = Math.Max(v, st.alphabeta(child, depth - 1, α, β, !maximizing_player)); α = Math.Max(α, v); if (β <= α) { break;// (*β cut - off *) } } return(v); } else { int v = 100000; foreach (var pos in frontier) { bm = pos; reversi child = st.make_move(pos); v = Math.Min(v, st.alphabeta(child, depth - 1, α, β, !maximizing_player)); β = Math.Min(β, v); if (β <= α) { break;// (*β cut - off *) } } return(v); } }
private void reset() { game = new reversi(); computer_score.Text = game.black.Count.ToString(); your_score.Text = game.white.Count.ToString(); for (int i = 0; i < 64; ++i) { board[i].BackgroundImage = null; /////////////////////////////////////////////////////////////////////////// //board[i].Image = new Bitmap(board[i].Width, board[i].Height); //using (Font myFont = new Font("Arial", 14)) //{ // Bitmap b = board[i].Image as Bitmap; // Graphics g = Graphics.FromImage(b); // g.DrawString(board[i].Tag.ToString(), myFont, Brushes.Green, new Point(2, 2)); //} //////////////////////////////////////////////////////////////////////////// } foreach (var x in game.black) { board[x].BackgroundImage = Properties.Resources.black; } foreach (var x in game.white) { board[x].BackgroundImage = Properties.Resources.white; } for (int i = 0; i < 64; ++i) { board[i].Refresh(); } computer_score.Text = "2"; your_score.Text = "2"; turn.Text = ""; }
private void computer_move() { frontier = game.get_frontier(false); if (frontier == null) { if (game.game_over()) { gameover(); return; } MessageBox.Show("computer has no moves"); // computer has no moves return; } possible_move m = game.ai_make_move((int)ab_depth.Value); board[m.pos].BackgroundImage = Properties.Resources.hint; board[m.pos].Refresh(); System.Threading.Thread.Sleep(100); game = game.make_move(m); display_state(); computer_score.Text = game.black.Count.ToString(); your_score.Text = game.white.Count.ToString(); }