Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

COP-3337 Programming II Programming Assignment 2: FIU Knight Foundation School of Computing & Info. Sciences In this assignment, you are asked to complete a Java program that uses inheritance and...

1 answer below »

COP-3337 Programming II
Programming Assignment 2:
FIU Knight Foundation School of Computing & Info. Sciences
In this assignment, you are asked to complete a Java program that uses inheritance and
polymorphism to implement the Chess game. Your program must keep track of pieces on
the chess board from the time that game begins until the time that players make their final
moves/captures. The program needs to prevent the players from making illegal moves.
1 Given Classes
1.1 util.Piece
This class (which is the superclass of all classes representing Pawn (p), Knight (N), Bishop
(B), Rook (R), Queen (Q) and King (K)) contains a protected boolean variable named
“white” and is true if the piece color is white and false otherwise. It also has a constructo
initializing white, and a method boolean isLegal(Move move, Game game) to check the legal-
ity of a given move/capture in a given game. This method returns true if the move/capture
is legal and returns false otherwise. Since the legality of a move/capture depends on the
type of piece that is displaced, you need to ove
ide this method in every subclass of this
class.
1.2 util.Square
This class which represents one of the 64 squares of the chess board contains two private
instance fields boolean lightcolor and Piece occupant. The first variable has a public gette
and the second one has a getter and setter. The square is light-color if and only if the
lightcolor is true. Also, the square is empty if and only if the occupant is null. In the case
that the square is not empty, occupant refers to the piece occupying that square.
1.3 game.Move
This class represent a move or capture made by a player on the chessboard in a game. A
chessboard is a 8 × 8 table of objects of class Square. In this table, rows are called ranks,
and columns are called files.
As seen in Figure 1, the table row
ank with index 0 is labeled with “8”, index 1 is
labeled with “1”, . . . , and index 7 is labeled by “1”. Also, the table column/file with index
0 is labeled with “a”, index 1 is labeled by “b”, . . . , and index 7 is labeled with “h”.
1
Figure 1: Row and column labels in a chessboard.
To specify a move or capture, this class uses the coordinates (rank and file) of two squares:
the source and the target. For example, move d2d4 represents the move/capture in which
the piece occupying square d2 (source square with row index 6 and column index 3) has been
placed on square d4 (target square with row index 4 and column index 3).
Class Move is made of five private instance fields: String move which keeps track of
the string representation of the move (e.g. a8b7 or d2d4), int row0, col0 which store the
coordinates of source square, and int row1, col1 which store the coordinates of target square.
The first one can be accessed by calling the toString method of this class. The other fou
have public getters.
1.4 game.Game
This class represents a Chess game and is made of the following private instance fields:
ˆ String player1 : name of the first playe
ˆ String player2 : name of the second playe
ˆ A
ayList〈Move〉 moves : list of all moves played in the game so fa
ˆ Square[][] board : A 2D a
ay of Square objects to keep track of the occupants of every
square in the board.
It also has public getters for players’ names, the public method Piece getPiece(int row, int
col) to get the piece occupying a given square, and the public method boolean isWhiteTurn()
which returns true if it is player one’s turn to move and returns false if it is player two’s to
move.
Another public method of this class is boolean move(Move move) which moves a piece
from one square to the other. Obviously the source and target squares are given by the
2
object of class Move passed to this method. As the return value, this method returns true
if the move is legal; otherwise, it returns false.
Moreover, there exists another public method with signature void showBoard(PrintStream
stream) that gets a out stream (e.g. System.out) and prints the board and its occupants on
the out stream.
Finally, it has a constructor that places all pieces on the board and initializes the instance
fields of the class. Below you can see the initial setting of pieces made by Game’s constructor:
n b q k b n
p p p p p p p p
P P P P P P P P
R N B Q K B N R
2 Program Commands
Your program must be responsive to the following commands entered by user via standard
input stream:
ˆ new game PLAYER1 PLAYER2: creates a new game between two players with given
names PLAYER1 and PLAYER2. The first player owns white pieces while the second
player plays with black pieces.
ˆ mv α0β0α1β1: moves the piece from square α0β0 to square α1β1 where α0 and α1 can
e letters from a to h; while β0 and β1 can be integers from 1 to 8. If the move is
legal, you need to print the following message and then display the board; otherwise,
you need to print an “illegal move” message on screen.
player1 + “ moves: α0β0α1β1\n” OR player2 + “ moves: α0β0α1β1\n”
ˆ cp α0β0 α1β1: captures the piece in square α1β1 with the piece in square α0β0 where
α0 and α1 can be letters from a to h; while β0 and β1 can be integers from 1 to 8. If the
3
capture is legal, you need to print the following message and then display the board;
otherwise, you need to print an “illegal capture” message on screen.
player1 + “ captures: α0β0α1β1\n” OR player2 + “ captures: α0β0α1β1\n”
ˆ undo: undoes the previous move/capture and displays the updated board.
ˆ print status: prints out the cu
ent status of pieces on the board followed by the list
of moves played in the game.
3 Legal Moves and Captures
Figure 2 shows how knight can move on the board and capture pieces with opposite color on
the board. Also, it shows the direction in which white pawn moves and captures. The black
pawn moves and captures in the opposite direction. In summary, a pawn can move only one
cell forward at a time; however, if it is white and is in the second rank (or is black and is in
the seventh rank), it has the option of moving two cells forward as well if there is no piece
on its way.
Figure 2: Movement and capture of knight (left), movement (middle) and capture of pawn
(right) on chessboard.
Bishop moves diagonally and rook moves vertically or horizontally. Neither bishop no
ook cam jump over any piece while moving from one square to the other. Queen can move
like a bishop or rook. King moves one square at a time to one of the four adjacent squares.
4 30% Bonus Parts
1. As the first bonus point, your program must support promotion of white
lack pawn
to a queen, knight, rook or bishop once it reaches to the eighth/first rank.
2. As the second bonus part, your program must support en passant move
(see https:
en.wikipedia.org/wiki/En passant for details)
4
3. As the third bonus part, your program must print a “check” message once the king is
in check and print “checkmate” once the king is in check and there is no legal move
that gets king out of check.
4. (ungraded extra items) your program must support castling long and short moves.
5. (ungraded extra items) your program must support draw offers and draw by three-fold
epetition.
5 Submissions
You need to submit a .zip file compressing the following folders:
ˆ the packages containing all the java source files related to the assignment (.java files).
ˆ A readme file clearly explaining what parts have/haven’t been implemented.
5

What main should contain:
case "print":
        String secondToken = scnr.next();
        if(secondToken.equals("status"){
            game.showBoard(System.out);
            System.out.println("List of moves: " + game);
        }
        else{
            System.out.prinln("Illegal command! please try again");
        }
    case "undo":
        Game alternative = new Game(game.getPlayer1(), game.getPlayer2());
        /*...*
        game = alternative;
What Pawn.isLegal should contain:
@Ove
ide
    public boolean isLegal(Move move, Game game) {
    if(!super.isLegal(move, game))
        return false;
   
ules for pawn only!
    int rowDiff = move.getRow1() - move.getRow0();
    int colDiff = move.getCol1() - move.getCol0();
    if(rowDiff > 0 && white || rowDiff < 0 && !white)
        return false;
pawn doesn't move backward!
   
add more rules here
    if(game.getPiece(move.getRow1(),move.getCol1()) == null){
move not capture
        if (colDiff != 0)
non-vertical
            return false;
        return Math.abs(rowDiff) == 1 || 
            Math.abs(rowDiff) == 2 && move.getRow0() == 1 && !white && game.getPiece(2,move.getCol0()) == null ||
            Math.abs(rowDiff) == 2 && move.getRow0() == 6 && white && game.getPiece(5,move.getCol0()) == null;
    }else
        return Math.abs(rowDiff) * Math.abs(colDiff) == 1;
    }
What King.isLegal should contain:
@Ove
ide
    public boolean isLegal(Move move, Game game) {
    if(!super.isLegal(move, game))
        return false;
    int rowDiff = move.getRow1() - move.getRow0();
    int colDiff = move.getCol1() - move.getCol0();
    return Math.abs(rowDiff) == 1 && Math.abs(colDiff) == 1 || Math.abs(rowDiff) + Math.abs(colDiff) == 1;
    }
What Bishop.isLegal should contain:
int rowDiff = move.getRow1() - move.getRow0();
    int colDiff = move.getCol1() - move.getCol0();
    return rowDiff * colDiff != 0;
What Rook.isLegal should contain:
int rowDiff = move.getRow1() - move.getRow0();
    int colDiff = move.getCol1() - move.getCol0();
    return rowDiff * colDiff == 0;
What Knight.isLegal should contain:
int rowDiff = move.getRow1() - move.getRow0();
    int colDiff = move.getCol1() - move.getCol0();
    return Math.abs(rowDiff * colDiff) == 2
PREVIOUSNEXT
Answered 1 days After Feb 17, 2022

Solution

Abhishek answered on Feb 18 2022
113 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here