Python算法分析代写 | Analysis of Algorithms Homework 3



We will create a simple chess program that could 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 shown below.


Refer to:

( to learn chess piece movements if you

are unfamiliar.

Use this code for FILE MANIPULATION:

public static void main(String[] args) throws FileNotFoundException {

Scanner scan = new Scanner(new File(“input.txt”));

String line;

String[] lineSegment;



line = scan.nextLine();

lineSegment = line.split(” “); // stores all input in array of Strings


PRELIM WORK. The ChessPiece

To start us off, create an abstract ChessPiece class. What instance data would all chess pieces have? Knowing that all the chess pieces move in different ways, declare an abstract method called move. This method will return true or false depending if the move is valid or not.

PART A. The Chess Board

Now, 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 subclasses of 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/back at a time. However, it can move 2 spaces ONLY if it’s that pawn’s first move of the “game.”
  1. ROOK – This pieces moves forwards/backwards and sideways.
  2. KNIGHT – This piece can jump in an L-shaped pattern.
  3. BISHOP – This piece moves diagonally.

Make sure the four classes above use ChessPiece as its super class.

Now create a driver program, to test what you have so far. In this driver program,create your board, use the file input1.txt to place chess pieces on your board and display them after each additional piece is set.