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
This commit is contained in:
parent
7dfa002605
commit
5da16bb1d5
|
@ -15,12 +15,13 @@ public class Game {
|
||||||
private int player2 = 2;
|
private int player2 = 2;
|
||||||
private int playerPlaying = 1;
|
private int playerPlaying = 1;
|
||||||
|
|
||||||
|
//
|
||||||
private boolean gameFinished = false;
|
private boolean gameFinished = false;
|
||||||
|
|
||||||
public Game() {
|
public Game() {
|
||||||
gamefield = new Gamefield();
|
gamefield = new Gamefield();
|
||||||
language = new Language("de");
|
language = new Language("de");
|
||||||
|
language.outputGameStartedText();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,20 +34,26 @@ public class Game {
|
||||||
* @param field vom Spieler markiertes Feld
|
* @param field vom Spieler markiertes Feld
|
||||||
*/
|
*/
|
||||||
public void placeField(int field) {
|
public void placeField(int field) {
|
||||||
|
language.outputSeparator();
|
||||||
if (gameFinished) {
|
if (gameFinished) {
|
||||||
// Fehler aufrufen, dass das Spiel zuende ist.
|
language.outputGameOverText();
|
||||||
}
|
|
||||||
|
|
||||||
if (field > 9 || field < 1) {
|
|
||||||
// Fehler aufrufen von Klasse Sprache
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
gamefield.setField(field, player1);
|
if (!(field > 9 || field < 1) && !gamefield.setField(field, playerPlaying)) {
|
||||||
|
language.outputWrongFieldSelected();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
language.outputMoveText(gamefield.countSetFields(), playerPlaying);
|
||||||
|
gamefield.outputField();
|
||||||
|
|
||||||
if(checkForWin()) {
|
if(checkForWin()) {
|
||||||
// Hier überprüfen, ob der Spieler gewonnen hat
|
|
||||||
language.outputWinnerText(playerPlaying);
|
language.outputWinnerText(playerPlaying);
|
||||||
|
gameFinished = true;
|
||||||
|
} else if (checkForTie()) {
|
||||||
|
language.outputGameOverText();
|
||||||
|
gameFinished = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
switchPlayer();
|
switchPlayer();
|
||||||
|
@ -56,7 +63,8 @@ public class Game {
|
||||||
* Methode: switchPlayer
|
* Methode: switchPlayer
|
||||||
*
|
*
|
||||||
* Diese Methode wechselt den Spieler. Dieser wird von
|
* Diese Methode wechselt den Spieler. Dieser wird von
|
||||||
* Methode placeField aufgerufen
|
* Methode placeField aufgerufen.
|
||||||
|
* Der Spieler wird automatisch gewechselt.
|
||||||
*/
|
*/
|
||||||
public void switchPlayer() {
|
public void switchPlayer() {
|
||||||
if (playerPlaying == 1) {
|
if (playerPlaying == 1) {
|
||||||
|
@ -125,4 +133,10 @@ public class Game {
|
||||||
(gamefield.getField(field1) >= 1));
|
(gamefield.getField(field1) >= 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean checkForTie() {
|
||||||
|
return gamefield.countSetFields() == 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,39 @@ public class Language {
|
||||||
return false;
|
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
|
* Methode: outputWinnerText
|
||||||
* Diese Methode gibt den Gewinnertext über eine Konsolenausgabe aus.
|
* Diese Methode gibt den Gewinnertext über eine Konsolenausgabe aus.
|
||||||
|
|
|
@ -1,9 +1,18 @@
|
||||||
public class TicTacToe {
|
public class TicTacToe {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Gamefield gamefield = new Gamefield();
|
Game g1 = new Game();
|
||||||
gamefield.setField(1, 1);
|
|
||||||
gamefield.setField(7, 2);
|
g1.placeField(1);
|
||||||
gamefield.outputField();
|
g1.placeField(2);
|
||||||
|
g1.placeField(3);
|
||||||
|
g1.placeField(5);
|
||||||
|
g1.placeField(4);
|
||||||
|
g1.placeField(6);
|
||||||
|
g1.placeField(8);
|
||||||
|
g1.placeField(7);
|
||||||
|
g1.placeField(9);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,77 @@
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import org.junit.Assert;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.After;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse GameTest
|
||||||
|
*
|
||||||
|
* Diese Klasse beinhaltet und führt alle Testfälle für die Klasse GameTest aus
|
||||||
|
*
|
||||||
|
*/
|
||||||
class GameTest {
|
class GameTest {
|
||||||
|
|
||||||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
private final PrintStream standardOut = System.out;
|
||||||
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||||
private final PrintStream originalOut = System.out;
|
|
||||||
private final PrintStream originalErr = System.err;
|
|
||||||
|
|
||||||
@Before
|
private Game game = new Game();
|
||||||
public void setUpStreams() {
|
|
||||||
System.setOut(new PrintStream(outContent));
|
@BeforeEach
|
||||||
System.setErr(new PrintStream(errContent));
|
public void beforeEach() {
|
||||||
|
game = new Game();
|
||||||
|
System.setOut(new PrintStream(outputStreamCaptor));
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@AfterEach
|
||||||
public void restoreStreams() {
|
public void tearDown() {
|
||||||
System.setOut(originalOut);
|
System.setOut(standardOut);
|
||||||
System.setErr(originalErr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Konstruktive Testfälle
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void placeOneField() {
|
||||||
|
game.placeField(1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void checkTest() {
|
public void testGameShouldbeTie() {
|
||||||
System.out.println("hello");
|
game.placeField(1);
|
||||||
assertEquals("hello", outContent.toString());
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue