You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.0 KiB

package Game;
import java.util.ArrayList;
public class Tictactoe extends Game {
enum State {
CIRCLE,
CROSS,
EMPTY
}
private String input;
private State[] currentBoard;
public Tictactoe() {
init();
}
private void init() {
currentBoard = newBoard();
}
@Override
public void update(String string) {
writeBoard(currentBoard);
}
public char getStatedChar(State state) {
switch (state) {
case CIRCLE:
return 'O';
case CROSS:
return 'X';
case EMPTY:
return ' ';
default:
return '-';
}
}
private State[] newBoard() {
State[] board = new State[9];
for (int i = 0; i < board.length; i++) {
board[i] = State.EMPTY;
}
return board;
}
public State[] getCurrentBoard() {
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]) +" ");
}
}