Beispiel #1
0
        public static void TileToIndexes(ChessboardTile tile, out int row, out int column)
        {
            if (tile == ChessboardTile.None)
            {
                throw new ArgumentException("The tile cannot be None");
            }

            row    = BoardSize - ((int)tile / BoardSize) - 1;
            column = (int)tile % BoardSize;
        }
Beispiel #2
0
        public IMatchState Update(GameTime gameTime)
        {
            //Check for camera movement and exit
            if (m_owner.InputManager.IsKeyDown(Keys.Escape))
            {
                m_owner.ExitMatch();
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Left))
            {
                m_owner.GraphicsManager.CameraLeft(gameTime);
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Right))
            {
                m_owner.GraphicsManager.CameraRight(gameTime);
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Up))
            {
                m_owner.GraphicsManager.CameraUp(gameTime);
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Down))
            {
                m_owner.GraphicsManager.CameraDown(gameTime);
            }

            //Check for a click
            if (m_owner.InputManager.IsLeftMouseButtonDown())
            {
                Vector2 mousePosition = m_owner.InputManager.GetMousePosition();
                if (m_owner.GraphicsManager.IsMouseIn(mousePosition))
                {
                    ChessboardTile pickedTile = ColorChessboardPicker.GetTile(mousePosition,
                                                                              m_owner.GraphicsManager,
                                                                              m_owner.ChessboardModel,
                                                                              m_owner.ChessboardPickingEffect);

                    if (pickedTile == ChessboardTile.None)
                    {
                        return(this);
                    }

                    if (m_owner.Match.TrySelect(ChessPlayer.White, pickedTile))
                    {
                        m_owner.AudioManager.PlaySound(SoundKey.PieceUp);
                        return(WhitePieceMoving.GetMatchState(m_owner));
                    }
                }
            }

            //If not returned yet return yourself
            return(this);
        }
Beispiel #3
0
        static ColorChessboardPicker()
        {
            //Initialize the map
            for (int i = 0; i < 8; ++i)
            {
                for (int j = 0; j < 8; ++j)
                {
                    Color          color = new Color((byte)(i * 32), (byte)(j * 32), 128);
                    ChessboardTile tile  = (ChessboardTile)((i * 8) + j);

                    s_colorMap.Add(color);
                    s_tileMap.Add(tile);
                }
            }
        }
Beispiel #4
0
 public ChessPiece this[ChessboardTile tile]
 {
     get
     {
         int row, column;
         ChessboardTileHelper.TileToIndexes(tile, out row, out column);
         return(this[row, column]);
     }
     set
     {
         int row, column;
         ChessboardTileHelper.TileToIndexes(tile, out row, out column);
         this[row, column] = value;
     }
 }
Beispiel #5
0
        public bool TrySelect(ChessPlayer player, ChessboardTile tile)
        {
            int row, column;

            ChessboardTileHelper.TileToIndexes(tile, out row, out column);

            if (m_board[row, column] != null && m_board[row, column].Color == player)
            {
                m_board[row, column].Selected = true;
                m_selectedRow    = row;
                m_selectedColumn = column;
                return(true);
            }

            return(false);
        }
Beispiel #6
0
        public bool IsUndo(ChessboardTile tile)
        {
            int row, column;

            ChessboardTileHelper.TileToIndexes(tile, out row, out column);

            if (row == m_selectedRow && column == m_selectedColumn)
            {
                m_board[m_selectedRow, m_selectedColumn].Selected = false;
                m_selectedRow    = -1;
                m_selectedColumn = -1;
                return(true);
            }

            return(false);
        }
Beispiel #7
0
        public static ChessboardTile GetTile(Vector2 mousePosition, IGraphicsManager graphicsManager,
                                             Model chessboardModel, Effect chessboardPickingEffect)
        {
            graphicsManager.GraphicsDevice.Clear(s_neutralColor);

            //Get the needed stuff
            Matrix world = Matrix.Identity;

            //Draw the screen and retrive the map
            graphicsManager.DrawModel(chessboardModel, ref world, chessboardPickingEffect);
            Texture2D pickingTexture = graphicsManager.GetScreenContents();

            Color[] pickingMap = new Color[pickingTexture.Width * pickingTexture.Height];
            pickingTexture.GetData(pickingMap);

            //Get the color and a tile
            Color          selectedColor = pickingMap[(int)(mousePosition.Y * pickingTexture.Width + mousePosition.X)];
            ChessboardTile tile          = GetTileForColor(selectedColor);

            //Return the result
            return(tile);
        }
Beispiel #8
0
        public IMatchState Update(GameTime gameTime)
        {
            //Check for camera movement and exit
            if (m_owner.InputManager.IsKeyDown(Keys.Escape))
            {
                m_owner.ExitMatch();
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Left))
            {
                m_owner.GraphicsManager.CameraLeft(gameTime);
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Right))
            {
                m_owner.GraphicsManager.CameraRight(gameTime);
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Up))
            {
                m_owner.GraphicsManager.CameraUp(gameTime);
            }
            if (m_owner.InputManager.IsKeyHoldDown(Keys.Down))
            {
                m_owner.GraphicsManager.CameraDown(gameTime);
            }

            //Check for a click
            if (m_owner.InputManager.IsLeftMouseButtonDown())
            {
                Vector2 mousePosition = m_owner.InputManager.GetMousePosition();
                if (m_owner.GraphicsManager.IsMouseIn(mousePosition))
                {
                    ChessboardTile pickedTile = ColorChessboardPicker.GetTile(mousePosition,
                                                                              m_owner.GraphicsManager,
                                                                              m_owner.ChessboardModel,
                                                                              m_owner.ChessboardPickingEffect);

                    if (pickedTile == ChessboardTile.None)
                    {
                        return(this);
                    }

                    if (m_owner.Match.IsUndo(pickedTile))
                    {
                        m_owner.AudioManager.PlaySound(SoundKey.PieceDown);
                        return(BlackPiecePicking.GetMatchState(m_owner));
                    }

                    if (m_owner.Match.TryMove(ChessPlayer.Black, pickedTile))
                    {
                        m_owner.AudioManager.PlaySound(SoundKey.PieceDown);

                        ChessPlayer?winningPlayer = m_owner.Match.WinningPlayer;
                        if (winningPlayer != null)
                        {
                            if (winningPlayer == ChessPlayer.White)
                            {
                                return(WhiteWon.GetMatchState(m_owner));
                            }
                            if (winningPlayer == ChessPlayer.Black)
                            {
                                return(BlackWon.GetMatchState(m_owner));
                            }
                        }
                        return(BlackToWhiteTransfer.GetMatchState(m_owner));
                    }

                    m_owner.AudioManager.PlaySound(SoundKey.Error);
                }
            }

            //If not returned yet return yourself
            return(this);
        }
