0% found this document useful (0 votes)
43 views2 pages

Game Board

The document defines classes for a game board and game pieces, including a board class to represent the game board as a 2D array, a game piece class to represent individual pieces with row and column positions, and a driver class to test creating boards and adding pieces.

Uploaded by

api-315616667
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Game Board

The document defines classes for a game board and game pieces, including a board class to represent the game board as a 2D array, a game piece class to represent individual pieces with row and column positions, and a driver class to test creating boards and adding pieces.

Uploaded by

api-315616667
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Game Board Code

public class board{


private gamePiece[][]theBoard;
public board(){
theBoard=new gamePiece[4][4];
}
public void addPiece(gamePiece piece){
theBoard[piece.getRow()][piece.getCol()]=piece;
}
public String toString(){
String output=new String();
for(int row=0; row<theBoard.length;row++){
for(int col=0; col<theBoard[0].length;col++){
output+=theBoard[row][col]+"\t";
}
output+="\n";
}
return output;
}
}
--------------------------------------------------------------------------public class gamePiece{
private int row;
private int col;
private board board;
public gamePiece(int row, int col, board board){
this.row=row;
this.col=col;
this.board=board;
}
public void addSelfToBoard(){
board.addPiece(this);
}
public int getRow(){
return this.row;
}
public int getCol(){
return this.col;
}
public String toString(){
return row+","+col;
}

}
--------------------------------------------------------------------------public class boardDriver{
public static void main(String[]args){
board gameboard=new board();
board trialboard=new board();
gamePiece piece1=new gamePiece(3,3, gameboard);
//gameboard.addPiece(piece1);
gamePiece trial1=new gamePiece(1,2,trialboard);
//trialboard.addPiece(trial1);
gamePiece piece2=new gamePiece(2,3,gameboard);
//gameboard.addPiece(piece2);
gamePiece trial2=new gamePiece(2,2,trialboard);
//trialboard.addPiece(trial2);
piece1.addSelfToBoard();
piece2.addSelfToBoard();
trial1.addSelfToBoard();
trial2.addSelfToBoard();
System.out.println("Gameboard\n"+gameboard);
System.out.println("Trialboard\n"+trialboard);
}
}
-------------------------------------------------------------------------------------

You might also like