Browse Source
refactoring: created Board class for better object orientation and extracted functionality from Tictactoe to board with tests
feature-game
refactoring: created Board class for better object orientation and extracted functionality from Tictactoe to board with tests
feature-game
Dion Aliu
3 years ago
4 changed files with 112 additions and 71 deletions
-
66src/main/java/Game/TicTacToe/Board.java
-
64src/main/java/Game/Tictactoe.java
-
39src/test/java/Game/TicTacToe/BoardTest.java
-
14src/test/java/Game/TictactoeTest.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; |
||||
|
} |
||||
|
} |
@ -1,75 +1,25 @@ |
|||||
package Game; |
package Game; |
||||
|
|
||||
import java.util.ArrayList; |
|
||||
|
import Game.TicTacToe.Board; |
||||
|
|
||||
public class Tictactoe extends Game { |
public class Tictactoe extends Game { |
||||
|
|
||||
enum State { |
|
||||
CIRCLE, |
|
||||
CROSS, |
|
||||
EMPTY |
|
||||
} |
|
||||
private String input; |
private String input; |
||||
private State[] currentBoard; |
|
||||
|
private Board currentBoard; |
||||
|
private boolean crossTurn; |
||||
|
|
||||
public Tictactoe() { |
public Tictactoe() { |
||||
init(); |
init(); |
||||
} |
} |
||||
|
|
||||
private void init() { |
private void init() { |
||||
currentBoard = newBoard(); |
|
||||
|
crossTurn = true; |
||||
|
currentBoard = new Board(); |
||||
} |
} |
||||
|
|
||||
@Override |
@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]) +" "); |
|
||||
} |
|
||||
} |
} |
@ -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); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue