예제 #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            if (boardInfo.WhoseMove == ChessPieceColor.Black && blackIsAI)
            {
                ChessEngine.EngineMove(boardInfo);
            }
            else
            {
                MouseState mouseState = Mouse.GetState();
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    int xPos = (int)(mouseState.X / Constants.SquareSize);
                    int yPos = (int)(mouseState.Y / Constants.SquareSize);

                    if (xPos < Constants.NumberOfFiles && yPos < Constants.NumberOfRanks)
                    {
                        if (selectedIndex > boardInfo.pieces.Length)
                        {
                            byte index = (byte)(yPos * Constants.NumberOfFiles + xPos);
                            if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove)
                            {
                                selectedIndex = index;
                                boardInfo.pieces[index].isSelected = true;
                            }
                        }
                        else
                        {
                            byte index = (byte)(yPos * Constants.NumberOfFiles + xPos);
                            if (index != selectedIndex)
                            {
                                //SourceIndex has no piece
                                if (boardInfo.pieces[selectedIndex] == null)
                                {
                                    return;
                                }

                                //Select new piece if same color
                                if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove)
                                {
                                    boardInfo.pieces[selectedIndex].isSelected = false;
                                    boardInfo.pieces[index].isSelected         = true;
                                    selectedIndex = index;
                                }

                                //Check if this is infact a valid move
                                if (!ChessEngine.IsValidMove(boardInfo, selectedIndex, index))
                                {
                                    return;
                                }

                                ChessEngine.MovePiece(boardInfo, selectedIndex, index);
                                selectedIndex = 99;
                            }
                            else
                            {
                                boardInfo.pieces[selectedIndex].isSelected = false;
                                selectedIndex = 99;
                            }
                        }
                    }
                    else if (selectedIndex < boardInfo.pieces.Length)
                    {
                        boardInfo.pieces[selectedIndex].isSelected = false;
                        selectedIndex = 99;
                    }
                }
            }

            base.Update(gameTime);
        }
예제 #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            if (boardInfo.WhoseMove == ChessPieceColor.Black && blackIsAI)
            {
                ChessEngine.EngineMove(boardInfo);
            }
            else
            {
                TouchCollection touchCollection = TouchPanel.GetState();
                foreach (TouchLocation tl in touchCollection)
                {
                    if (tl.State == TouchLocationState.Released)
                    {
                        int xPos = (int)(tl.Position.X / Constants.SquareSize);
                        int yPos = (int)(tl.Position.Y / Constants.SquareSize);

                        if (xPos < Constants.NumberOfFiles && yPos < Constants.NumberOfRanks)
                        {
                            if (selectedIndex > boardInfo.pieces.Length)
                            {
                                byte index = (byte)(yPos * Constants.NumberOfFiles + xPos);
                                if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove)
                                {
                                    selectedIndex = index;
                                    boardInfo.pieces[index].isSelected = true;
                                    break;
                                }
                            }
                            else
                            {
                                byte index = (byte)(yPos * Constants.NumberOfFiles + xPos);
                                if (index != selectedIndex)
                                {
                                    //SourceIndex has no piece
                                    if (boardInfo.pieces[selectedIndex] == null)
                                    {
                                        return;
                                    }

                                    //Select new piece if same color
                                    if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove)
                                    {
                                        boardInfo.pieces[selectedIndex].isSelected = false;
                                        boardInfo.pieces[index].isSelected         = true;
                                        selectedIndex = index;
                                    }

                                    //Check if this is infact a valid move
                                    if (!ChessEngine.IsValidMove(boardInfo, selectedIndex, index))
                                    {
                                        return;
                                    }

                                    ChessEngine.MovePiece(boardInfo, selectedIndex, index);
                                    selectedIndex = 99;

                                    break;
                                }
                                else
                                {
                                    boardInfo.pieces[selectedIndex].isSelected = false;
                                    selectedIndex = 99;
                                }
                            }
                        }
                        else if (selectedIndex < boardInfo.pieces.Length)
                        {
                            boardInfo.pieces[selectedIndex].isSelected = false;
                            selectedIndex = 99;
                        }
                    }
                }
            }

            base.Update(gameTime);
        }