You are on page 1of 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