#JAVA Create a new class titled ChessBoard that contains - A private multi-dimen
ID: 3754572 • Letter: #
Question
#JAVA
Create a new class titled ChessBoard that contains
- A private multi-dimensional array of containing the squares of the chess board
Getter and Setter methods for putting a chess piece into that array (yes, similar to the ones in ChessPiece.)
- Include in the Setter methods some basic collision detection. Specifically, do NOT allow a2 piece to occupy a square. This will form the basis for our game "rules" later on.
Create a new application titled setupChessBoard that contains your main statement.
- Populate the ChessBoard with your ChessPiece elements
- Print out the current location of your ChessPieces on your board... a simple list is fine.
Explanation / Answer
class ChessPiece{
int rank;
String suite;
public ChessPiece(int rank, String suite) {
this.rank = rank;
this.suite = suite;
}
@Override
public String toString() {
return rank+" "+suite;
}
}
public class ChessBoard {
private ChessPiece[][] board=new ChessPiece[8][8];
public ChessPiece[][] getBoard() {
return board;
}
public void setBoard(ChessPiece[][] board) {
this.board = board;
}
public ChessBoard() {
// TODO Auto-generated constructor stub
}
public ChessBoard(ChessPiece[][] board) {
this.board=board;
}
public ChessPiece get(int i, int j) {
return board[i][j];
}
public void set(int i, int j, ChessPiece piece) {
if(board[i][j]!=null) {
System.out.println("position already occupied");
return;
}
board[i][j]=piece;
}
}
import java.util.Scanner;
public class setupChessBoard {
ChessBoard board=new ChessBoard();
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
setupChessBoard chessBoard=new setupChessBoard();
for(int i=0; i<64;i++) {
System.out.println("Enter x position of chess piece: ");
int x=sc.nextInt();
System.out.println("Enter x position of chess piece: ");
int y=sc.nextInt();
System.out.println("Enter rank of chess piece: ");
int rank=sc.nextInt();
sc.nextLine();
System.out.println("Enter suite of chess piece: ");
String suite=sc.nextLine();
chessBoard.board.set(x, y, new ChessPiece(rank, suite));
}
for(int i=0; i<8; i++) {
for(int j=0; j<8; j++) {
System.out.println(chessBoard.board.get(i, j));
}
}
}
}
//
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.