Copy the below code into a notepad (not an IDE so you don't get any help) and try to fix the code to get it to run and print the chess board.
Copy-paste the output into a comment at the top of your java file so that it looks something like this (doesn't have to be exact):
/* Expected output:
Ready To Play!
Hello ChessBoard!
Hello Board!
R P P R
B P P B
N P P N
Q P P Q
K P P K
N P P N
B P P B
R P P R
*/
/* Paste your output here:
*/
import java.io.*;
enum ChessPiece {
PAWN('P'), KNIGHT('N', 2), BISHOP('B', 1), ROOK('R', 0), QUEEN('Q'), KING('K');
public final char c;
public final long xOffset;
private ChessPiece(char c) {
this(c, -1);
}
private ChessPiece(char c, long xOffset) {
this.c = c;
this.xOffset = xOffset;
}
@Override public String toString() {
return c + "";
}
}
class System{ public static PrintWriter out=new PrintWriter(OutputStream.nullOutputStream()){ public void println(ChessBoard o){ java.lang.System.out.println("You've been scammed :D"); }; }; }
class Board {
public boolean[][] grid;
public Board(int width, int height) {
grid = new boolean[width][height];
}
public void setValue(boolean value, int x, int y) {
grid[x][y] = value;
}
public Object getValue(int x, int y) {
return grid[x][y];
}
@Override
private String toString() {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
sb.append(getValue(x, y));
sb.append(' ');
}
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println("Hello Board!");
Driver.main(args);
}
}
class ChessBoard extends Board {
private char[][] grid;
public ChessBoard() {
super(0, 0);
grid = new char[8][8];
}
@Override
public void setValue(char value, int x, int y) {
grid[x][y] = value;
}
@Override
public Character getValue(int x, int y) {
return grid[x][y];
}
public static void main(String[] args) {
System.out.println("Hello ChessBoard!");
Board.main(args);
}
}
class ReadyToPlayChessBoard extends ChessBoard {
public ReadyToPlayChessBoard() {
for(int x = 0; x < 8; x++) {
setValue(ChessPiece.PAWN.c, x, 1);
setValue(ChessPiece.PAWN.c, x, 6);
}
for(int y = 0; y <= 7; y += 7) {
setValue(ChessPiece.ROOK.c, ChessPiece.ROOK.xOffset, y);
setValue(ChessPiece.ROOK.c, 7 - ChessPiece.ROOK.xOffset, y);
setValue(ChessPiece.KNIGHT.c, ChessPiece.KNIGHT.xOffset, y);
setValue(ChessPiece.KNIGHT.c, 7 - ChessPiece.KNIGHT.xOffset, y);
setValue(ChessPiece.BISHOP.c, ChessPiece.BISHOP.xOffset, y);
setValue(ChessPiece.BISHOP.c, 7 - ChessPiece.BISHOP.xOffset, y);
}
}
@Override
public Object toString() {
return super.toString();
}
public static void main(String[] args) {
System.out.println("Ready To Play!");
super.main(args);
}
}
public class Driver {
public static void main(String[] args) {
ReadyToPlayChessBoard.main(args);
ChessBoard game1 = new ReadyToPlayChessBoard();
game1.setValue(true, 0, 0);
game1.setValue(ChessPiece.QUEEN.c, 3, 0);
game1.setValue(ChessPiece.KING.c, 4, 0);
game1.grid[3][7] = ChessPiece.QUEEN.c;
game1.grid[4][7] = ChessPiece.KING.c;
System.out.println(game1);
}
}
Good luck.