Question
Main objective : Store a chessboard as a linked list of chess pieces. Assume an 8 8 chessboard. Implement the following procedures that, given a
Main objective: Store a chessboard as a linked list of chess pieces. Assume an 8 8 chessboard. Implement the following procedures that, given a chessboard:
Determine validity: verify that two pieces do not occupy the same square.
Find piece: given a square, determine (if any) the chess piece at that square.
Determine attack (from that piece): determine if the piece found above attacks another piece. Note that the pieces must be of opposite colors. Do not worry about blocking, so assume that pieces can pass through other pieces on the board. This is not true for proper chess, but makes life much easier for this assignment. Thus, a piece x is said to attack another pieces y, if x can travel to ys square (according to the rules of chess) assuming there are no other pieces on the board.
Do not worry about non-standard moves such as en passant or castling. You will not get any credit if you do not implement using a linked list. Furthermore, you have to write your own linked list from scratch. You cannot use built in libraries for linked lists.
Suggestions for coding: You do not have to follow these instructions, but it might make the code nicer. Create a (super)class ChessPiece with children subclasses for each different type of piece. Each of these can implement their own attacking function. Think about where you want to store the position and color of each piece.
Create a class ChessBoard that has a linked list of ChessPiece objects. This class can have methods for determining validity, finding, etc.
Format: You should provide a Makefile. On running make, it should create Chessboard.jar. It takes two command line arguments: an input file and an output file
The format of the input file is as follows.
The line starts with two integers, referring to a chessboard. This is followed by a colon. Then, the line contains a list of pieces denoted by char column row, where char is one of k (king), q (queen), r (rook), b (bishop), n (knight). No pawns for now. If the character is capitalized, it denotes black pieces, otherwise, the piece is white. The next line simply contains the coordinates of a single square. For example, these could look like:
8 2: q 4 3 k 4 4 r 8 2 R 8 8 b 1 1 K 4 8 N 7 7
The portion after the colon denotes a chessboard, and the portion before the colon is a query asking what is at (8,2). The answer here is a white rook, denoted r. This pattern of lines continues throughout input.txt.
Do not worry about error handling on the input, so you can assume that inputs will always have this format. No piece will be placed outside the chessboard. (You may want to use the split method in java for splitting strings.)
Output: On running (say) java -jar Chessboard.jar in.txt output.txt, the input file in.txt is read in, and the output file out.txt should be produced. The ith line of the output file corresponds to the ith chessboard. The ith line of the output file should contain:
If the ith chessboard is not valid, the line should be Invalid.
If the ith chessboard is valid: first output the chesspiece at the query square. If none, print -. If there is piece at the query square, print a space, and then y (the piece attacks some other piece) or n (it does not attack another piece). Pay attention to the space. For the example given above, the output is:
r y
print solution method given:
class PrintSolutionSnippet { private static int board_size; // A cool method that actually prints a chessboard with chesspieces onto the console. Uses some UNICODE awesomeness // Input: 2D character array isFilled, where isFilled[i][j] is K, Q, R, B or N for black king, queen, rook, bishop or knight chesspieces and // k, q, r, b or n if there are white king, queen, rook, bishop or knight respectively at row i, col j. // Assumes that isFilled is board_size X board_size. // Output: void, basically prints the chessboard with the chesspiece onto console private static void printSolution(char[][] isFilled){ String line = " "; // starting strings to be printed. line is the dividing horizontal strip along the board String col_nums = " ", col_str = ""; // these strings are for printing the column numbers below for (int i=1; i < board_size+1; i++) { // loop over all the columns to create the line and col_nums strings line = line + "+--"; // each iteratin add "+--" to line if (i < 10) // have to break into cases, since i < 10 is one symbol, but i >= 10 is two col_str = " " + Integer.toString(i) + " "; // add string i to col_str with spacing else col_str = " " + Integer.toString(i); // add string i to col_str with less spacing, since i >= 10 col_nums = col_nums + col_str ; // append col_str to col_nums } line = line + "+"; // complete line string for (int i=board_size; i > 0; i--) { // loop over all the rows in decreasing order. each iteration will print a col System.out.println(line); // start by printing a line String pieces, background, chesspiece; // pieces string will actually put the chesspiece symbols into string if (i < 10) // pieces begins with col number, again break into cases if i has 1 vs 2 symbols pieces = " "+Integer.toString(i); else pieces = Integer.toString(i); for (int j=1; j < board_size+1; j++) { // now loop over cols to create individual squares if ((i+j)%2 == 1) // place alternating black or red background for squares background = "\u001B[40m"; // ANSI escape code for black else background = "\u001B[41m"; // ANSI escape code for red switch(isFilled[i][j]) { case 'k': chesspiece = "\u2654 "; // put UNICODE symbol for white king break; case 'q': chesspiece = "\u2655 "; // put UNICODE symbol for white queen break; case 'r': chesspiece = "\u2656 "; // put UNICODE symbol for white rook break; case 'b': chesspiece = "\u2657 "; // put UNICODE symbol for white bishop break; case 'n': chesspiece = "\u2658 "; // put UNICODE symbol for white knight break; case 'p': chesspiece = "\u2659 "; // put UNICODE symbol for white pawn break; case 'K': chesspiece = "\u265A "; // put UNICODE symbol for black king break; case 'Q': chesspiece = "\u265B "; // put UNICODE symbol for black queen break; case 'R': chesspiece = "\u265C "; // put UNICODE symbol for black rook break; case 'B': chesspiece = "\u265D "; // put UNICODE symbol for black bishop break; case 'N': chesspiece = "\u265E "; // put UNICODE symbol for black knight break; case 'P': chesspiece = "\u265F "; // put UNICODE symbol for black pawn break; default: chesspiece = " "; } pieces = pieces + "|"+background+chesspiece+"\u001B[0m"; // put UNICODE symbol for queen with a line. also set background, and then apply ANSI reset code } System.out.println(pieces+"|"); } System.out.println(line); // print out the final line System.out.println(col_nums); // print out the columns below } // Method to initialize isFilled array with '-' // Input: 2D char array and its integer size // Output: Updated char array public static char[][] initializeBoard(char[][] isFilled, int size) { for(int i = 0; i < size+1; i++) { for(int j = 0; j < size+1; j++) { isFilled[i][j] = '-'; } } return isFilled; } public static void main(String[] args) { // If your solution placed the chesspieces as shown in isFilledMatrix1 and isFilledMatrix2, the printSolution // will print out the board. // Example 1: System.out.println("Example 1:"); board_size = 4; char[][] isFilledMatrix1 = new char[board_size+1][board_size+1]; isFilledMatrix1 = initializeBoard(isFilledMatrix1, board_size); isFilledMatrix1[4][1] = 'K'; // Place black king in row 4, col 1 isFilledMatrix1[3][3] = 'b'; // Place white bishop in row 3, col 3 isFilledMatrix1[3][4] = 'k'; // Place white king in row 3, col 4 isFilledMatrix1[1][2] = 'r'; // Place white rook in row 1, col 2 isFilledMatrix1[1][4] = 'B'; // Place black bishop in row 1, col 4 printSolution(isFilledMatrix1); // Example 2: System.out.println("Example 2:"); board_size = 8; char[][] isFilledMatrix2 = new char[board_size+1][board_size+1]; isFilledMatrix2 = initializeBoard(isFilledMatrix2, board_size); isFilledMatrix2[1][1] = 'b'; // Place white bishop in row 1, col 1 isFilledMatrix2[4][8] = 'K'; // Place black king in row 4, col 8 isFilledMatrix2[3][4] = 'q'; // Place white queen in row 3, col 4 isFilledMatrix2[4][4] = 'k'; // Place white king in row 4, col 4 isFilledMatrix2[7][7] = 'N'; // Place black knight in row 7, col 7 isFilledMatrix2[2][8] = 'r'; // Place white rook in row 2, col 8 isFilledMatrix2[8][8] = 'r'; // Place white rook in row 8, col 8 printSolution(isFilledMatrix2); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started