Beispiel #1
0
 public static List<BoardCoord> AvailableMoves(Piece piece)
 {
     var moves = new List<BoardCoord>();
     if (piece.Type == PieceType.Pawn && piece.Colour == PieceColour.White)
     {
         moves.Add(new BoardCoord() {XRank = piece.BoardTile.XRank, YRank = Helpers.DecrementYRank(piece.BoardTile.YRank,1)});
         moves.Add(new BoardCoord() { XRank = piece.BoardTile.XRank, YRank = Helpers.DecrementYRank(piece.BoardTile.YRank,2)});
     } else
     if (piece.Type == PieceType.Pawn && piece.Colour == PieceColour.Black)
     {
         moves.Add(new BoardCoord() { XRank = piece.BoardTile.XRank, YRank = Helpers.IncrementYRank(piece.BoardTile.YRank, 1) });
         moves.Add(new BoardCoord() { XRank = piece.BoardTile.XRank, YRank = Helpers.IncrementYRank(piece.BoardTile.YRank, 2) });
     }
     if (piece.Type == PieceType.Knight)
     {
         //up left
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, -1, piece.BoardTile.YRank, 2));
         //UP AND LEFFT
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, -2, piece.BoardTile.YRank, 1));
         //down left
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, -2, piece.BoardTile.YRank, -1));
         //DOWN LEFT AGAIN
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, -1, piece.BoardTile.YRank, -2));
         //up right
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, 1, piece.BoardTile.YRank, 2));
         //UP AND RIGHT?lol
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, 2, piece.BoardTile.YRank, 1));
         //down right
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, 1, piece.BoardTile.YRank, -2));
         //DOWN RIGHT AGAIN
         moves.Add(Helpers.MoveXY(piece.BoardTile.XRank, 2, piece.BoardTile.YRank, -1));
     }
     return moves;
 }
Beispiel #2
0
 public void setPiece(char p, int player)
 {
     if (p == 'p')
         piece = new Pawn(player);
     else if (p == 'r')
         piece = new Rook(player);
     else if (p == 'n')
         piece = new Knight(player);
     else if (p == 'b')
         piece = new Bishop(player);
     else if (p == 'q')
         piece = new Queen(player);
     else if (p == 'k')
         piece = new King(player);
 }
Beispiel #3
0
 //Checks for move mouse click
 private void CheckForMouseClick()
 {
     foreach (var piece in _board.Pieces)
     {
         bool pieceClicked = piece.CollisionRect.Contains(currentMouseState.X, currentMouseState.Y);
         if (pieceClicked)
         {
             _currentGrid = new int[8, 8]
                     {
                         {0, 1, 0, 1, 0, 1, 0, 1},
                         {1, 0, 1, 0, 1, 0, 1, 0},
                         {0, 1, 0, 1, 0, 1, 0, 1},
                         {1, 0, 1, 0, 1, 0, 1, 0},
                         {0, 1, 0, 1, 0, 1, 0, 1},
                         {1, 0, 1, 0, 1, 0, 1, 0},
                         {0, 1, 0, 1, 0, 1, 0, 1},
                         {1, 0, 1, 0, 1, 0, 1, 0},
                     };
             _currentGrid[piece.TilePosition.X, piece.TilePosition.Y] = 2;
             PieceSelected = piece;
             //Where can we move
             AvailableMoves = Move.AvailableMoves(PieceSelected);
             foreach (var move in AvailableMoves)
             {
                 var tile = Helpers.BoardCoordToTile(move);
                 //colour
                 _currentGrid[tile.X, tile.Y] = 3;
             }
             _map.Generate(_currentGrid, 132);
         }
     }
 }
Beispiel #4
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)
        {
            // The active state from the last frame is now old
            lastMouseState = currentMouseState;

            // Get the mouse state relevant for this frame
            currentMouseState = Mouse.GetState();

            // Recognize a single click of the left mouse button
            if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
            {
                if (PieceSelected != null)
                {
                    var tileReference = Helpers.PixelToTile(currentMouseState.X, currentMouseState.Y);
                    // helper.inbounds
                    if (tileReference.X >= 0 && tileReference.X <= 7 && tileReference.Y >= 0 && tileReference.Y <= 7)
                    {
                        if (_currentGrid[(int)tileReference.Y, (int)tileReference.X] == 3)
                        {
                            PieceSelected.Move(tileReference);
                        }
                    }
                    PieceSelected = null;
                    _currentGrid = new int[8, 8]
                            {
                                {0, 1, 0, 1, 0, 1, 0, 1},
                                {1, 0, 1, 0, 1, 0, 1, 0},
                                {0, 1, 0, 1, 0, 1, 0, 1},
                                {1, 0, 1, 0, 1, 0, 1, 0},
                                {0, 1, 0, 1, 0, 1, 0, 1},
                                {1, 0, 1, 0, 1, 0, 1, 0},
                                {0, 1, 0, 1, 0, 1, 0, 1},
                                {1, 0, 1, 0, 1, 0, 1, 0},
                            };
                    _map.Generate(_currentGrid, 132);
                }
                else
                {
                    CheckForMouseClick();
                }
            }
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }
Beispiel #5
0
 //execute legal move
 private void SetHeldPiece(int file, int rank)
 {
     if (heldPiece != null)
     {
         Console.WriteLine(DisplayNotation(file, rank));
         undoDone = false;
         currFile = file;
         currRank = rank;
         lastFile = heldPieceFile;
         lastRank = heldPieceRank;
         lastEatenPiece = square[file, rank].Piece;
         square[file, rank].Piece = heldPiece;
         if (heldPiece is King)
         {
             if (CastlingRights[currentPlayer - 1])
             {
                 {
                     if (file == 2) //queenside
                     {
                         square[3, rank].Piece = square[0, rank].Piece;
                         square[0, rank].Piece = null;
                     }
                     if (file == 6) //kingside
                     {
                         square[5, rank].Piece = square[7, rank].Piece;
                         square[7, rank].Piece = null;
                     }
                 }
             }
             else CastlingRights[currentPlayer - 1] = false;
         }
         ClearHeldPiece();
         PromotePawns();
         square[file, rank].Piece.moved++;
         if (lastEatenPiece is King)
         {
             Console.Beep(600, 500);
             Console.WriteLine(GetCurrentPlayer() + " wins!");
             gameOver = true;
         }
         SwapCurrentPlayer();
         if (CheckState())
             UndoLastMove();
     }
     else
     {
         Console.WriteLine("there is no held piece to set");
     }
 }
