Browse Source

Added Cli class for centralized I/O management and getPrintStream() method with test.

Updated other classes for Cli class integration.
feature-cli
Nick Stolbov 3 years ago
parent
commit
2ab0579f3c
  1. 16
      src/main/java/Application/Cli.java
  2. 10
      src/main/java/Application/Menu.java
  3. 4
      src/main/java/Application/MenuManager.java
  4. 6
      src/main/java/Game/Game.java
  5. 36
      src/main/java/Game/TicTacToe/Board.java
  6. 9
      src/main/java/Main.java
  7. 35
      src/test/java/Application/CliTest.java
  8. 2
      src/test/java/Application/MenuManagerTest.java
  9. 4
      src/test/java/Application/MenuTest.java
  10. 29
      src/test/java/Game/GameTest.java
  11. 2
      src/test/java/Game/TicTacToe/BoardTest.java
  12. 7
      src/test/java/Game/TictactoeTest.java

16
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;
}
}

10
src/main/java/Application/Menu.java

@ -8,13 +8,13 @@ public class Menu {
private Menu previousMenu; private Menu previousMenu;
private ArrayList<Menu> subMenuList; private ArrayList<Menu> subMenuList;
public Menu(String name){
public Menu(String name) {
setName(name); setName(name);
setPreviousMenu(null); setPreviousMenu(null);
subMenuList = new ArrayList<>(); subMenuList = new ArrayList<>();
} }
public void addMenu(Menu menu){
public void addMenu(Menu menu) {
menu.setPreviousMenu(this); menu.setPreviousMenu(this);
subMenuList.add(menu); subMenuList.add(menu);
} }
@ -27,7 +27,7 @@ public class Menu {
this.name = name; this.name = name;
} }
public Menu getMenu(int i){
public Menu getMenu(int i) {
return getSubMenuList().get(i); return getSubMenuList().get(i);
} }
@ -35,11 +35,11 @@ public class Menu {
return this.subMenuList; return this.subMenuList;
} }
private void setPreviousMenu(Menu menu){
private void setPreviousMenu(Menu menu) {
this.previousMenu = menu; this.previousMenu = menu;
} }
public Menu getPreviousMenu(){
public Menu getPreviousMenu() {
return this.previousMenu; return this.previousMenu;
} }
} }

4
src/main/java/Application/MenuManager.java

