Browse Source

refactoring: created Board class for better object orientation and extracted functionality from Tictactoe to board with tests

feature-game
Dion Aliu 2 years ago
parent
commit
289a1f90a1
  1. 66
      src/main/java/Game/TicTacToe/Board.java
  2. 64
      src/main/java/Game/Tictactoe.java
  3. 39
      src/test/java/Game/TicTacToe/BoardTest.java
  4. 14
      src/test/java/Game/TictactoeTest.java

66
src/main/java/Game/TicTacToe/Board.java

@ -0,0 +1,66 @@
package Game.TicTacToe;
import java.util.ArrayList;
public class Board {
enum State {
CIRCLE,
CROSS,
EMPTY
}
private State[] states;
public Board() {
states = new State[9];
for (int i = 0; i < states.length; i++) {
states[i] = State.EMPTY;
}
}
public static char getStatedChar(State state) {
switch (state) {
case CIRCLE:
return 'O';
case CROSS:
return 'X';
case EMPTY:
return ' ';
default:
return '-';
}
}
/*
1 2 3
o x o
4 5 6
o x o
7 8 9
o x o
*/
public ArrayList<String> getOutputBoard() {
ArrayList<String> outputBoard = new ArrayList<>();
outputBoard.add("1 ║2 ║3");
outputBoard.add(" " + getStatedChar(states[0]) + " ║ " + getStatedChar(states[1]) + " ║ " + getStatedChar(states[2]) +" ");
outputBoard.add("═════╬═════╬═════");
outputBoard.add("4 ║5 ║6");
outputBoard.add(" " + getStatedChar(states[3]) + " ║ " + getStatedChar(states[4]) + " ║ " + getStatedChar(states[5]) +" ");
outputBoard.add("═════╬═════╬═════");
outputBoard.add("7 ║8 ║9");
outputBoard.add(" " + getStatedChar(states[6]) + " ║ " + getStatedChar(states[7]) + " ║ " + getStatedChar(states[8]) +" ");
return outputBoard;
}
public State[] getStates() {
return this.states;
}
}

64
src/main/java/Game/Tictactoe.java

@ -1,75 +1,25 @@
package Game;
import java.util.ArrayList;
import Game.TicTacToe.Board;
public class Tictactoe extends Game {
enum State {
CIRCLE,
CROSS,
EMPTY
}
private String input;
private State[] currentBoard;
private Board currentBoard;
private boolean crossTurn;
public Tictactoe() {
init();
}
private void init() {
currentBoard = newBoard();
crossTurn = true;
currentBoard = new Board();
}
@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 void update(String input) {
outputBuffer.addAll(currentBoard.getOutputBoard());
}
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]) +" ");
}
}

39
src/test/java/Game/TicTacToe/BoardTest.java

@ -0,0 +1,39 @@
package Game.TicTacToe;
import Game.Tictactoe;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class BoardTest {
Board board;
@BeforeEach
void setUp() {
board = new Board();
}
@AfterEach
void tearDown() {
}
@Test
void getStatedChar() {
assertEquals(Board.getStatedChar(Board.State.CIRCLE), 'O');
assertEquals(Board.getStatedChar(Board.State.CROSS), 'X');
assertEquals(Board.getStatedChar(Board.State.EMPTY), ' ');
}
@Test
void testForEmptyBoardOnInitialization() {
for (int i = 0; i < 9; i++) {
assertEquals(board.getStates()[i], Board.State.EMPTY);
}
}
}

14
src/test/java/Game/TictactoeTest.java

@ -24,18 +24,4 @@ class TictactoeTest {
}
@Test
void getStatedChar() {
assertEquals(ttt.getStatedChar(Tictactoe.State.CIRCLE), 'O');
assertEquals(ttt.getStatedChar(Tictactoe.State.CROSS), 'X');
assertEquals(ttt.getStatedChar(Tictactoe.State.EMPTY), ' ');
}
@Test
void testForEmptyBoardOnInitialization() {
Tictactoe.State[] board = ttt.getCurrentBoard();
for (int i = 0; i < board.length; i++) {
assertEquals(board[i], Tictactoe.State.EMPTY);
}
}
}
Loading…
Cancel
Save