From 5da16bb1d54090c4c0da42e72934ef0fc5cc4fe1 Mon Sep 17 00:00:00 2001 From: MikeZyeman Date: Sat, 9 Oct 2021 12:31:56 +0200 Subject: [PATCH] Game.java + Implementented method PlaceMethod completely + Implemented method CheckForTie * Changed checkForWin into else Language.java + Added outputSeparator() + Added outputGameStartedText() + Added outputMoveText() GameTest.java Added Tests to catch System out printlns --- src/Game.java | 34 +++++++++++++------ src/Language.java | 33 ++++++++++++++++++ src/TicTacToe.java | 17 +++++++--- test/GameTest.java | 85 ++++++++++++++++++++++++++++++++++------------ 4 files changed, 133 insertions(+), 36 deletions(-) diff --git a/src/Game.java b/src/Game.java index 5627f69..d3165ee 100644 --- a/src/Game.java +++ b/src/Game.java @@ -15,12 +15,13 @@ public class Game { private int player2 = 2; private int playerPlaying = 1; + // private boolean gameFinished = false; public Game() { gamefield = new Gamefield(); language = new Language("de"); - + language.outputGameStartedText(); } /** @@ -33,20 +34,26 @@ public class Game { * @param field vom Spieler markiertes Feld */ public void placeField(int field) { + language.outputSeparator(); if (gameFinished) { - // Fehler aufrufen, dass das Spiel zuende ist. - } - - if (field > 9 || field < 1) { - // Fehler aufrufen von Klasse Sprache + language.outputGameOverText(); return; } - gamefield.setField(field, player1); + if (!(field > 9 || field < 1) && !gamefield.setField(field, playerPlaying)) { + language.outputWrongFieldSelected(); + return; + } - if(checkForWin()){ - // Hier überprüfen, ob der Spieler gewonnen hat + language.outputMoveText(gamefield.countSetFields(), playerPlaying); + gamefield.outputField(); + + if(checkForWin()) { language.outputWinnerText(playerPlaying); + gameFinished = true; + } else if (checkForTie()) { + language.outputGameOverText(); + gameFinished = true; } switchPlayer(); @@ -56,7 +63,8 @@ public class Game { * Methode: switchPlayer * * Diese Methode wechselt den Spieler. Dieser wird von - * Methode placeField aufgerufen + * Methode placeField aufgerufen. + * Der Spieler wird automatisch gewechselt. */ public void switchPlayer() { if (playerPlaying == 1) { @@ -125,4 +133,10 @@ public class Game { (gamefield.getField(field1) >= 1)); } + public boolean checkForTie() { + return gamefield.countSetFields() == 9; + } + + + } diff --git a/src/Language.java b/src/Language.java index 68258c9..a67b607 100644 --- a/src/Language.java +++ b/src/Language.java @@ -49,6 +49,39 @@ public class Language { return false; } + /** + * Methode: outputGameStartedText + * Diese Methode gibt den Text für den SpielStart aus. + */ + public void outputGameStartedText() { + System.out.println(checkLanguage( + "Spiel hat gestartet. Mögen der Bessere gewinnen!", + "Game has started. May the best win!", + "Trò chơi đã bắt đầu. Chúc cho chiến thắng tốt nhất!" + )); + } + + /** + * Methode: outputMove + * Diese Methode gibt die Anzahl Spielzüge aus, die im Spiel bereits gemacht worden sind, und + * welcher Spieler den Zug macht + */ + public void outputMoveText(int move, int player) { + System.out.println(checkLanguage( + "Spiezug: " + move + ", Spieler " + player + " spielt.", + "Move: " + move + ", Player " + player + "plays", + "Di chuyển: " + move + ", Người " + player + " chơi" + )); + } + + /** + * Methode: outputSeparator + * Diese Methode gibt die Anzahl Züge aus, die im Spiel bereits gemacht worden sind. + */ + public void outputSeparator() { + System.out.println("____________\n"); + } + /** * Methode: outputWinnerText * Diese Methode gibt den Gewinnertext über eine Konsolenausgabe aus. diff --git a/src/TicTacToe.java b/src/TicTacToe.java index aa510ed..3a02c70 100644 --- a/src/TicTacToe.java +++ b/src/TicTacToe.java @@ -1,9 +1,18 @@ public class TicTacToe { public static void main(String[] args) { - Gamefield gamefield = new Gamefield(); - gamefield.setField(1, 1); - gamefield.setField(7, 2); - gamefield.outputField(); + Game g1 = new Game(); + + g1.placeField(1); + g1.placeField(2); + g1.placeField(3); + g1.placeField(5); + g1.placeField(4); + g1.placeField(6); + g1.placeField(8); + g1.placeField(7); + g1.placeField(9); + + } } diff --git a/test/GameTest.java b/test/GameTest.java index 6de9bc4..06b61e5 100644 --- a/test/GameTest.java +++ b/test/GameTest.java @@ -1,36 +1,77 @@ -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; +import org.junit.Assert; +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; - +/** + * Klasse GameTest + * + * Diese Klasse beinhaltet und führt alle Testfälle für die Klasse GameTest aus + * + */ class GameTest { - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); - private final PrintStream originalOut = System.out; - private final PrintStream originalErr = System.err; - - @Before - public void setUpStreams() { - System.setOut(new PrintStream(outContent)); - System.setErr(new PrintStream(errContent)); + private final PrintStream standardOut = System.out; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + private Game game = new Game(); + + @BeforeEach + public void beforeEach() { + game = new Game(); + System.setOut(new PrintStream(outputStreamCaptor)); } - @After - public void restoreStreams() { - System.setOut(originalOut); - System.setErr(originalErr); + @AfterEach + public void tearDown() { + System.setOut(standardOut); } + + // Konstruktive Testfälle + + /** + * + */ + @Test + public void placeOneField() { + game.placeField(1); + + } + + @Test - void checkTest() { - System.out.println("hello"); - assertEquals("hello", outContent.toString()); + public void testGameShouldbeTie() { + game.placeField(1); + game.placeField(2); + game.placeField(3); + game.placeField(5); + game.placeField(4); + game.placeField(6); + game.placeField(8); + game.placeField(7); + game.placeField(9); } + + + // Destruktive Testfälle + + @Test + public void placeNotExistingField() { + + System.out.println("Hello Baeldung Readers!!"); + System.out.println("Hello Baeldung Readers!!"); + System.out.println("Hello Baeldung Readers!!"); + System.out.println("Hello Baeldung Readers!!"); + + Assert.assertEquals("Hello Baeldung Readers!!\n" + + "Hello Baeldung Readers!!\n" + + "Hello Baeldung Readers!!\n" + + "Hello Baeldung Readers!!", outputStreamCaptor.toString() + .trim()); + } + } \ No newline at end of file