Exemplo n.º 1
0
 public UltimateTicTacToeBoard()
 {
     for (int i = 0; i < 9; i++)
     {
         _grid[i] = new TicTacToeBoard();
     }
     State       = GameState.Open;
     WinningMove = null;
 }
Exemplo n.º 2
0
        private static short CountWins(Player player, TicTacToeBoard board)
        {
            var wins = (short)(CanWin(player, board, new[] { 0, 1, 2 }) ? 1 : 0);

            wins += (short)(CanWin(player, board, new[] { 3, 4, 5 }) ? 1 : 0);
            wins += (short)(CanWin(player, board, new[] { 6, 7, 8 }) ? 1 : 0);
            wins += (short)(CanWin(player, board, new[] { 0, 3, 6 }) ? 1 : 0);
            wins += (short)(CanWin(player, board, new[] { 1, 4, 7 }) ? 1 : 0);
            wins += (short)(CanWin(player, board, new[] { 2, 5, 8 }) ? 1 : 0);
            wins += (short)(CanWin(player, board, new[] { 0, 4, 8 }) ? 1 : 0);
            wins += (short)(CanWin(player, board, new[] { 2, 4, 6 }) ? 1 : 0);
            return(wins);
        }
Exemplo n.º 3
0
        private static short CalculateScore(TicTacToeBoard board)
        {
            short xWins = 0;
            short oWins = 0;

            if (board.State == GameState.Xwin)
            {
                xWins = 100;
            }
            else if (board.State == GameState.Owin)
            {
                oWins = 100;
            }
            else if (board.State == GameState.Open)
            {
                xWins = CountWins(Player.X, board);
                oWins = CountWins(Player.O, board);
            }
            return((short)(xWins - oWins));
        }
Exemplo n.º 4
0
        internal TicTacToeBoard Clone(bool cloneMoves = false)
        {
            var clonedBoard = new TicTacToeBoard();

            if (cloneMoves)
            {
                foreach (var move in _moves)
                {
                    clonedBoard._moves.AddLast(move);
                }
            }
            else if (_moves.Count >= 1)
            {
                clonedBoard._moves.AddLast(_moves.Last.Value);
            }
            clonedBoard.WinningMove = WinningMove;
            clonedBoard.State       = State;
            _grid.CopyTo(clonedBoard._grid, 0);
            return(clonedBoard);
        }
Exemplo n.º 5
0
 private static bool CanWin(Player player, TicTacToeBoard board, IEnumerable <int> cells)
 {
     return(cells.All(cell => board.CellValue((byte)cell) == Player.Empty || board.CellValue((byte)cell) == player));
 }