Browse Source

changed interface Game to abstract class for better object orientation

feature-game
Dion Aliu 3 years ago
parent
commit
9c7bedcd7c
  1. 21
      src/main/java/Game/Game.java
  2. 14
      src/main/java/Game/Tictactoe.java
  3. 43
      src/test/java/Game/GameTest.java
  4. 17
      src/test/java/Game/TictactoeTest.java

21
src/main/java/Game/Game.java

@ -1,7 +1,24 @@
package Game; package Game;
public interface Game {
public abstract void print();
import java.util.ArrayList;
public abstract class Game {
protected ArrayList<String> outputBuffer = new ArrayList<>();
public void print() {
for (String s : outputBuffer) {
System.out.println(s);
}
}
protected void setOutputBuffer(ArrayList<String> outputBuffer) {
this.outputBuffer = outputBuffer;
}
public ArrayList<String> getOutputBuffer() {
return this.outputBuffer;
}

14
src/main/java/Game/Tictactoe.java

@ -2,7 +2,7 @@ package Game;
import java.util.ArrayList; import java.util.ArrayList;
public class Tictactoe implements Game {
public class Tictactoe extends Game {
enum State { enum State {
@ -10,7 +10,6 @@ public class Tictactoe implements Game {
CROSS, CROSS,
EMPTY EMPTY
} }
private ArrayList<String> outputBuffer = new ArrayList<String>();
private String input; private String input;
private State[] currentBoard; private State[] currentBoard;
@ -22,12 +21,7 @@ public class Tictactoe implements Game {
currentBoard = newBoard(); currentBoard = newBoard();
} }
@Override
public void print() {
for (int i = 0; i < outputBuffer.size(); i++) {
System.out.println(outputBuffer.get(i));
}
}
public char getStatedChar(State state) { public char getStatedChar(State state) {
switch (state) { switch (state) {
@ -50,10 +44,6 @@ public class Tictactoe implements Game {
return board; return board;
} }
void setOutputBuffer(ArrayList<String> outputBuffer){
this.outputBuffer = outputBuffer;
}
public State[] getCurrentBoard() { public State[] getCurrentBoard() {
return currentBoard; return currentBoard;
} }

43
src/test/java/Game/GameTest.java

@ -0,0 +1,43 @@
package Game;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class GameTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Game game;
@BeforeEach
void setUp() {
System.setOut(new PrintStream(outContent));
game = mock(Game.class, Mockito.CALLS_REAL_METHODS);
}
@AfterEach
void tearDown() {
System.setOut(originalOut);
}
@Test
public void print() {
ArrayList<String> testOB = new ArrayList<>();
testOB.add("Hello");
testOB.add("World");
testOB.add("!!!");
game.setOutputBuffer(testOB);
game.print();
assertEquals("Hello\nWorld\n!!!\n", outContent.toString().replaceAll("\r", ""));
}
}

17
src/test/java/Game/TictactoeTest.java

@ -12,35 +12,18 @@ import static org.junit.jupiter.api.Assertions.*;
class TictactoeTest { class TictactoeTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Tictactoe ttt; Tictactoe ttt;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
ttt = new Tictactoe(); ttt = new Tictactoe();
System.setOut(new PrintStream(outContent));
} }
@AfterEach @AfterEach
void tearDown() { void tearDown() {
System.setOut(originalOut);
}
@Test
public void testPrint() {
ArrayList<String> testOB = new ArrayList<String>();
testOB.add("Hello");
testOB.add("World");
testOB.add("!!!");
ttt.setOutputBuffer(testOB);
ttt.print();
assertEquals("Hello\nWorld\n!!!\n", outContent.toString().replaceAll("\r", ""));
} }
@Test @Test
void getStatedChar() { void getStatedChar() {
assertEquals(ttt.getStatedChar(Tictactoe.State.CIRCLE), 'O'); assertEquals(ttt.getStatedChar(Tictactoe.State.CIRCLE), 'O');

Loading…
Cancel
Save