Beispiel #6
0
 //highlight squares the heldPiece can move to
 private void DrawPossibleMoves(int file, int rank, Piece p)
 {
     GL.Color4(Color.Green);
     var possibleMoves = p.getPossibleMoves(this, Square[file, rank]);
     foreach (Square i in possibleMoves)
     {
         GL.Begin(BeginMode.Quads);
         DrawQuad(bottomLeft + new Vector2(scale.X * i.file, scale.Y * i.rank), scale/8);
         GL.End();
     }
 }
Beispiel #7
0
        //draw pieces
        private void DrawPiece(int file, int rank, Piece piece)
        {
            if (piece.getPlayer() == 1)
                GL.Color4(Color.White);
            else
                GL.Color4(Color.Black);

            if (piece is Pawn)
                Pawn.Draw(bottomLeft + new Vector2(scale.X * file, scale.Y * rank), scale);
            else if (piece is Bishop)
                Bishop.Draw(bottomLeft + new Vector2(scale.X * file, scale.Y * rank), scale);
            else if (piece is King)
                King.Draw(bottomLeft + new Vector2(scale.X * file, scale.Y * rank), scale);
            else if (piece is Rook)
                Rook.Draw(bottomLeft + new Vector2(scale.X * file, scale.Y * rank), scale);
            else if (piece is Knight)
                Knight.Draw(bottomLeft + new Vector2(scale.X * file, scale.Y * rank), scale);
            else if (piece is Queen)
                Queen.Draw(bottomLeft + new Vector2(scale.X * file, scale.Y * rank), scale);
        }
Beispiel #8
0
        //(re)set the board
        public void SetBoard()
        {
            currentPlayer = WHITE;

            ResetTimes();
            heldPiece = null;

            //reset player times
            player1Time = DEFAULT_TIME_LIMIT;
            player2Time = DEFAULT_TIME_LIMIT;

            CastlingRights[0] = true;
            CastlingRights[1] = true;

            //set all squares to null
            for (int i = 2; i < square.GetLength(0); i++)
                for (int j = 2; j < 6; j++)
                  square[i,j].Piece = null;

            for (int i = 0; i < 8; i++)
            {
                square[i, 1].Piece = new Pawn(WHITE);
                square[i, 6].Piece = new Pawn(BLACK);
            }
            //white pieces
            square[0, 0].Piece = new Rook(WHITE);
            square[1, 0].Piece = new Knight(WHITE);
            square[2, 0].Piece = new Bishop(WHITE);
            square[3, 0].Piece = new Queen(WHITE);
            square[4, 0].Piece = new King(WHITE);
            square[5, 0].Piece = new Bishop(WHITE);
            square[6, 0].Piece = new Knight(WHITE);
            square[7, 0].Piece = new Rook(WHITE);
            //black square
            square[0, 7].Piece = new Rook(BLACK);
            square[1, 7].Piece = new Knight(BLACK);
            square[2, 7].Piece = new Bishop(BLACK);
            square[3, 7].Piece = new Queen(BLACK);
            square[4, 7].Piece = new King(BLACK);
            square[5, 7].Piece = new Bishop(BLACK);
            square[6, 7].Piece = new Knight(BLACK);
            square[7, 7].Piece = new Rook(BLACK);
        }
Beispiel #9
0
 //pick up a piece to heldPiece from the board. doesn't need to be a bool, can be void?
 public bool PickupPiece(int file, int rank)
 {
     if (!square[file, rank].isEmpty()) //&& owner = currentPlayer
     {
         Piece piece = square[file, rank].Piece;
         if (piece.getPlayer() == currentPlayer)
         {
             heldPieceFile = file;
             heldPieceRank = rank;
             heldPiece = piece;
             square[file, rank].Piece = null;
             return true;
         }
         else
         {
             Console.Beep(600, 200);
             return false;
         }
     }
     else return false;
 }
Beispiel #10
0
 //set heldPiece back to null
 public void ClearHeldPiece()
 {
     heldPiece = null;
 }