Пример #1
0
        public override void Update(GameTime gameTime)
        {
            var gameOverPrompt = GameOverPrompt.GetInstance(Game);

            // Handle keyboard inputs
            if (gameOverPrompt.IsVisible)
            {
                if (Keyboard.GetState().GetPressedKeys().Length > 0)
                {
                    // The user wants to continue?
                    if (Keyboard.GetState().GetPressedKeys().Contains(Keys.Enter))
                    {
                        // Hide the prompt
                        gameOverPrompt.Hide();
                        // Reset the game state
                        GameState.Reset();
                        // Reset this board
                        this.Reset();
                    }
                    else
                    {
                        GameState.ShouldQuit = true;
                    }
                }
            }

            if (GameState.IsGameOver && !gameOverPrompt.IsVisible)
            {
                gameOverPrompt.Show();
            }
            else             // Check if any of the gameover conditions are satisfied
            {
                // Check the diagonals
                var gameOver = isSame(tiles[0][0], tiles[1][1], tiles[2][2]) ||
                               isSame(tiles[0][2], tiles[1][1], tiles[2][0]);

                // Check the columns
                for (int i = 0; i < 3; i++)
                {
                    gameOver = gameOver || isSame(tiles[i].ToArray());                     // Columns
                }
                // Check the rows
                for (int i = 0; i < 3; i++)
                {
                    gameOver = gameOver || isSame(tiles.Select(x => x[i]).ToArray());                     // Rows
                }

                // Set the winner
                if (gameOver)
                {
                    GameState.Winner = GameState.Turn;
                }
                // Check if the board is full, it's a draw
                else if (tiles.All(l => l.Where(t => t.Value == TileValue.EMPTY).Count() == 0))
                {
                    GameState.Winner = TileValue.EMPTY;
                }
            }
            base.Update(gameTime);
        }
Пример #2
0
 public static GameOverPrompt GetInstance(Game game)
 {
     if (instance == null)
     {
         instance = new GameOverPrompt(game);
     }
     return(instance);
 }