Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

********** IN JAVA ************ I need the following methods completed: Save and Load: YOU WILL NEED THE FOLLOWING CLASSES: Board.java (THIS HAS THE METHODS THAT

********** IN JAVA ************

I need the following methods completed:

Save and Load:

YOU WILL NEED THE FOLLOWING CLASSES:

Board.java (THIS HAS THE METHODS THAT NEED DONE)

/** * This class represents the board. A chess board may contain up to 32 chess * pieces, 16 of each color. Each piece has a unique position on the board, between * a1 and h8. The letters a - h represent columns, while the numbers 1 through 8 * represent rows. The lower left corner of the board is a1. * * Implement save and load methods as described below. * */ public class Board { public static final int BOARD_SIZE = 8; private Piece [][] board = new Piece[BOARD_SIZE][BOARD_SIZE]; private int numberOfPieces = 0; public void addPiece(Piece p, String ps) { //magic int col = ((int)ps.charAt(0)) - 97; int row = ((int)ps.charAt(1)) - 49; if (board[row][col] == null) numberOfPieces++; board[row][col] = p; } public Piece removePiece(String ps) { int col = ((int)ps.charAt(0)) - 97; int row = ((int)ps.charAt(1)) - 49; Piece p = board[row][col]; if (p != null) numberOfPieces--; return p; } public void printBoard() { for (int i = BOARD_SIZE-1; i >= 0; i--) { for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j] == null) { System.out.print("-- "); } else { System.out.print(board[i][j] + " "); } } System.out.println(); } } //Write the contents of the board to the specified file. //Hint: create a new File, new ObjectOutputStream, //and write first the number of pieces on the board, then each piece as //object and position as two byte values. public boolean save(String filename) { return false; } //Read the contents of the board from the specified file. //Hint: create a new File, new ObjectInputStream, //and read first the number of pieces on the board, then each piece as //object and position as two byte values. public boolean load(String filename) { return false; } }

ChessLab.java (THIS IS THE MAIN CLASS)

/** * This program simulates a chess board. The sole purpose of this program * is to demonstrate the saving and loading of objects to */ public class ChessLab { /** * @param args the command line arguments */ public static void main(String[] args) { Board b = new Board(); b.addPiece(new Piece("R", "B"), "a8"); b.addPiece(new Piece("N", "B"), "b8"); b.addPiece(new Piece("B", "B"), "c8"); b.addPiece(new Piece("Q", "B"), "d8"); b.addPiece(new Piece("K", "B"), "e8"); b.addPiece(new Piece("B", "B"), "f8"); b.addPiece(new Piece("N", "B"), "g8"); b.addPiece(new Piece("R", "B"), "h8"); for (char i = 'a'; i <= 'h'; i++) { b.addPiece(new Piece("P", "B"), i + "7"); b.addPiece(new Piece("P", "W"), i + "2"); } b.addPiece(new Piece("R", "W"), "a1"); b.addPiece(new Piece("N", "W"), "b1"); b.addPiece(new Piece("B", "W"), "c1"); b.addPiece(new Piece("Q", "W"), "d1"); b.addPiece(new Piece("K", "W"), "e1"); b.addPiece(new Piece("B", "W"), "f1"); b.addPiece(new Piece("N", "W"), "g1"); b.addPiece(new Piece("R", "W"), "h1"); System.out.println("Starting board state..."); b.printBoard(); System.out.println("Saving file..."); b.save("savegame.dat"); System.out.println("Erasing board in memory..."); b = new Board(); System.out.println("Loading board from file..."); b.load("savegame.dat"); System.out.println("Restored board..."); b.printBoard(); } }

Piece.java

import java.io.Serializable; /** * */ public class Piece implements Serializable { public static enum Rank {QUEEN, KING, BISHOP, KNIGHT, ROOK, PAWN}; public static enum Color {BLACK, WHITE}; private Rank type; private Color color; public Piece(String type, String color) { switch(type) { case "K": this.type = Rank.KING; break; case "Q": this.type = Rank.QUEEN; break; case "N": this.type = Rank.KNIGHT; break; case "R": this.type = Rank.ROOK; break; case "B": this.type = Rank.BISHOP; break; default: this.type = Rank.PAWN; } if (color.equals("W")) { this.color = Color.WHITE; } else { this.color = Color.BLACK; } } @Override public String toString() { String name = (color==Color.WHITE)?"w":"b"; switch (type) { case BISHOP: name += "B"; break; case KING: name += "K"; break; case KNIGHT: name += "N"; break; case PAWN: name += "P"; break; case QUEEN: name += "Q"; break; default: name += "R"; break; } return name; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions