Java 代写 | Project 4: Basic Chess


Project 4: Basic Chess

Due May 7 by 11:59pm

We will create a simple chess program that couldl lead to a complete chess game implementation. For now, you will create a basic chess board. You are not required to complete the game. However, you will be given an input file that will dictate what chess pieces are on which squares on the board. Then the input file will also have you move these chess pieces. Your job is to check if these moves are valid. If so, move the particular chess piece. If not, tell the user that the move is invalid. A sample run is below.

Scanner scan = new Scanner(new File(“input.txt”)); String line; String[] lineSegment;

while(scan.hasNextLine()) {

line = scan.nextLine();

lineSegment = line.split(” “);

… }

PART A. The Chess Board

To start us off, create the board of the game using a 2D array. to display a chess board, we will do a basic print out like shown:

Notice that this board follows the Cartesian Coordinate System, except that the (0,0) coordinate pair is not the intersection of the two planes. Also note that we are used to printing out our 2D arrays beginning with (0,0) at the top left of the grid. That is not the case here. Figure out how to do this. How would the code look? Perhaps write down the coordinates for each element in the grid to figure out how to print your 2D array.

PART B. Place the chess pieces on the board.

At this point, you need to create the chess pieces. Here is the SUBSET of Chess Pieces you need to implement and how each piece can move on the board.

1. PAWN – This piece moves one space forward at a time. However, it can move 2 spaces forward ONLY if it’s that pawn’s first move of the ” game.”

2. ROOK – This pieces moves forwards and sideways.

3. KNIGHT – This piece can jump in an L-shaped pattern.

4. BISHOP – This piece moves diagonally.

All these chess pieces have common functionalities. First create a ChessPiece class, and make sure the four classes above use ChessPiece as its super class. Have an abstract method in ChessPiece called move.

Above is a sample initial state of the board with an input file that looks like this:

knight 0 0 pawn 7 0 rook 2 6 bishop 2 5 pawn 5 5 bishop 7 3 Note: the knight is denoted as an ” -h-” on the board to avoid confusion with the presence of a king in the future.

PART C. Move the chess pieces.

The next part of the input file will give you a list of moves that you should attempt to accomplish. One such move is having the pawn at coordinate (7,0) move to (7,2).

PART D. Creating Exceptions for Error Cases.

When a chess piece attempts to move illegally, we will throw exceptions.

First, create an OutOfBoardException, wherein you throw this exception for a move whose destination is outside the playing board.

Second, create an IllegalChessMoveException is thrown for illegal chess piece movements — such as a pawn moving diagonally or sideways.

Third, create a PathwayException wherein it is an illegal move if there is another chess piece on the way – either at the destination coordinate or in the path to the destination coordinate. (However, a knight can jump over chess pieces.)

Note: These new Exceptions you create shall be subclasses of the Exception class. Also, make sure to handle the exception by telling the user the error and allowing the program to continue (i.e. program does not quit.).

SAMPLE RUN: Note: the sample Input File is provided for you.

PART E. Complete Board Complete the setup of the board

1. First you need to create two new ChessPiece subclasses

King – This piece moves one space in any direction Queen – This piece moves any amount of spaces in any direction

2. Create a new input file that sets up the board such that a complete chess set is on the board.

3. Then modify your new input file to make sure that the movement of each kind of chess piece works as we expect.


Please start this project early and submit often via GitHub. Submit a link to your repository here one you are finished. In your README file, include any information you would like us to know. For example, any outside resources you used, what works/doesn’t work, special cases you are concerned about, etc. Only submit code that compiles. Create code that is documented, clean and elegant.


PART A – 10 points PART B – 20 points PART C – 35 points PART D – 25 points PART E – 10 points Code Style / Organization – 5 points

EXTRA CREDIT: 20 points

Incorporate a visual component to the game like the one shown below.


本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail: [email protected]  微信:dmxyzl003