Browse Source

added write board function for TicTacToe

feature-game
Dion Aliu 3 years ago
parent
commit
711f610c54
  1. 2
      src/main/java/Game/Game.java
  2. 29
      src/main/java/Game/Tictactoe.java

2
src/main/java/Game/Game.java

@ -6,6 +6,8 @@ public abstract class Game {
protected ArrayList<String> outputBuffer = new ArrayList<>(); protected ArrayList<String> outputBuffer = new ArrayList<>();
public abstract void update(String string);
public void print() { public void print() {
for (String s : outputBuffer) { for (String s : outputBuffer) {
System.out.println(s); System.out.println(s);

29
src/main/java/Game/Tictactoe.java

@ -4,7 +4,6 @@ import java.util.ArrayList;
public class Tictactoe extends Game { public class Tictactoe extends Game {
enum State { enum State {
CIRCLE, CIRCLE,
CROSS, CROSS,
@ -21,7 +20,10 @@ public class Tictactoe extends Game {
currentBoard = newBoard(); currentBoard = newBoard();
} }
@Override
public void update(String string) {
writeBoard(currentBoard);
}
public char getStatedChar(State state) { public char getStatedChar(State state) {
switch (state) { switch (state) {
@ -47,4 +49,27 @@ public class Tictactoe extends Game {
public State[] getCurrentBoard() { public State[] getCurrentBoard() {
return currentBoard; return currentBoard;
} }
/*
1 2 3
o x o
4 5 6
o x o
7 8 9
o x o
*/
private void writeBoard(State[] state) {
this.outputBuffer.clear();
outputBuffer.add("1 ║2 ║3");
outputBuffer.add(" " + getStatedChar(state[0]) + " ║ " + getStatedChar(state[1]) + " ║ " + getStatedChar(state[2]) +" ");
outputBuffer.add("═════╬═════╬═════");
outputBuffer.add("4 ║5 ║6");
outputBuffer.add(" " + getStatedChar(state[3]) + " ║ " + getStatedChar(state[4]) + " ║ " + getStatedChar(state[5]) +" ");
outputBuffer.add("═════╬═════╬═════");
outputBuffer.add("7 ║8 ║9");
outputBuffer.add(" " + getStatedChar(state[6]) + " ║ " + getStatedChar(state[7]) + " ║ " + getStatedChar(state[8]) +" ");
}
} }
Loading…
Cancel
Save