Example #1
0
        public bool isMoveSafe(int colorSel, ChessPiece[] pcs, int pNum, int[] moveLoc)         //returns whether a move is safe or not
        //if this list is empty it is checkmate
        {
            Board b;

            ChessPiece[] p = new ChessPiece[32];
            for (int i = 0; i < pcs.Length; i++)        //create a set of new pieces to be passed to the new board for checking
            {
                if (pcs[i].getType() == 1)
                {
                    p[i] = new Pawn(i, pcs[i].getPColor());
                    p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]);
                }
                if (pcs[i].getType() == 2)
                {
                    p[i] = new Knight(i, pcs[i].getPColor());
                    p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]);
                }
                if (pcs[i].getType() == 3)
                {
                    p[i] = new Bishop(i, pcs[i].getPColor());
                    p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]);
                }
                if (pcs[i].getType() == 4)
                {
                    p[i] = new Rook(i, pcs[i].getPColor());
                    p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]);
                }
                if (pcs[i].getType() == 5)
                {
                    p[i] = new Queen(i, pcs[i].getPColor());
                    p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]);
                }
                if (pcs[i].getType() == 6)
                {
                    p[i] = new King(i, pcs[i].getPColor());
                    p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]);
                }
                if (p[i].getLocation()[0] == moveLoc[0] && p[i].getLocation()[1] == moveLoc[1] && p[i].getType() != 6)      //no piece can hold the same location
                {
                    p[i].setLocation(-1, -1);
                }
            }
            p[pNum].setLocation(moveLoc[0], moveLoc[1]);
            b = new Board(colorSel, p);                                                                //create a new board with the new move location to check if it puts the king in check
            int[] kingLoc = b.findKing(pcs[pNum].getPColor());                                         //the kings location in the new board
            if (((King)b.getBoard()[kingLoc[0], kingLoc[1]]).isCheck(kingLoc, b.getBoard(), colorSel)) //if the move is check return false
            {
                return(false);
            }
            else             //if the move is safe return true
            {
                return(true);
            }
        }
Example #2
0
        private void initializeGame()         //call to set up the gameboard
        {
            ChessPiece[,] gameBoard = game.getBoard();
            for (int i = 0; i < 8; i++)        //maps the gameboard to buttons for the ui
            {
                for (int j = 0; j < 8; j++)
                {
                    boardButtons[i, j]          = new Button();
                    boardButtons[i, j].Location = new Point(i * 50, j * 50);
                    boardButtons[i, j].Size     = new Size(50, 50);
                    boardButtons[i, j].Paint   += button_Paint;
                    boardButtons[i, j].Enabled  = false;
                    if (gameBoard[i, j] != null)
                    {
                        if (gameBoard[i, j].getPColor() == 0)
                        {
                            if (gameBoard[i, j].getType() == 1)
                            {
                                boardButtons[i, j].Text      = "Pa";
                                boardButtons[i, j].ForeColor = Color.White;
                                if (turn == 0)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 2)
                            {
                                boardButtons[i, j].Text      = "Kn";
                                boardButtons[i, j].ForeColor = Color.White;
                                if (turn == 0)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 3)
                            {
                                boardButtons[i, j].Text      = "Bi";
                                boardButtons[i, j].ForeColor = Color.White;
                                if (turn == 0)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 4)
                            {
                                boardButtons[i, j].Text      = "Ro";
                                boardButtons[i, j].ForeColor = Color.White;
                                if (turn == 0)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 5)
                            {
                                boardButtons[i, j].Text      = "Qu";
                                boardButtons[i, j].ForeColor = Color.White;
                                if (turn == 0)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 6)
                            {
                                boardButtons[i, j].Text      = "Ki";
                                boardButtons[i, j].ForeColor = Color.White;
                                if (turn == 0)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                        }
                        else
                        {
                            if (gameBoard[i, j].getType() == 1)
                            {
                                boardButtons[i, j].Text = "Pa";
                                if (turn == 1)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 2)
                            {
                                boardButtons[i, j].Text = "Kn";
                                if (turn == 1)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 3)
                            {
                                boardButtons[i, j].Text = "Bi";
                                if (turn == 1)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 4)
                            {
                                boardButtons[i, j].Text = "Ro";
                                if (turn == 1)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 5)
                            {
                                boardButtons[i, j].Text = "Qu";
                                if (turn == 1)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                            if (gameBoard[i, j].getType() == 6)
                            {
                                boardButtons[i, j].Text = "Ki";
                                if (turn == 1)
                                {
                                    boardButtons[i, j].Enabled = true;
                                }
                            }
                        }
                    }
                    if ((i + j) % 2 == 0)
                    {
                        boardButtons[i, j].BackColor = Color.Gray;
                    }
                    else
                    {
                        boardButtons[i, j].BackColor = Color.DarkGray;
                    }
                    boardButtons[i, j].Click += button_Click;
                    Controls.Add(boardButtons[i, j]);
                }
            }
            Button saveBtn = new Button();

            saveBtn.Click   += saveButton_Click;
            saveBtn.Text     = "Save Game";
            saveBtn.Location = new Point(350, 420);
            Controls.Add(saveBtn);
            checkGameState();
        }