コード例 #1
0
        public void ClaimCell(TicTacToeCellStatus player)
        {
            if (Status != TicTacToeCellStatus.Unclaimed)
            {
                throw new TicTacToeException("Cell is already claimed");
            }

            if (player == TicTacToeCellStatus.Unclaimed)
            {
                throw new TicTacToeException("\'Unclaimed\' cannot claim a cell");
            }

            Status = player;
        }
コード例 #2
0
        public void ClaimCell(int X, int Y, TicTacToeCellStatus player)
        {
            if (GridWinner != TicTacToeGridStatus.Contested)
            {
                throw new TicTacToeException("Grid is already filled in");
            }

            if (X < 0 || X > 2 || Y < 0 || Y > 2)
            {
                throw new ArgumentException("column or row selection invalid: " + X + ", " + Y);
            }

            grid[X, Y].ClaimCell(player);
            CheckWinner(X, Y, player);
        }
コード例 #3
0
 private void DeclareWinner(TicTacToeCellStatus player)
 {
     if (player == TicTacToeCellStatus.X)
     {
         GridWinner = TicTacToeGridStatus.X;
     }
     else if (player == TicTacToeCellStatus.O)
     {
         GridWinner = TicTacToeGridStatus.O;
     }
     else
     {
         GridWinner = TicTacToeGridStatus.Stalemate;
     }
     OnGridWin();
     return;
 }
コード例 #4
0
        public void ClaimCell(int gridX, int gridY, int X, int Y)
        {
            if (Winner != TicTacToeWinner.Contested)
            {
                throw new TicTacToeException("Cannot claim cell after a winner has been declared");
            }

            if (gridX < 0 || gridX > 2 || gridY < 0 || gridY > 2)
            {
                throw new ArgumentException("column or row selection invalid: " + gridX + ", " + gridY);
            }

            TicTacToeCellStatus player = WhoseTurn == TicTacToePlayerTurn.X ? TicTacToeCellStatus.X : TicTacToeCellStatus.O;

            if (NextMoveX == -1 || (gridX == NextMoveX && gridY == NextMoveY))
            {
                subGrids[gridX, gridY].ClaimCell(X, Y, player);

                //check for subgrid win, raise event and check for win conditions
                if (subGrids[gridX, gridY].GridWinner != TicTacToeGridStatus.Contested)
                {
                    OnSubgridGridWin(gridX, gridY, subGrids[gridX, gridY].GridWinner);
                    CheckWinConditions(gridX, gridY);
                }

                //set the subgrid for the next move
                if (subGrids[X, Y].GridWinner == TicTacToeGridStatus.Contested)
                {
                    NextMoveX = X;
                    NextMoveY = Y;
                }
                else
                {
                    NextMoveX = -1;
                    NextMoveY = -1;
                }

                OnMove(gridX, gridY, X, Y);
                WhoseTurn = WhoseTurn == TicTacToePlayerTurn.X ? TicTacToePlayerTurn.O : TicTacToePlayerTurn.X;
            }
            else
            {
                throw new TicTacToeException("player must make a move in subgrid " + NextMoveX.ToString() + ", " + NextMoveY.ToString() + "!");
            }
        }
コード例 #5
0
        private void CheckWinner(int column, int row, TicTacToeCellStatus player)
        {
            //check for horizontal winner
            if (grid[0, row].Status == grid[1, row].Status &&
                grid[1, row].Status == grid[2, row].Status)
            {
                DeclareWinner(player);
                return;
            }

            //check for vertical winner
            if (grid[column, 0].Status == grid[column, 1].Status &&
                grid[column, 1].Status == grid[column, 2].Status)
            {
                DeclareWinner(player);
                return;
            }

            //check for diagonal winner
            if ((grid[0, 0].Status == grid[1, 1].Status && grid[1, 1].Status == grid[2, 2].Status && grid[1, 1].Status != TicTacToeCellStatus.Unclaimed) ||
                (grid[0, 2].Status == grid[1, 1].Status && grid[1, 1].Status == grid[2, 0].Status && grid[1, 1].Status != TicTacToeCellStatus.Unclaimed))
            {
                DeclareWinner(player);
                return;
            }

            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    if (grid[i, j].Status == TicTacToeCellStatus.Unclaimed)
                    {
                        return;
                    }
                }
            }
            DeclareWinner(TicTacToeCellStatus.Unclaimed);
        }
コード例 #6
0
 public void Reset()
 {
     Status = TicTacToeCellStatus.Unclaimed;
 }