Exemplo n.º 1
0
 private void ExitApp()
 {
     GameStatus = GameStatus.GameOver;
 }
Exemplo n.º 2
0
        public int addMove(bool CurrentPlayer, int row, int col)
        {
            row--; col--;
            if (this.currPlayer != CurrentPlayer)
            {
                throw new Exception("other player's turn");
            }
            else if (GameStatus != GameStatus.Active)
            {
                throw new Exception("Game has been finished");
            }
            else if (row >= n | col >= n | row < 0 | col < 0)
            {
                throw new Exception("index out of range");
            }

            else if (Board[row, col] != 0)
            {
                throw new Exception("square is already taken");
            }

            else
            {
                try
                {
                    this.currPlayer = !this.currPlayer;
                    Moves.Add(new Tuple <int, int>(row, col));
                    int Player = CurrentPlayer ? 1 : -1;
                    Board[row, col] = Player;
                    int vert, horz, diag, reverDiag;
                    vert = horz = diag = reverDiag = 0;

                    for (int i = 0; i < n; i++)
                    {
                        vert += Board[(row + i) % n, col];
                    }

                    for (int i = 0; i < n; i++)
                    {
                        horz += Board[row, (col + i) % n];
                    }

                    if (row == col)
                    {
                        for (int i = 0; i < n; i++)
                        {
                            diag += Board[(row + i) % n, (col + i) % n];
                        }
                    }

                    if (row == n - 1 - col)
                    {
                        for (int i = 0; i < n; i++)
                        {
                            reverDiag += Board[(row + i) % n, (col - i + n) % n];
                        }
                    }
                    int temp = Player * n;
                    if (vert == temp | horz == temp | diag == temp | reverDiag == temp)
                    {
                        if (CurrentPlayer)
                        {
                            GameStatus = GameStatus.Player1Win;
                        }
                        else
                        {
                            GameStatus = GameStatus.Player2Win;
                        }
                        showBoard();
                        return(Player);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            showBoard();
            return(0);
        }