Currently, I've been trying to write the code for a TicTacToe game in Java. However, I been having troubles designing the game by following Object Oriented design principles. As of now, I've written up the code for a Board class. The Board class is designed with the single purpose of creating the Board for the TicTacToe game via an Arraylist. Though I feel like the Board class does not follow an Object Oriented Design. I'll be posting the code below, any suggestions or tips on how to make the Board Class Object Oriented would be really appreciated!
import java.util.ArrayList;
public class Board {
ArrayList spots;
public Board() {
this.spots = new ArrayList();
for (int i = 0; i < 9; i++) {
this.spots.add(String.valueOf(i));
}
}
public ArrayList firstRow() {
ArrayList firstRow = new ArrayList();
firstRow.add(this.spots.get(0));
firstRow.add(this.spots.get(1));
firstRow.add(this.spots.get(2));
return firstRow;
}
public ArrayList secondRow() {
ArrayList secondRow = new ArrayList();
secondRow.add(this.spots.get(3));
secondRow.add(this.spots.get(4));
secondRow.add(this.spots.get(5));
return secondRow;
}
public ArrayList thirdRow() {
ArrayList thirdRow = new ArrayList();
thirdRow.add(this.spots.get(6));
thirdRow.add(this.spots.get(7));
thirdRow.add(this.spots.get(8));
return thirdRow;
}
public void display() {
String formattedFirstRow = this.spots.get(0) + " | " + this.spots.get(1) + " | " + this.spots.get(2) + "\n" + this.spots.get(3) + " | " + this.spots.get(4) + " | " + this.spots.get(5) + "\n" + this.spots.get(6) + " | " + this.spots.get(7) + " | " + this.spots.get(8);
System.out.print(formattedFirstRow);
}
}
Aucun commentaire:
Enregistrer un commentaire