Exemple #1
0
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            MouseState ms    = Mouse.GetState();
            Boolean    moved = false;

            if (ms.LeftButton == ButtonState.Pressed)
            {
                if (startGameClick == false)
                {
                    startGameClick = true;
                    board.startGame();
                    return;
                }
                if ((int)(ms.X) > 480)
                {
                    board.startNewGame();
                    legalMoves = null;
                    return;
                }
                if (!firstClick)
                {
                    int newxPos = (int)(ms.X / 60);
                    int newyPos = (int)(ms.Y / 60);

                    toPos = new Coord(newxPos, newyPos);

                    // Here we try to make a move
                    board.tryMove(fromPos, toPos);

                    legalMoves = null;

                    currentIndex [0] = 99;
                    currentIndex [1] = 99;
                    firstClick       = true;
                    moved            = true;
                }
                else
                {
                    int xPos = (int)(ms.X / 60);
                    int yPos = (int)(ms.Y / 60);
                    if (board.board [xPos, yPos].color == board.turn)
                    {
                        fromPos = new Coord(xPos, yPos);
                        if (moved == false)
                        {
                            if (currentIndex [0] == xPos && currentIndex [1] == yPos)
                            {
                                currentIndex [0] = 99;
                                currentIndex [1] = 99;
                            }
                            else
                            {
                                currentIndex [0] = (int)(ms.X / 60);
                                currentIndex [1] = (int)(ms.Y / 60);
                            }
                        }
                        firstClick = false;
                        legalMoves = Rules.getLegalMoves(board.board, fromPos);
                    }
                    else
                    {
                        Console.WriteLine("NOT CORRECT TURN");
                    }
                }
            }
        }
Exemple #2
0
        // Ask the rules if a move between two positions is legal, calls swap() if ok
        public void tryMove(Coord fromPos, Coord toPos)
        {
            Coord kingCoord = new Coord();

            if (Rules.isLegalMove(board, fromPos, toPos))
            {
                board[fromPos.xpos, fromPos.ypos].moved = true;

                Board tempBoard = new Board(this);

                Piece temp = tempBoard.board [fromPos.xpos, fromPos.ypos];

                temp.coord = toPos;
                if (tempBoard.board [toPos.xpos, toPos.ypos].type != Piece.PieceType.NONE)
                {
                    tempBoard.board [toPos.xpos, toPos.ypos] = new Piece(Piece.PieceType.NONE, toPos.xpos, toPos.ypos, Piece.PieceColor.NONE, 99);
                }

                tempBoard.board [fromPos.xpos, fromPos.ypos] = tempBoard.board [toPos.xpos, toPos.ypos];
                tempBoard.board [toPos.xpos, toPos.ypos]     = temp;

                foreach (Piece p in tempBoard.board)
                {
                    if (p.type == Piece.PieceType.KING && p.color == turn)
                    {
                        kingCoord = new Coord(p.coord.xpos, p.coord.ypos);
                    }
                }

                Piece.PieceColor colorToCheck;
                if (turn == Piece.PieceColor.WHITE)
                {
                    colorToCheck = Piece.PieceColor.BLACK;
                }
                else
                {
                    colorToCheck = Piece.PieceColor.WHITE;
                }

                if (!(Rules.isCheck(tempBoard.board, colorToCheck, kingCoord)))
                {
                    swap(fromPos, toPos);

                    //Investigate if move caused a Check.

                    foreach (Piece p in board)
                    {
                        if (p.type == Piece.PieceType.KING && p.color != turn)
                        {
                            kingCoord = new Coord(p.coord.xpos, p.coord.ypos);
                        }
                    }

                    if (Rules.isCheck(board, turn, kingCoord))
                    {
                        if (turn == Piece.PieceColor.BLACK)
                        {
                            Console.WriteLine("White is in check");
                            whiteIsCheck = true;
                        }
                        else
                        {
                            Console.WriteLine("Black is in check");
                            blackIsCheck = true;
                        }
                    }
                    else
                    {
                        whiteIsCheck = false;
                        blackIsCheck = false;
                    }

                    if (turn == Piece.PieceColor.WHITE)
                    {
                        turn = Piece.PieceColor.BLACK;
                        player2.nextMove();
                    }
                    else
                    {
                        turn = Piece.PieceColor.WHITE;
                        player1.nextMove();
                    }
                }
                else
                {
                    Console.WriteLine("Cant move because king will be checked");
                }

                if (whiteIsCheck)
                {
                    Console.WriteLine("check if white is checkmate");
                    if (Rules.isCheckMate(this, Piece.PieceColor.WHITE))
                    {
                        Console.WriteLine("GAME IS OVER, BLACK WON");
                    }
                }
                if (blackIsCheck)
                {
                    Console.WriteLine("check if black is checkmate");
                    if (Rules.isCheckMate(this, Piece.PieceColor.BLACK))
                    {
                        Console.WriteLine("GAME IS OVER, WHITE WON");
                    }
                }

                saveBoardToFile();
            }
        }