public GameBoardScene(Game game)
            : base(game,
                game.Content.Load<Texture2D>("bg"),
                new DrawState(game, new Vector4(2f, 0f, 1f, 1f), Color.Gray))
        {
            _backgroundSong = game.Content.Load<SoundEffect>("SunsetParkModern");
            _victory = game.Content.Load<SoundEffect>("25");

            _backHover = false;

            _start_ = game.Content.Load<SoundEffect>("save");
            _start = _start_.CreateInstance();
            _clickError_ = game.Content.Load<SoundEffect>("negative_2");
            _clickError = _clickError_.CreateInstance();

            _board = new Board(game, game.Content.Load<Texture2D>("transparent"),
                new DrawState(game, new Vector4(0.65f, 0.5f, 0f, 0f), Color.White));
            _board.AddState(0.5f, new DrawState(game, new Vector4(0.65f, 0.5f, 0f, 0f), Color.Gray));
            _board.AddState(0.5f, new DrawState(game, new Vector4(0.35f, 0.1f, 0.6f, 0.8f), Color.Gray));

            _hareWin = new GraphComponent(game, game.Content.Load<Texture2D>("RabbitWin"),
                new DrawState(game, new Vector4(0.5f, 0.5f, 0.0f, 0.0f), Color.PowderBlue));
            _tortoiseWin = new GraphComponent(game, game.Content.Load<Texture2D>("TurtleWin"),
                new DrawState(game, new Vector4(0.5f, 0.5f, 0.0f, 0.0f), Color.PowderBlue));
            _board.AddComponent(_hareWin);
            _board.AddComponent(_tortoiseWin);

            _panel = new Container(game, game.Content.Load<Texture2D>("blank"),
                new DrawState(game, new Vector4(0f, 0f, 0f, 1.0f), new Color(0.0f, 0.0f, 0.0f, 0.3f)));
            _panel.AddState(0.5f, new DrawState(game, new Vector4(0f, 0f, 0f, 1.0f), new Color(0.0f, 0.0f, 0.0f, 0.3f)));
            _panel.AddState(0.5f, new DrawState(game, new Vector4(0f, 0f, 0.3f, 1.0f), new Color(0.0f, 0.0f, 0.0f, 0.3f)));

            _alertMessage = new FontComponent(game, game.Content.Load<SpriteFont>("Font"),
                new DrawState(game, new Vector4(0.1f, 0.25f, 0.8f, 0f), new Color(0.0f, 0.0f, 0.0f, 0.0f)));
            _alertMessage.Content = "不能移動狀況產生!";
            _view = new FontComponent(game, game.Content.Load<SpriteFont>("Font"),
                new DrawState( game, new Vector4( 0.2f, 0.05f, 0.0f, 0.0f ), Color.White));
            _view.AddState(0.5f, new DrawState(game, new Vector4(0.2f, 0.05f, 0.0f, 0.0f), Color.White));
            _view.AddState(0.5f, new DrawState(game, new Vector4(0.25f, 0.05f, 0.3f, 0.0f), Color.White));
            _view.Content = "狀態";
            _stateView = new FontComponent(game, game.Content.Load<SpriteFont>("Font"),
                new DrawState(game, new Vector4(0.1f, 0.15f, 0.0f, 0.0f), Color.White));
            _stateView.AddState(0.5f, new DrawState(game, new Vector4(0.1f, 0.15f, 0.0f, 0.0f), Color.White));
            _stateView.AddState(0.5f, new DrawState(game, new Vector4(0.1f, 0.15f, 0.5f, 0.0f), Color.White));
            _stateView.Content = "初始化中...";
            _tortoise = new GraphComponent(game, game.Content.Load<Texture2D>("Turtle"),
                new DrawState(game, new Vector4(0.4f, 0.3f, 0.4f, 0.2f), Color.White));
            _hare = new GraphComponent(game, game.Content.Load<Texture2D>("Rabbit"),
                new DrawState(game, new Vector4(0.4f, 0.5f, 0.4f, 0.2f), Color.White));
            _rightArrow = new GraphComponent(game, game.Content.Load<Texture2D>("rightarrow"),
                new DrawState(game, new Vector4(0.1f, 0.3f, 0.4f, 0.2f), Color.White));
            _turn = Board.Turn.TortoiseTurn;
            _nomove = false;

            _back = new GraphComponent(game, game.Content.Load<Texture2D>("Previous"),
                new DrawState(game, new Vector4(0.3f, 0.75f, 0.4f, 0.2f), Color.White));

            AddComponent(_board);
            _panel.AddComponent(_view);
            _panel.AddComponent(_stateView);
            _panel.AddComponent(_alertMessage);
            _panel.AddComponent(_tortoise);
            _panel.AddComponent(_hare);
            _panel.AddComponent(_rightArrow);
            _panel.AddComponent(_back);
            AddComponent(_panel);

            _isComputed = false;
        }
