diff --git a/src/main/java/Game/TicTacToe/Board.java b/src/main/java/Game/TicTacToe/Board.java new file mode 100644 index 0000000..92ac33e --- /dev/null +++ b/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 getOutputBoard() { + ArrayList 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; + } +} diff --git a/src/main/java/Game/Tictactoe.java b/src/main/java/Game/Tictactoe.java index c936bb2..b8c09eb 100644 --- a/src/main/java/Game/Tictactoe.java +++ b/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]) +" "); - } } diff --git a/src/test/java/Game/TicTacToe/BoardTest.java b/src/test/java/Game/TicTacToe/BoardTest.java new file mode 100644 index 0000000..e75cbdb --- /dev/null +++ b/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); + } + } + + + +} \ No newline at end of file diff --git a/src/test/java/Game/TictactoeTest.java b/src/test/java/Game/TictactoeTest.java index 08e51ef..5eb08d8 100644 --- a/src/test/java/Game/TictactoeTest.java +++ b/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); - } - } } \ No newline at end of file