diff --git a/src/main/java/Application/Cli.java b/src/main/java/Application/Cli.java new file mode 100644 index 0000000..a2d787d --- /dev/null +++ b/src/main/java/Application/Cli.java @@ -0,0 +1,16 @@ +package Application; + +import java.io.PrintStream; + +public class Cli { + + private PrintStream printStream; + + public Cli(PrintStream printStream) { + this.printStream = printStream; + } + + public PrintStream getPrintStream() { + return this.printStream; + } +} diff --git a/src/main/java/Application/Menu.java b/src/main/java/Application/Menu.java index 0651965..347d66a 100644 --- a/src/main/java/Application/Menu.java +++ b/src/main/java/Application/Menu.java @@ -8,13 +8,13 @@ public class Menu { private Menu previousMenu; private ArrayList subMenuList; - public Menu(String name){ + public Menu(String name) { setName(name); setPreviousMenu(null); subMenuList = new ArrayList<>(); } - public void addMenu(Menu menu){ + public void addMenu(Menu menu) { menu.setPreviousMenu(this); subMenuList.add(menu); } @@ -27,7 +27,7 @@ public class Menu { this.name = name; } - public Menu getMenu(int i){ + public Menu getMenu(int i) { return getSubMenuList().get(i); } @@ -35,11 +35,11 @@ public class Menu { return this.subMenuList; } - private void setPreviousMenu(Menu menu){ + private void setPreviousMenu(Menu menu) { this.previousMenu = menu; } - public Menu getPreviousMenu(){ + public Menu getPreviousMenu() { return this.previousMenu; } } diff --git a/src/main/java/Application/MenuManager.java b/src/main/java/Application/MenuManager.java index 4aa1be5..36a95fc 100644 --- a/src/main/java/Application/MenuManager.java +++ b/src/main/java/Application/MenuManager.java @@ -17,13 +17,13 @@ public class MenuManager { } public int getSize() { - if(inRootMenu()) + if (inRootMenu()) return menuList.size(); return currentMenu.getSubMenuList().size(); } public void select(int i) { - if(i < 0 || i >= this.getSize()) + if (i < 0 || i >= this.getSize()) return; if (currentMenu == null) this.currentMenu = menuList.get(i); diff --git a/src/main/java/Game/Game.java b/src/main/java/Game/Game.java index 39bc4f8..00f398b 100644 --- a/src/main/java/Game/Game.java +++ b/src/main/java/Game/Game.java @@ -1,5 +1,7 @@ package Game; +import Application.Cli; + import java.util.ArrayList; public abstract class Game { @@ -9,9 +11,9 @@ public abstract class Game { public abstract void update(String input); - public void print() { + public void print(Cli cli) { for (String s : outputBuffer) { - System.out.println(s); + cli.getPrintStream().println(s); } } diff --git a/src/main/java/Game/TicTacToe/Board.java b/src/main/java/Game/TicTacToe/Board.java index daaea57..03dd018 100644 --- a/src/main/java/Game/TicTacToe/Board.java +++ b/src/main/java/Game/TicTacToe/Board.java @@ -1,7 +1,6 @@ package Game.TicTacToe; import java.util.ArrayList; -import java.util.Arrays; public class Board { @@ -28,18 +27,17 @@ public class Board { states[i] = State.EMPTY; } winPatterns = new ArrayList<>(); - winPatterns.add(new int[]{1,1,1,0,0,0,0,0,0}); - winPatterns.add(new int[]{0,0,0,1,1,1,0,0,0}); - winPatterns.add(new int[]{0,0,0,0,0,0,1,1,1}); - winPatterns.add(new int[]{1,0,0,1,0,0,1,0,0}); - winPatterns.add(new int[]{0,1,0,0,1,0,0,1,0}); - winPatterns.add(new int[]{0,0,1,0,0,1,0,0,1}); - winPatterns.add(new int[]{0,0,1,0,1,0,1,0,0}); - winPatterns.add(new int[]{1,0,0,0,1,0,0,0,1}); + winPatterns.add(new int[]{1, 1, 1, 0, 0, 0, 0, 0, 0}); + winPatterns.add(new int[]{0, 0, 0, 1, 1, 1, 0, 0, 0}); + winPatterns.add(new int[]{0, 0, 0, 0, 0, 0, 1, 1, 1}); + winPatterns.add(new int[]{1, 0, 0, 1, 0, 0, 1, 0, 0}); + winPatterns.add(new int[]{0, 1, 0, 0, 1, 0, 0, 1, 0}); + winPatterns.add(new int[]{0, 0, 1, 0, 0, 1, 0, 0, 1}); + winPatterns.add(new int[]{0, 0, 1, 0, 1, 0, 1, 0, 0}); + winPatterns.add(new int[]{1, 0, 0, 0, 1, 0, 0, 0, 1}); } - public static char getStatedChar(State state) { switch (state) { case CIRCLE: @@ -55,15 +53,15 @@ public class Board { public boolean setCellState(int cell, boolean cross) { if (cell <= 9 && cell >= 1) { - if (this.states[cell-1] != State.EMPTY) { + if (this.states[cell - 1] != State.EMPTY) { return false; } if (cross) { - this.states[cell-1] = State.CROSS; + this.states[cell - 1] = State.CROSS; } else { - this.states[cell-1] = State.CIRCLE; + this.states[cell - 1] = State.CIRCLE; } - }else { + } else { return false; } return true; @@ -83,19 +81,19 @@ public class Board { 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(" " + 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(" " + 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]) +" "); + outputBoard.add(" " + getStatedChar(states[6]) + " ║ " + getStatedChar(states[7]) + " ║ " + getStatedChar(states[8]) + " "); return outputBoard; } - + public State[] getStates() { return this.states; } @@ -105,7 +103,7 @@ public class Board { if (!(o instanceof Board)) { return false; } - Board x = (Board)o; + Board x = (Board) o; for (int i = 0; i < x.getStates().length; i++) { if (this.getStates()[i] != x.getStates()[i]) { return false; @@ -156,7 +154,7 @@ public class Board { State[] stateArray = new State[9]; for (int i = 0; i < temp.length; i++) { - switch(temp[i]) { + switch (temp[i]) { case 1: stateArray[i] = State.CIRCLE; break; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 89b9c35..cf9689e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,17 +1,18 @@ +import Application.Cli; import Game.Tictactoe; import java.util.Scanner; public class Main { - public static void main(String[] args){ - System.out.println("Hello world!"); + public static void main(String[] args) { Tictactoe ttt = new Tictactoe(); + Cli cli = new Cli(System.out); Scanner scan = new Scanner(System.in); - ttt.print(); + ttt.print(cli); while (scan.hasNext()) { ttt.update(scan.next()); - ttt.print(); + ttt.print(cli); } } } diff --git a/src/test/java/Application/CliTest.java b/src/test/java/Application/CliTest.java new file mode 100644 index 0000000..747cf61 --- /dev/null +++ b/src/test/java/Application/CliTest.java @@ -0,0 +1,35 @@ +package Application; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CliTest { + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + Cli cli; + + @BeforeEach + void setUp() { + System.setOut(new PrintStream(outContent)); + cli = new Cli(System.out); + } + + @AfterEach + void tearDown() { + System.setOut(originalOut); + } + + @Test + void getPrintStream() { + cli.getPrintStream().println("Hello World!"); + assertEquals("Hello World!\n", outContent.toString().replaceAll("\r", "")); + } +} \ No newline at end of file diff --git a/src/test/java/Application/MenuManagerTest.java b/src/test/java/Application/MenuManagerTest.java index 3ad5701..7497abb 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -78,7 +78,7 @@ class MenuManagerTest { } @Test - void getFormattedMenuList(){ + void getFormattedMenuList() { assertEquals(mm.getFormattedMenuList(), "1: Menu1\n2: Menu2\n"); mm.select(0); assertEquals(mm.getFormattedMenuList(), "1: Menu1.1\n"); diff --git a/src/test/java/Application/MenuTest.java b/src/test/java/Application/MenuTest.java index d0494b6..04b677a 100644 --- a/src/test/java/Application/MenuTest.java +++ b/src/test/java/Application/MenuTest.java @@ -4,8 +4,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.ArrayList; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -39,7 +37,7 @@ class MenuTest { } @Test - void getPreviousMenu(){ + void getPreviousMenu() { assertTrue(rootMenu.getPreviousMenu() == null && rootMenu.getMenu(0).getPreviousMenu().getName().equals("Root") && rootMenu.getMenu(0).getMenu(0).getPreviousMenu().getName().equals("Menu1")); } diff --git a/src/test/java/Game/GameTest.java b/src/test/java/Game/GameTest.java index 57c399e..c90b419 100644 --- a/src/test/java/Game/GameTest.java +++ b/src/test/java/Game/GameTest.java @@ -2,42 +2,17 @@ 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 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", "")); } + } \ No newline at end of file diff --git a/src/test/java/Game/TicTacToe/BoardTest.java b/src/test/java/Game/TicTacToe/BoardTest.java index 40218e4..8a5450a 100644 --- a/src/test/java/Game/TicTacToe/BoardTest.java +++ b/src/test/java/Game/TicTacToe/BoardTest.java @@ -1,6 +1,5 @@ package Game.TicTacToe; -import Game.Tictactoe; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -9,7 +8,6 @@ import org.mockito.internal.util.reflection.FieldSetter; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; class BoardTest { diff --git a/src/test/java/Game/TictactoeTest.java b/src/test/java/Game/TictactoeTest.java index 5eb08d8..1dc0fb2 100644 --- a/src/test/java/Game/TictactoeTest.java +++ b/src/test/java/Game/TictactoeTest.java @@ -2,13 +2,6 @@ package Game; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.ArrayList; - -import static org.junit.jupiter.api.Assertions.*; class TictactoeTest {