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 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
private final PrintStream standardOut = System.out;
|
||||
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||
|
||||
@Before
|
||||
public void setUpStreams() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
System.setErr(new PrintStream(errContent));
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue