예제 #1
0
        private void button_Click(object sender,
                                  System.EventArgs e)//handles when a button is clicked on the board
        {
            int i = -1;
            int j = -1;
            int z = 0;

            //handles finding the current button location numbers
            Button button = (Button)sender;

            while (i < 7)          //finding which button sent to know which piece it corresponds to on the board
            {
                i++;
                j = 0;
                while (j < 8)
                {
                    if (button.GetHashCode() == boardButtons[i, j].GetHashCode())                 //breaks out of the loop once the button is found
                    {
                        break;
                    }
                    j++;
                }
                if (j == 8)              //to save from out of bounds issues while searching
                {
                    j = 7;
                }
                if (button.GetHashCode() == boardButtons[i, j].GetHashCode())             //breaks out of the loop once the button is found
                {
                    break;
                }
            }


            //handles the movement of pieces on the board
            if (game.getBoard()[i, j] != null)               //a piece is selected on the board and not a blank space
            {
                ChessPiece curPiece = game.getBoard()[i, j]; //the piece that was clicked
                z = 0;
                if (moves == null || selectedPiece == null)  //handles a weird case where selectedPiece somehow ends up null
                //and moves isn't null and handles first piece clicked for the persons turn
                {
                    moves = null;                                //sets to null in weird case that moves isn't null and selectedPiece is null
                    moves = curPiece.moves(game.getBoard(), colorSel);
                    while (z < moves.Length && moves[z] != null) //once the possible moves are found set those locations to be clickable
                    {
                        if (game.getBoard()[moves[z][0], moves[z][1]] == null)
                        {
                            if (((King)game.getBoard()[game.findKing(curPiece.getPColor())[0], game.findKing(curPiece.getPColor())[1]]).isMoveSafe(colorSel, game.getPieces(), curPiece.getID(), moves[z]))
                            {
                                boardButtons[moves[z][0], moves[z][1]].Enabled = true;
                            }
                            selectedPiece = curPiece;
                            z++;
                        }
                        else if (game.getBoard()[moves[z][0], moves[z][1]].getPColor() != curPiece.getPColor())
                        {
                            if (((King)game.getBoard()[game.findKing(curPiece.getPColor())[0], game.findKing(curPiece.getPColor())[1]]).isMoveSafe(colorSel, game.getPieces(), curPiece.getID(), moves[z]))
                            {
                                boardButtons[moves[z][0], moves[z][1]].Enabled = true;
                            }
                            selectedPiece = curPiece;
                            z++;
                        }
                        else                         //null out any moves that are of the same color of the piece being moved, those are in the moves list for the isCheck function
                        {
                            moves[z] = null;
                            z++;
                        }
                    }
                }
                else if (selectedPiece.getPColor() == curPiece.getPColor()) //handles if the next click is another one of the same persons turn
                {
                    while (z < moves.Length)                                //set old moves list to unclickable
                    {
                        if (moves[z] != null)
                        {
                            boardButtons[moves[z][0], moves[z][1]].Enabled = false;
                        }
                        z++;
                    }
                    moves = null;
                    z     = 0;
                    moves = curPiece.moves(game.getBoard(), colorSel);
                    while (z < moves.Length && moves[z] != null)              //once the possible moves are found set those locations to be clickable
                    {
                        if (game.getBoard()[moves[z][0], moves[z][1]] == null)
                        {
                            if (((King)game.getBoard()[game.findKing(curPiece.getPColor())[0], game.findKing(curPiece.getPColor())[1]]).isMoveSafe(colorSel, game.getPieces(), curPiece.getID(), moves[z]))
                            {
                                boardButtons[moves[z][0], moves[z][1]].Enabled = true;
                            }
                            selectedPiece = curPiece;
                            z++;
                        }
                        else if (game.getBoard()[moves[z][0], moves[z][1]].getPColor() != curPiece.getPColor())                      //only allow movement to locations that aren't your own pieces
                        {
                            if (((King)game.getBoard()[game.findKing(curPiece.getPColor())[0], game.findKing(curPiece.getPColor())[1]]).isMoveSafe(colorSel, game.getPieces(), curPiece.getID(), moves[z]))
                            {
                                boardButtons[moves[z][0], moves[z][1]].Enabled = true;
                            }
                            selectedPiece = curPiece;
                            z++;
                        }
                        else                         //null out any moves that are of the same color of the piece being moved, those are in the moves list for the isCheck function
                        {
                            moves[z] = null;
                            z++;
                        }
                    }
                }
                else                 //enemy piece was selected after you selected a piece to move
                {
                    z = 0;
                    while (z < moves.Length)                  //set old moves list to unclickable since your piece is moving
                    {
                        if (moves[z] != null)
                        {
                            boardButtons[moves[z][0], moves[z][1]].Enabled = false;
                        }
                        z++;
                    }
                    //set new location of selected piece to correct display
                    if (selectedPiece.getType() == 1)
                    {
                        boardButtons[i, j].Text = "Pa";
                        if (selectedPiece.getPColor() == 0)
                        {
                            boardButtons[i, j].ForeColor = Color.White;
                        }
                        else
                        {
                            boardButtons[i, j].ForeColor = Color.Black;
                        }
                        boardButtons[i, j].Enabled = true;
                        if (j == 0 || j == 7)                  //set to new piece type since you reached the end of the board with a pawn
                        {
                            int k         = selectedPiece.getID();
                            int m         = selectedPiece.getLocation()[0];
                            int n         = selectedPiece.getLocation()[1];
                            int selection = pieceSelect();                            //select new piece
                            ((Pawn)selectedPiece).changePieceType(selection, game.getPieces());
                            game.getBoard()[m, n] = game.getPieces()[k];
                            selectedPiece         = game.getPieces()[k];
                            selectedPiece.setLocation(m, n);
                        }
                    }
                    if (selectedPiece.getType() == 2)
                    {
                        boardButtons[i, j].Text = "Kn";
                        if (selectedPiece.getPColor() == 0)
                        {
                            boardButtons[i, j].ForeColor = Color.White;
                        }
                        else
                        {
                            boardButtons[i, j].ForeColor = Color.Black;
                        }
                        boardButtons[i, j].Enabled = true;
                    }
                    if (selectedPiece.getType() == 3)
                    {
                        boardButtons[i, j].Text = "Bi";
                        if (selectedPiece.getPColor() == 0)
                        {
                            boardButtons[i, j].ForeColor = Color.White;
                        }
                        else
                        {
                            boardButtons[i, j].ForeColor = Color.Black;
                        }
                        boardButtons[i, j].Enabled = true;
                    }
                    if (selectedPiece.getType() == 4)
                    {
                        boardButtons[i, j].Text = "Ro";
                        if (selectedPiece.getPColor() == 0)
                        {
                            boardButtons[i, j].ForeColor = Color.White;
                        }
                        else
                        {
                            boardButtons[i, j].ForeColor = Color.Black;
                        }
                        boardButtons[i, j].Enabled = true;
                    }
                    if (selectedPiece.getType() == 5)
                    {
                        boardButtons[i, j].Text = "Qu";
                        if (selectedPiece.getPColor() == 0)
                        {
                            boardButtons[i, j].ForeColor = Color.White;
                        }
                        else
                        {
                            boardButtons[i, j].ForeColor = Color.Black;
                        }
                        boardButtons[i, j].Enabled = true;
                    }
                    if (selectedPiece.getType() == 6)
                    {
                        boardButtons[i, j].Text = "Ki";
                        if (selectedPiece.getPColor() == 0)
                        {
                            boardButtons[i, j].ForeColor = Color.White;
                        }
                        else
                        {
                            boardButtons[i, j].ForeColor = Color.Black;
                        }
                        boardButtons[i, j].Enabled = true;
                    }

                    //set all necessary things to unclickable and null and the piece to its new location
                    boardButtons[selectedPiece.getLocation()[0], selectedPiece.getLocation()[1]].Enabled = false;
                    boardButtons[selectedPiece.getLocation()[0], selectedPiece.getLocation()[1]].Text    = "";
                    moves = null;
                    game.getBoard()[i, j] = selectedPiece;
                    game.getBoard()[selectedPiece.getLocation()[0], selectedPiece.getLocation()[1]] = null;
                    selectedPiece.setLocation(i, j);
                    selectedPiece = null;
                    for (int r = 0; r < game.getPieces().Length; r++)
                    {
                        if (game.getPieces()[r].getLocation()[0] == -1)                      //this piece isn't on the board don't bother checking it

                        {
                        }
                        else if (game.getBoard()[game.getPieces()[r].getLocation()[0], game.getPieces()[r].getLocation()[1]] == null)
                        {
                            game.getPieces()[r].setLocation(-1, -1);                                                                                                 //the piece is no longer on the board, set its location to off the board
                        }
                        else if (game.getBoard()[game.getPieces()[r].getLocation()[0], game.getPieces()[r].getLocation()[1]].getID() != game.getPieces()[r].getID()) //check if the piece is no longer on the board
                        {
                            game.getPieces()[r].setLocation(-1, -1);                                                                                                 //the piece is no longer on the board, set its location to off the board
                        }
                        //this is for the save and load features
                    }
                    changeTurn();
                }
            }
            else             //a blank space is selected on the board 'only occurs if there is a piece currently selected for movement'
            {
                z = 0;
                while (z < moves.Length && moves[z] != null)          //set old moves list to unclickable since your piece is moving
                {
                    boardButtons[moves[z][0], moves[z][1]].Enabled = false;
                    z++;
                }

                //set new location of selected piece to correct display mostly for color of piece and handles some special case moves
                if (selectedPiece.getType() == 1)
                {
                    if (selectedPiece.getLocation()[0] != i && game.getBoard()[i, j] == null)             //if the pawn is making a diagonal move into an empty space this is a special move taking an enemy pawn off its first turn move
                    //set the enemy pawns location to null and empty
                    {
                        game.getBoard()[i, selectedPiece.getLocation()[1]]   = null;
                        boardButtons[i, selectedPiece.getLocation()[1]].Text = "";
                    }
                    boardButtons[i, j].Text = "Pa";
                    if (selectedPiece.getPColor() == 0)
                    {
                        boardButtons[i, j].ForeColor = Color.White;
                    }
                    else
                    {
                        boardButtons[i, j].ForeColor = Color.Black;
                    }
                    boardButtons[i, j].Enabled = true;
                    if (j == 0 || j == 7)              //set to new piece type since you reached the end of the board with a pawn
                    {
                        int k         = selectedPiece.getID();
                        int m         = selectedPiece.getLocation()[0];
                        int n         = selectedPiece.getLocation()[1];
                        int selection = pieceSelect();
                        ((Pawn)selectedPiece).changePieceType(selection, game.getPieces());
                        game.getBoard()[m, n] = game.getPieces()[k];
                        selectedPiece         = game.getPieces()[k];
                        selectedPiece.setLocation(m, n);
                    }
                }
                if (selectedPiece.getType() == 2)
                {
                    boardButtons[i, j].Text = "Kn";
                    if (selectedPiece.getPColor() == 0)
                    {
                        boardButtons[i, j].ForeColor = Color.White;
                    }
                    else
                    {
                        boardButtons[i, j].ForeColor = Color.Black;
                    }
                    boardButtons[i, j].Enabled = true;
                }
                if (selectedPiece.getType() == 3)
                {
                    boardButtons[i, j].Text = "Bi";
                    if (selectedPiece.getPColor() == 0)
                    {
                        boardButtons[i, j].ForeColor = Color.White;
                    }
                    else
                    {
                        boardButtons[i, j].ForeColor = Color.Black;
                    }
                    boardButtons[i, j].Enabled = true;
                }
                if (selectedPiece.getType() == 4)
                {
                    boardButtons[i, j].Text = "Ro";
                    if (selectedPiece.getPColor() == 0)
                    {
                        boardButtons[i, j].ForeColor = Color.White;
                    }
                    else
                    {
                        boardButtons[i, j].ForeColor = Color.Black;
                    }
                    boardButtons[i, j].Enabled = true;
                }
                if (selectedPiece.getType() == 5)
                {
                    boardButtons[i, j].Text = "Qu";
                    if (selectedPiece.getPColor() == 0)
                    {
                        boardButtons[i, j].ForeColor = Color.White;
                    }
                    else
                    {
                        boardButtons[i, j].ForeColor = Color.Black;
                    }
                    boardButtons[i, j].Enabled = true;
                }
                if (selectedPiece.getType() == 6)
                {
                    boardButtons[i, j].Text = "Ki";
                    if (i - selectedPiece.getLocation()[0] > 1)                //castle move
                    //sets the new location for the rook
                    {
                        boardButtons[i - 1, j].Text    = "Ro";
                        boardButtons[i - 1, j].Enabled = true;
                        game.getBoard()[7, j].setLocation(i - 1, j);
                        game.getBoard()[i - 1, j]  = game.getBoard()[7, j];
                        game.getBoard()[7, j]      = null;
                        boardButtons[7, j].Text    = "";
                        boardButtons[7, j].Enabled = false;
                    }
                    if (selectedPiece.getLocation()[0] - i > 1)                //castle move
                    //sets the new location for the rook
                    {
                        boardButtons[i + 1, j].Text    = "Ro";
                        boardButtons[i + 1, j].Enabled = true;
                        game.getBoard()[0, j].setLocation(i + 1, j);
                        game.getBoard()[i + 1, j]  = game.getBoard()[0, j];
                        game.getBoard()[0, j]      = null;
                        boardButtons[0, j].Text    = "";
                        boardButtons[0, j].Enabled = false;
                    }
                    if (selectedPiece.getPColor() == 0)
                    {
                        boardButtons[i, j].ForeColor = Color.White;
                    }
                    else
                    {
                        boardButtons[i, j].ForeColor = Color.Black;
                    }
                    boardButtons[i, j].Enabled = true;
                }

                //set all necessary things to unclickable and null and the piece to its new location
                boardButtons[selectedPiece.getLocation()[0], selectedPiece.getLocation()[1]].Enabled = false;
                boardButtons[selectedPiece.getLocation()[0], selectedPiece.getLocation()[1]].Text    = "";
                moves = null;
                game.getBoard()[i, j] = selectedPiece;
                game.getBoard()[selectedPiece.getLocation()[0], selectedPiece.getLocation()[1]] = null;
                selectedPiece.setLocation(i, j);
                selectedPiece = null;
                for (int r = 0; r < game.getPieces().Length; r++)
                {
                    if (game.getPieces()[r].getLocation()[0] == -1)                  //this piece isn't on the board don't bother checking it

                    {
                    }
                    else if (game.getBoard()[game.getPieces()[r].getLocation()[0], game.getPieces()[r].getLocation()[1]] == null)
                    {
                        game.getPieces()[r].setLocation(-1, -1);                                                                                                 //the piece is no longer on the board, set its location to off the board
                    }
                    else if (game.getBoard()[game.getPieces()[r].getLocation()[0], game.getPieces()[r].getLocation()[1]].getID() != game.getPieces()[r].getID()) //check if the piece is no longer on the board
                    {
                        game.getPieces()[r].setLocation(-1, -1);                                                                                                 //the piece is no longer on the board, set its location to off the board
                    }
                    //this is for the save and load features
                }
                changeTurn();
            }
        }
예제 #2
0
        public void load()         //used to load a previously saved game
        {
            Controls.Clear();
            var projectFolder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            var file          = Path.Combine(projectFolder, @"SaveData\save.txt");

            Char[] loadData = File.ReadAllText(file).ToCharArray();
            int    i        = 0;
            int    j        = 0;

            ChessPiece[] pcs = new ChessPiece[32];
            while (i < loadData.Length)  //starts from the first char in the save file which is the type of the first piece in the list
            //creates new pieces from the data in the save file and adds them to a list of chesspieces to be passed to the board creation
            {
                if (loadData[i] == '1')
                {
                    i++;
                    pcs[j] = new Pawn(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    if (loadData[i] != '0')
                    {
                        ((Pawn)pcs[j]).setFirstMove(loadData[i] - 48);
                        i++;
                    }
                    else
                    {
                        i++;
                    }
                    j++;
                }
                if (loadData[i] == '2')
                {
                    i++;
                    pcs[j] = new Knight(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    j++;
                }
                if (loadData[i] == '3')
                {
                    i++;
                    pcs[j] = new Bishop(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    j++;
                }
                if (loadData[i] == '4')
                {
                    i++;
                    pcs[j] = new Rook(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    if (loadData[i] != '0')
                    {
                        ((Rook)pcs[j]).setFirstMove(loadData[i] - 48);
                        i++;
                    }
                    else
                    {
                        i++;
                    }
                    j++;
                }
                if (loadData[i] == '5')
                {
                    i++;
                    pcs[j] = new Queen(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    j++;
                }
                if (loadData[i] == '6')
                {
                    i++;
                    pcs[j] = new King(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    if (loadData[i] != '0')
                    {
                        ((King)pcs[j]).setFirstMove(loadData[i] - 48);
                        i++;
                    }
                    else
                    {
                        i++;
                    }
                    j++;
                }
                if (loadData[i] == ':')//: means the next char is the color selection
                {
                    i++;
                    colorSel = ((int)loadData[i] - 48);
                    i++;
                    turn = loadData[i] - 48;
                    break;
                }
            }

            game = new Board(colorSel, pcs);
            initializeGame();
        }
예제 #3
0
파일: ChessPiece.cs 프로젝트: muguii/Chess
        protected internal bool IsThereOpponentePiece(Position position)
        {
            ChessPiece p = (ChessPiece)Board.GetPiece(position);

            return(p != null && p.Color != Color);
        }