Beispiel #9
0
        public bool TryMove(ChessPlayer player, ChessboardTile tile)
        {
            int row, column;

            ChessboardTileHelper.TileToIndexes(tile, out row, out column);

            //Normal move
            if (m_board[m_selectedRow, m_selectedColumn].CanMove(row, column))
            {
                //Pick up the piece
                ChessPiece movingPiece = m_board[m_selectedRow, m_selectedColumn];
                m_board[m_selectedRow, m_selectedColumn] = null;
                m_selectedRow    = -1;
                m_selectedColumn = -1;

                //Update the piece
                movingPiece.Selected  = false;
                movingPiece.FirstMove = false;

                //Check for winning/losing
                if (row == m_whiteKing.CurrentRow && column == m_whiteKing.CurrentColumn)
                {
                    m_winningPlayer = ChessPlayer.Black;
                }
                if (row == m_blackKing.CurrentRow && column == m_blackKing.CurrentColumn)
                {
                    m_winningPlayer = ChessPlayer.White;
                }

                //Check for rochade (white, kingside)
                if (movingPiece == m_whiteKing && row == 0 && column == 6)
                {
                    WhiteRook rookSpot = m_board[0, 7] as WhiteRook;
                    if (rookSpot != null && rookSpot.FirstMove)
                    {
                        //We can do the rochade
                        m_board[0, 7]      = movingPiece;
                        m_board[0, 6]      = rookSpot;
                        rookSpot.FirstMove = false;
                    }
                }

                //Check for rochade (black, queenside)
                if (movingPiece == m_blackKing && row == 7 && column == 1)
                {
                    BlackRook rookSpot = m_board[7, 0] as BlackRook;
                    if (rookSpot != null && rookSpot.FirstMove)
                    {
                        //We can do the rochade
                        m_board[7, 0]      = movingPiece;
                        m_board[7, 1]      = rookSpot;
                        rookSpot.FirstMove = false;
                    }
                }

                //Check for rochade (black, kingside)
                if (movingPiece == m_blackKing && row == 7 && column == 6)
                {
                    BlackRook rookSpot = m_board[7, 7] as BlackRook;
                    if (rookSpot != null && rookSpot.FirstMove)
                    {
                        //We can do the rochade
                        m_board[7, 7]      = movingPiece;
                        m_board[7, 6]      = rookSpot;
                        rookSpot.FirstMove = false;
                    }
                }

                //Put down the piece
                m_board[row, column] = movingPiece;

                //Clear selection

                return(true);
            }

            //Rochade (white, queenside)
            if (m_board[m_selectedRow, m_selectedColumn] is WhiteKing &&
                m_whiteKing.FirstMove &&
                row == 0 &&
                column == 1 &&
                m_board[0, 1] == null &&
                m_board[0, 2] == null &&
                m_board[0, 3] == null)
            {
                WhiteRook rookSpot = m_board[0, 0] as WhiteRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_whiteKing.Selected = false;

                    //Move
                    m_board[0, 1] = m_whiteKing;
                    m_board[0, 2] = rookSpot;

                    //Mark first moves
                    m_whiteKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            //Rochade (white, kingside)
            if (m_board[m_selectedRow, m_selectedColumn] is WhiteKing &&
                m_whiteKing.FirstMove &&
                row == 0 &&
                column == 6 &&
                m_board[0, 5] == null &&
                m_board[0, 6] == null)
            {
                WhiteRook rookSpot = m_board[0, 7] as WhiteRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_whiteKing.Selected = false;

                    //Move
                    m_board[0, 6] = m_whiteKing;
                    m_board[0, 5] = rookSpot;

                    //Mark first moves
                    m_whiteKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            //Rochade (black, queenside)
            if (m_board[m_selectedRow, m_selectedColumn] is BlackKing &&
                m_blackKing.FirstMove &&
                row == 7 &&
                column == 1 &&
                m_board[7, 1] == null &&
                m_board[7, 2] == null &&
                m_board[7, 3] == null)
            {
                BlackRook rookSpot = m_board[7, 0] as BlackRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_blackKing.Selected = false;

                    //Move
                    m_board[7, 1] = m_blackKing;
                    m_board[7, 2] = rookSpot;

                    //Mark first moves
                    m_blackKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            //Rochade (black, kingside)
            if (m_board[m_selectedRow, m_selectedColumn] is BlackKing &&
                m_blackKing.FirstMove &&
                row == 7 &&
                column == 6 &&
                m_board[7, 5] == null &&
                m_board[7, 6] == null)
            {
                BlackRook rookSpot = m_board[7, 7] as BlackRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_blackKing.Selected = false;

                    //Move
                    m_board[7, 6] = m_blackKing;
                    m_board[7, 5] = rookSpot;

                    //Mark first moves
                    m_blackKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            return(false);
        }