private void NewGame(GameState state) { this.state = state; this.board = new Board(state.Cells); board.Margin = new Thickness(20); board.SetValue(Grid.RowProperty, 0); board.SetValue(Grid.ColumnProperty, 0); this.root.Children.Add(board); IPlayer player1 = new Player(Reversi.BLACK, this); IPlayer player2 = null; if (state.GameMode == GameMode.OnePlayer) { int difficulty = Math.Max(0, Math.Min(state.Difficulty, 2)); player2 = new Strategy(Reversi.WHITE, difficulty); this.difficultyLabel.Visibility = Windows.UI.Xaml.Visibility.Visible; this.difficultyValue.Text = Reversi.DifficultyToString(difficulty); this.difficultyValue.Visibility = Windows.UI.Xaml.Visibility.Visible; } else { player2 = new Player(Reversi.WHITE, this); } if (state.TimeStamp == null) { state.TimeStamp = DateTime.Now; } t0 = state.TimeStamp.Value; timer.Start(); var game = new Game(board, player1, player2); game.Update += boardUpdatedHandler; game.GameOver += gameOverHandler; game.Begin(state.Player != Reversi.WHITE ? player1 : player2); }
private static Task <Move> FindBestMove(IEnumerable <Move> moves, int[][] cells, int color, int depth) { return(Task.Run <Move>(() => { Move bestMove = null; int bestscore = int.MinValue; if (moves != null) { foreach (var move in moves) { var copy = Reversi.Clone(cells); Reversi.TryPlay(copy, move); // find the best move our opponent can make. Hence, the minus sign. int ss = -Score(copy, 0, -color, depth, false); if (ss > bestscore) { bestscore = ss; bestMove = move; } } } return bestMove; })); }
public ChooseDifficultyPage() { this.InitializeComponent(); this.SizeChanged += (s, e) => { Reversi.SetView(this.filledView, this.filledView, this.filledView, this.snapView); }; }
public Pawn(int color) { this.InitializeComponent(); this.color = color; myBrush.Color = Reversi.IntToColor(color); myStoryboard1.Completed += myStoryboard1_Completed; myStoryboard2.Completed += myStoryboard2_Completed; }
public CreditsPage() { this.InitializeComponent(); this.SizeChanged += (s, e) => { Reversi.SetView(this.unsnappedView, this.unsnappedView, this.unsnappedView, this.snapView); }; }
public GamePage() { this.InitializeComponent(); this.SizeChanged += (s, e) => { Reversi.SetView(this.unsnappedView, this.unsnappedView, this.unsnappedView, this.snapView); }; tickHandler = new EventHandler <object>(dispatcherTimer_Tick); boardUpdatedHandler = new EventHandler <GameUpdateArgs>(game_BoardUpdated); gameOverHandler = new EventHandler(game_GameOver); timer.Tick += tickHandler; }
private IList <Move> GetValidMoves(int color, int[][] state) { var answer = new List <Move>(); for (int i = 0; i < Board.ROWS; i++) { for (int j = 0; j < Board.COLS; j++) { if (state[i][j] == Reversi.UNDEFINED) { var positions = Reversi.GetPositionsToFlip(i, j, color, state); if (positions.Count > 0) { answer.Add(new Move { Row = i, Col = j, Color = color, PositionsToFlip = positions }); } } } } return(answer); }
private static int Score(int[][] cells, int d, int color, int depth, bool pass // true means our opponent did not have any move to play; he passed ) { int other = -color; if (d > depth) { // time to stop recursion int s = 0; for (int i = 0; i < _corners.Length; i++) { var corner = _corners[i]; var xsquare = _xsquares[i]; var q = cells[corner.Row][corner.Col]; if (q == color) { s += 300; } else if (q == other) { s -= 300; } else { q = cells[xsquare.Row][xsquare.Col]; if (q == color) { s -= 50; } else if (q == other) { s += 50; } } } return(s); } int bestScore = int.MinValue; int n = 0; // will store the # of valid moves we can make for (int row = 0; row < Board.ROWS; row++) { for (int col = 0; col < Board.COLS; col++) { var copy = Reversi.Clone(cells); var move = new Move { Row = row, Col = col, Color = color, PositionsToFlip = Reversi.GetPositionsToFlip(row, col, color, copy) }; if (Reversi.TryPlay(copy, move)) { n++; // find the score our opponent can make. int ss = -Score(copy, d + 1, other, depth, false); if (ss > bestScore) { bestScore = ss; } } } } if (n == 0) { // there is no valid move that we can play. we can't set bestPos to any meaningful value. if (pass) { // opponent also had no move to play // this means game will end for (int i = 0; i < Board.ROWS; i++) { for (int j = 0; j < Board.COLS; j++) { if (cells[i][j] == color) { n++; } else if (cells[i][j] == other) { n--; } } } if (n > 0) { // we will win. return(n + 8000); } else { // we will lose. return(n - 8000); } } else { bestScore = -Score(cells, d + 1, other, depth, true); } } if (d >= depth - 1) { return(bestScore + (n << 3)); } else { return(bestScore); } }