@ -17,13 +17,13 @@ public class MenuManager {
} }
public int getSize() { public int getSize() {
if(inRootMenu())
if (inRootMenu())
return menuList.size(); return menuList.size();
return currentMenu.getSubMenuList().size(); return currentMenu.getSubMenuList().size();
} }
public void select(int i) { public void select(int i) {
if(i < 0 || i >= this.getSize())
if (i < 0 || i >= this.getSize())
return; return;
if (currentMenu == null) if (currentMenu == null)
this.currentMenu = menuList.get(i); this.currentMenu = menuList.get(i);

6
src/main/java/Game/Game.java

@ -1,5 +1,7 @@
package Game; package Game;
import Application.Cli;
import java.util.ArrayList; import java.util.ArrayList;
public abstract class Game { public abstract class Game {
@ -9,9 +11,9 @@ public abstract class Game {
public abstract void update(String input); public abstract void update(String input);
public void print() {
public void print(Cli cli) {
for (String s : outputBuffer) { for (String s : outputBuffer) {
System.out.println(s);
cli.getPrintStream().println(s);
} }
} }

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

@ -1,7 +1,6 @@
package Game.TicTacToe; package Game.TicTacToe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class Board { public class Board {
@ -28,18 +27,17 @@ public class Board {
states[i] = State.EMPTY; states[i] = State.EMPTY;
} }
winPatterns = new ArrayList<>(); 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) { public static char getStatedChar(State state) {
switch (state) { switch (state) {
case CIRCLE: case CIRCLE:
@ -55,15 +53,15 @@ public class Board {
public boolean setCellState(int cell, boolean cross) { public boolean setCellState(int cell, boolean cross) {
if (cell <= 9 && cell >= 1) { if (cell <= 9 && cell >= 1) {
if (this.states[cell-1] != State.EMPTY) {
if (this.states[cell - 1] != State.EMPTY) {
return false; return false;
} }
if (cross) { if (cross) {
this.states[cell-1] = State.CROSS;
this.states[cell - 1] = State.CROSS;
} else { } else {
this.states[cell-1] = State.CIRCLE;
this.states[cell - 1] = State.CIRCLE;
} }
}else {
} else {
return false; return false;
} }
return true; return true;
@ -83,15 +81,15 @@ public class Board {
public ArrayList<String> getOutputBoard() { public ArrayList<String> getOutputBoard() {
ArrayList<String> outputBoard = new ArrayList<>(); ArrayList<String> outputBoard = new ArrayList<>();
outputBoard.add("1 ║2 ║3"); 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("═════╬═════╬═════");
outputBoard.add("4 ║5 ║6"); 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("═════╬═════╬═════");
outputBoard.add("7 ║8 ║9"); 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; return outputBoard;
} }
@ -105,7 +103,7 @@ public class Board {
if (!(o instanceof Board)) { if (!(o instanceof Board)) {
return false; return false;
} }
Board x = (Board)o;
Board x = (Board) o;
for (int i = 0; i < x.getStates().length; i++) { for (int i = 0; i < x.getStates().length; i++) {
if (this.getStates()[i] != x.getStates()[i]) { if (this.getStates()[i] != x.getStates()[i]) {
return false; return false;
@ -156,7 +154,7 @@ public class Board {
State[] stateArray = new State[9]; State[] stateArray = new State[9];
for (int i = 0; i < temp.length; i++) { for (int i = 0; i < temp.length; i++) {
switch(temp[i]) {
switch (temp[i]) {
case 1: case 1:
stateArray[i] = State.CIRCLE; stateArray[i] = State.CIRCLE;
break; break;

9
src/main/java/Main.java

@ -1,17 +1,18 @@
import Application.Cli;
import Game.Tictactoe; import Game.Tictactoe;
import java.util.Scanner; import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args){
System.out.println("Hello world!");
public static void main(String[] args) {
Tictactoe ttt = new Tictactoe(); Tictactoe ttt = new Tictactoe();
Cli cli = new Cli(System.out);
Scanner scan = new Scanner(System.in); Scanner scan = new Scanner(System.in);
ttt.print();
ttt.print(cli);
while (scan.hasNext()) { while (scan.hasNext()) {
ttt.update(scan.next()); ttt.update(scan.next());
ttt.print();
ttt.print(cli);
} }
} }
} }

35
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", ""));
}
}

2
src/test/java/Application/MenuManagerTest.java

@ -78,7 +78,7 @@ class MenuManagerTest {
} }
@Test @Test
void getFormattedMenuList(){
void getFormattedMenuList() {
assertEquals(mm.getFormattedMenuList(), "1: Menu1\n2: Menu2\n"); assertEquals(mm.getFormattedMenuList(), "1: Menu1\n2: Menu2\n");
mm.select(0); mm.select(0);
assertEquals(mm.getFormattedMenuList(), "1: Menu1.1\n"); assertEquals(mm.getFormattedMenuList(), "1: Menu1.1\n");

4
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.BeforeEach;
import org.junit.jupiter.api.Test; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -39,7 +37,7 @@ class MenuTest {
} }
@Test @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")); assertTrue(rootMenu.getPreviousMenu() == null && rootMenu.getMenu(0).getPreviousMenu().getName().equals("Root") && rootMenu.getMenu(0).getMenu(0).getPreviousMenu().getName().equals("Menu1"));
} }

29
src/test/java/Game/GameTest.java

@ -2,42 +2,17 @@ package Game;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; 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 { class GameTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Game game;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
System.setOut(new PrintStream(outContent));
game = mock(Game.class, Mockito.CALLS_REAL_METHODS);
} }
@AfterEach @AfterEach
void tearDown() { 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", ""));
} }
} }

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

@ -1,6 +1,5 @@
package Game.TicTacToe; package Game.TicTacToe;
import Game.Tictactoe;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -9,7 +8,6 @@ import org.mockito.internal.util.reflection.FieldSetter;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class BoardTest { class BoardTest {

7
src/test/java/Game/TictactoeTest.java

@ -2,13 +2,6 @@ package Game;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; 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 { class TictactoeTest {

Loading…
Cancel
Save