Beispiel #1
0
        /*
         * Moves the piece p1 to the location of p2, calling the validation
         * methods of p1 to verify that the move is legal. If the move is legal,
         * the move is made and the board is updated. If it is illegal, the
         * move will not be made and GameInfo will be updated
         */
        public void MovePieces(ChessPiece p1, ChessPiece p2, String color)
        {
            List <ChessPiece> CopyBoard = this.CopyBoard();

            if (color != this.color)
            {
                CopyBoard.Reverse();
            }
            int i1 = CopyBoard.IndexOf(p1);
            int i2 = CopyBoard.IndexOf(p2);
            int x1 = i1 % DIMENSION;
            int x2 = i2 % DIMENSION;
            int y1 = (int)Math.Floor((double)(i1 / DIMENSION));
            int y2 = (int)Math.Floor((double)(i2 / DIMENSION));
            int dx = x2 - x1;
            int dy = y2 - y1;

            if (p1.GetName() != "Empty" && p1.GetColor() == color)
            {
                if (p1.IsValidMove(dx, dy, i1, i2, CopyBoard))
                {
                    CopyBoard.RemoveAt(i2);
                    CopyBoard.Insert(i2, p1);
                    CopyBoard.RemoveAt(i1);
                    CopyBoard.Insert(i1, new EmptyPiece());
                    if (color != this.color)
                    {
                        CopyBoard.Reverse();
                    }
                    this.board = CopyBoard;
                    if (p2.GetName() == "King")
                    {
                        EndGame();

                        if (p2.GetColor() == this.color)
                        {
                            this.gameInfo = "You lost!";
                        }
                        else
                        {
                            this.gameInfo = "You won!";
                        }
                    }
                    else
                    {
                        this.gameInfo = p1.GetName() + " to (" + x2 + "," + y2 + ")";
                    }
                    this.recentMove = i1 + "," + i2;
                    this.turn       = false;
                }
                else
                {
                    this.gameInfo = "Invalid move!";
                }
            }
            else
            {
                this.gameInfo = "Invalid color!";
            }
        }
Beispiel #2
0
        /*
         * Updates the GUI game board too reflect
         * what is stored in ChessModel
         */
        private void UpdateBoard()
        {
            List <ChessPiece> board = game.GetBoard();

            for (int i = 0; i < 64; i++)
            {
                ChessPiece piece = board.ElementAt(i);

                if (piece.GetColor() == "Black")
                {
                    if (piece.GetName() == "Bishop")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.bb;
                    }
                    else if (piece.GetName() == "King")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.bk;
                    }
                    else if (piece.GetName() == "Knight")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.bn;
                    }
                    else if (piece.GetName() == "Pawn")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.bp;
                    }
                    else if (piece.GetName() == "Queen")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.bq;
                    }
                    else if (piece.GetName() == "Rook")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.br;
                    }
                }
                else if (piece.GetColor() == "White")
                {
                    if (piece.GetName() == "Bishop")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.wb;
                    }
                    else if (piece.GetName() == "King")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.wk;
                    }
                    else if (piece.GetName() == "Knight")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.wn;
                    }
                    else if (piece.GetName() == "Pawn")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.wp;
                    }
                    else if (piece.GetName() == "Queen")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.wq;
                    }
                    else if (piece.GetName() == "Rook")
                    {
                        buttons[i].Image = global::OnlineChess.Properties.Resources.wr;
                    }
                }
                else
                {
                    buttons[i].Image = null;
                }
            }
        }
Beispiel #3
0
        /*
         * Turns the game board into a string of symbols
         */
        public String BoardToString()
        {
            String boardString = "";

            for (int i = 0; i < this.board.Count; i++)
            {
                ChessPiece piece = this.board.ElementAt(i);
                if (piece.GetName() == "Bishop")
                {
                    if (piece.GetColor() == "Black")
                    {
                        boardString += "B";
                    }
                    else
                    {
                        boardString += "b";
                    }
                }
                else if (piece.GetName() == "King")
                {
                    if (piece.GetColor() == "Black")
                    {
                        boardString += "K";
                    }
                    else
                    {
                        boardString += "k";
                    }
                }
                else if (piece.GetName() == "Knight")
                {
                    if (piece.GetColor() == "Black")
                    {
                        boardString += "N";
                    }
                    else
                    {
                        boardString += "n";
                    }
                }
                else if (piece.GetName() == "Pawn")
                {
                    if (piece.GetColor() == "Black")
                    {
                        boardString += "P";
                    }
                    else
                    {
                        boardString += "p";
                    }
                }
                else if (piece.GetName() == "Queen")
                {
                    if (piece.GetColor() == "Black")
                    {
                        boardString += "Q";
                    }
                    else
                    {
                        boardString += "q";
                    }
                }
                else if (piece.GetName() == "Rook")
                {
                    if (piece.GetColor() == "Black")
                    {
                        boardString += "R";
                    }
                    else
                    {
                        boardString += "r";
                    }
                }
                else
                {
                    boardString += "-";
                }
            }
            return(boardString);
        }