Example #2
0
        /// <summary>
        /// The tic-tac-toe board is clicked.
        /// The row and column are determined.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tictactoe_board_Click(object sender, EventArgs e)
        {
            if (handleEvents)
            {
                // Gather mouse position and cell widths
                TableLayoutPanel table  = (TableLayoutPanel)sender;
                Point            origin = table.Location;
                Point            mouse  = PointToClient(Cursor.Position);

                int cellWidth  = table.Width / 3;
                int cellHeight = table.Height / 3;

                mouse.X -= origin.X;
                mouse.Y -= origin.Y;


                // Determine active cell using integer division
                int currentRow    = mouse.Y / cellHeight;
                int currentColumn = mouse.X / cellWidth;
                Debug.WriteLine(currentRow + ",  " + currentColumn);

                // Get the panel and attempt a move
                // TODO
                // Get the panel and attempt a move
                Panel selection = b.GetPanel(currentRow, currentColumn);

                selection.TrySelect(b);
                String cell = currentRow + ", " + currentColumn;
                switch (cell)
                {
                case "0, 0":
                    l0_0.Text = selection.GetSelection().ToString();
                    break;

                case "0, 1":
                    l0_1.Text = selection.GetSelection().ToString();
                    break;

                case "0, 2":
                    l0_2.Text = selection.GetSelection().ToString();
                    break;

                case "1, 0":
                    l1_0.Text = selection.GetSelection().ToString();
                    break;

                case "1, 1":
                    l1_1.Text = selection.GetSelection().ToString();
                    break;

                case "1, 2":
                    l1_2.Text = selection.GetSelection().ToString();
                    break;

                case "2, 0":
                    l2_0.Text = selection.GetSelection().ToString();
                    break;

                case "2, 1":
                    l2_1.Text = selection.GetSelection().ToString();
                    break;

                case "2, 2":
                    l2_2.Text = selection.GetSelection().ToString();
                    break;
                }

                // Update textbox
                Board.Turn currentTurn = b.GetTurn();
                if (currentTurn == 0)
                {
                    game_status_text.Text = "Player 1's Turn (X)";
                }
                else
                {
                    game_status_text.Text = "Player 2's Turn (O)";
                }

                // Check for any potential wins or ties
                String winner  = "";
                int[]  pattern = new int[6];
                bool   win     = b.IsWinningMove(ref pattern, ref winner);
                bool   tie     = b.IsTie();

                if (win)
                {
                    GameWon(pattern, winner);
                    handleEvents = false;
                }
                else if (tie)
                {
                    tictactoe_board.Enabled = false;
                    handleEvents            = false;
                    game_status_text.Text   = "Tie Game";
                    b.Tie();
                    ties.Text = "Ties: " + b.GetTieCount().ToString();
                }
            }
        }
        public override void Update(GameTime gameTime)
        {
            _start.Volume = ((float)SettingParameters.SoundVolume) / 100f;
            _clickError.Volume = ((float)SettingParameters.SoundVolume) / 100f;

            /*
            if (_board.IsFinish())
            {
                Media.Play(_backgroundSong);
            }
            */

            if (!_isVictory) Media.Play(_backgroundSong);

            if (_back.IsHit())
            {
                _backHover = false;
                _start.Play();
                MediaPlayer.Stop();
                NextScene = "GameStart";
            }
            else if (_back.IsHover() && !_backHover)
            {
                _backHover = true;
                _back.ClearAllAndAddState(0.2f,
                    new DrawState(Game, new Vector4(0.3f, 0.75f, 0.4f, 0.2f), Color.PowderBlue));
            }
            else if (!_back.IsHover() && _backHover)
            {
                _backHover = false;
                _back.ClearAllAndAddState(0.2f,
                    new DrawState(Game, new Vector4(0.3f, 0.75f, 0.4f, 0.2f), Color.White));
            }

            if (_board.NowState == Board.BoardState.WaitIO)
            {
                _stateView.Content = "玩家回合中...";
            }
            else if (_board.NowState == Board.BoardState.Animation)
            {
                _stateView.Content = "移動中...";
            }
            else
            {
                _stateView.Content = "電腦思考中...";
            }

            if (_turn != _board.NowTurn)
            {
                _turn = _board.NowTurn;
                if (_turn == Board.Turn.TortoiseTurn)
                {
                    _rightArrow.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.3f, 0.4f, 0.2f), Color.White));
                }
                else
                {
                    _rightArrow.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.5f, 0.4f, 0.2f), Color.White));
                }
            }
            if (_board.TortoiseVictory())
            {
                Media.Play(_victory);
                _isVictory = true;
                _alertMessage.Content = "烏龜獲勝!";
                if (!_isComputed)
                {
                    ++SettingParameters.TortoiseScore;
                    SettingParameters.UpdateTile();
                    _isComputed = true;
                }
                _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), Color.Red));
                _tortoiseWin.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0f, 0f, 1f, 1f), Color.PowderBlue));
            }
            else if (_board.HareVictory())
            {
                Media.Play(_victory);
                _isVictory = true;
                _alertMessage.Content = "兔子獲勝!";
                if (!_isComputed)
                {
                    ++SettingParameters.HareScore;
                    SettingParameters.UpdateTile();
                    _isComputed = true;
                }
                _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), Color.Red));
                _hareWin.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0f, 0f, 1f, 1f), Color.PowderBlue));
            }
            else if (_nomove != _board.NoMove)
            {
                _nomove = _board.NoMove;
                if (_nomove)
                {
                    _clickError.Play();
                    _alertMessage.Content = "不能移動狀況產生!";
                    _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), Color.Red));
                }
                else
                {
                    _alertMessage.Content = "不能移動狀況產生!";
                    _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), new Color(0.0f, 0.0f, 0.0f, 0.0f)));
                }
            }
            base.Update(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            if (_board.IsFinish())
            {
                if (MediaPlayer.State == MediaState.Stopped)
                {
                    MediaPlayer.Play(_backgroundSong);
                }
            }

            if (_back.IsHit())
            {
                _backHover = false;
                _start.Play();
                MediaPlayer.Stop();
                NextScene = "Setting";
            }
            else if (_back.IsHover() && !_backHover)
            {
                _backHover = true;
                _back.ClearAllAndAddState(0.2f,
                    new DrawState(Game, new Vector4(0.3f, 0.75f, 0.4f, 0.2f), Color.PowderBlue));
            }
            else if (!_back.IsHover() && _backHover)
            {
                _backHover = false;
                _back.ClearAllAndAddState(0.2f,
                    new DrawState(Game, new Vector4(0.3f, 0.75f, 0.4f, 0.2f), Color.White));
            }

            if (_board.NowState == Board.BoardState.WaitIO)
            {
                _stateView.Content = "玩家回合中...";
            }
            else if (_board.NowState == Board.BoardState.Animation)
            {
                _stateView.Content = "移動中...";
            }
            else
            {
                _stateView.Content = "電腦思考中...";
            }

            if (_turn != _board.NowTurn)
            {
                _turn = _board.NowTurn;
                if (_turn == Board.Turn.TortoiseTurn)
                {
                    _rightArrow.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.3f, 0.4f, 0.2f), Color.White));
                }
                else
                {
                    _rightArrow.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.5f, 0.4f, 0.2f), Color.White));
                }
            }
            if (_board.TortoiseVictory())
            {
                if (MediaPlayer.State == MediaState.Playing && MediaPlayer.Queue.ActiveSong == _backgroundSong)
                {
                    MediaPlayer.Stop();
                    MediaPlayer.Play(_victory);
                }
                _alertMessage.Content = "烏龜獲勝!";
                _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), Color.Red));
                _tortoiseWin.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0f, 0f, 1f, 1f), Color.PowderBlue));
            }
            else if (_board.HareVictory())
            {
                if (MediaPlayer.State == MediaState.Playing && MediaPlayer.Queue.ActiveSong == _backgroundSong)
                {
                    MediaPlayer.Stop();
                    MediaPlayer.Play(_victory);
                }
                _alertMessage.Content = "兔子獲勝!";
                _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), Color.Red));
                _hareWin.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0f, 0f, 1f, 1f), Color.PowderBlue));
            }
            else if (_nomove != _board.NoMove)
            {
                _nomove = _board.NoMove;
                if (_nomove)
                {
                    _clickError.Play();
                    _alertMessage.Content = "不能移動狀況產生!";
                    _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), Color.Red));
                }
                else
                {
                    _alertMessage.Content = "不能移動狀況產生!";
                    _alertMessage.ClearAllAndAddState(0.2f, new DrawState(Game, new Vector4(0.1f, 0.25f, 0.8f, 0.0f), new Color(0.0f, 0.0f, 0.0f, 0.0f)));
                }
            }
            base.Update(gameTime);
        }