Changed huge if condition chain to a boolean term. Unit tests are being executed correctly
This commit is contained in:
parent
269cc360c3
commit
af08c54c4c
140
README.md
140
README.md
|
@ -11,3 +11,143 @@ wie dieses benötigt, um es laufen lassen zu können.
|
|||
|
||||
Bevorzugt wird die IDE IntelliJ, da mit dieser Entwicklungsumgebung das Programm
|
||||
programmiert worden ist und alle Voraussetzungen bereits gesetzt worden sind für die IDE.
|
||||
|
||||
|
||||
## Tutorial
|
||||
|
||||
Das TicTacToe besteht aus 9 Feldern und wird von zwei Spielern gespielt. Diese zwei Spieler werden mit den Zeichen `X` oder `O` versehen. Spieler werden mit jedem Zug gewechselt.
|
||||
|
||||
Das ist ein Beispiel von einem gespielten TicTacToe Feld.
|
||||
|
||||
| X | | |
|
||||
| --- | --- | --- |
|
||||
| **O** | **X** | |
|
||||
| | | **O** |
|
||||
|
||||
Der Spieler gewinnt, wenn er zuerst 3 von seinen Zeichen in einer vertikalen, horizontalen oder diagonale Reihe gesetzt hat.
|
||||
|
||||
Das Spiel hat folgende zwei Inputs, die zur Bedienung des Spieles dienen.
|
||||
|
||||
Um das TicTacToe spielen zu können, müssen Sie in der `TicTacToe.java` Datei öffnen. Die Datei wird dann so aussehen:
|
||||
|
||||
```java
|
||||
public class TicTacToe {
|
||||
public static void main(String[] args) {
|
||||
Game g1 = new Game();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Feld setzen**
|
||||
|
||||
Um ein Feld setzen zu können, fügen Sie eine neue Zeile hinzu und schreiben Sie
|
||||
|
||||
```java
|
||||
public class TicTacToe {
|
||||
public static void main(String[] args) {
|
||||
Game g1 = new Game();
|
||||
|
||||
g1.placeField( 1 /*Hier das gewünschte Feld */);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Wenn Sie ein Feld gesetzt hat, dann wird im Terminal folgendes ausgegeben:
|
||||
```
|
||||
Spiezug: 1, Spieler 1 spielt.
|
||||
X| |
|
||||
-----
|
||||
| |
|
||||
-----
|
||||
| |
|
||||
```
|
||||
|
||||
*Wenn* ein Feld bereits gesetzt worden ist und der Spieler versucht, diese wieder zu setzen, dann wird im Terminal folgendes ausgegeben
|
||||
|
||||
```
|
||||
Dieses Feld ist nicht zulässig
|
||||
```
|
||||
|
||||
**Sprache wechseln**
|
||||
|
||||
Das Spiel ist in der Lage, in Deutsch(`de`) Englisch(`en`) und Vietnamesisch(`vn`) auszugeben. Um die Sprache ändern zu können, benutzen Sie die Methode `g1.changeLanguage();`
|
||||
|
||||
```java
|
||||
public class TicTacToe {
|
||||
public static void main(String[] args) {
|
||||
Game g1 = new Game();
|
||||
|
||||
/* Hier die gewünschte Sprache als Kürzel und in kleiner Schrift wie hier dargestellt*/
|
||||
g1.changeLanguage("de");
|
||||
g1.changeLanguage("en");
|
||||
g1.changeLanguage("vn");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Spiel neustarten**
|
||||
|
||||
Um ein neues Spiel zu spielen, muss eine neues `Game` Objekt generiert werden. Das Objekt `Game` hat keine Methode, um das Spiel neuzustarten.
|
||||
|
||||
Hier eine Darstellung:
|
||||
|
||||
```java
|
||||
public class TicTacToe {
|
||||
public static void main(String[] args) {
|
||||
Game g1 = new Game();
|
||||
|
||||
/* Neues Spiel */
|
||||
Game g2 = new Game();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Beispiele der Kombinationen für den Sieg
|
||||
|
||||
Vertikale:
|
||||
|
||||
| X | | |
|
||||
| --- | --- | --- |
|
||||
| **X** | **O** | |
|
||||
| **X** | | **O** |
|
||||
|
||||
| X | X | |
|
||||
| --- | --- | --- |
|
||||
| **O** | **X** | |
|
||||
| **O** | **X** | **O** |
|
||||
|
||||
| | O | X |
|
||||
| --- | --- | --- |
|
||||
| **O** | | **X**|
|
||||
| **O** | **X** | **X** |
|
||||
|
||||
Horizontale:
|
||||
|
||||
| X | X | X |
|
||||
| --- | --- | --- |
|
||||
| | **O** | |
|
||||
| | | **O** |
|
||||
|
||||
| | | O |
|
||||
| --- | --- | --- |
|
||||
| **X** | **X** | **X** |
|
||||
| **O** | | **O** |
|
||||
|
||||
| | O | X |
|
||||
| --- | --- | --- |
|
||||
| **O** | | **O**|
|
||||
| **X** | **X** | **X** |
|
||||
|
||||
Diagonale:
|
||||
|
||||
| X | | |
|
||||
| --- | --- | --- |
|
||||
| **O** | **X** | |
|
||||
| **O** | | **X** |
|
||||
|
||||
| | | X |
|
||||
| --- | --- | --- |
|
||||
| **O** | **X** | |
|
||||
| **X** | | **O** |
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ public class Game {
|
|||
private final int player2 = 2;
|
||||
private int playerPlaying = 1;
|
||||
|
||||
//
|
||||
private boolean gameFinished = false;
|
||||
|
||||
public Game() {
|
||||
|
@ -57,7 +56,6 @@ public class Game {
|
|||
language.outputGameOverText();
|
||||
gameFinished = true;
|
||||
}
|
||||
|
||||
switchPlayer();
|
||||
}
|
||||
|
||||
|
@ -89,33 +87,13 @@ public class Game {
|
|||
*
|
||||
* Diese Methode prüft ob das Spiel einen Gewinner hat
|
||||
* @return bei erkanntem Gewinner true sonst false
|
||||
*
|
||||
*/
|
||||
private boolean checkForWin() {
|
||||
if (checkWinVariants(1,4,7)) {
|
||||
return true;
|
||||
}
|
||||
else if (checkWinVariants(2,5,8)) {
|
||||
return true;
|
||||
}
|
||||
else if (checkWinVariants(3,6,9)) {
|
||||
return true;
|
||||
}
|
||||
else if (checkWinVariants(1,2,3)) {
|
||||
return true;
|
||||
}
|
||||
else if (checkWinVariants(4,5,6)) {
|
||||
return true;
|
||||
}
|
||||
else if (checkWinVariants(7,8,9)) {
|
||||
return true;
|
||||
}
|
||||
else if (checkWinVariants(1,5,9)) {
|
||||
return true;
|
||||
}
|
||||
else if (checkWinVariants(3,5,7)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return checkWinVariants(1,4,7) || checkWinVariants(2,5,8) ||
|
||||
checkWinVariants(3,6,9) || checkWinVariants(1,2,3) ||
|
||||
checkWinVariants(4,5,6) || checkWinVariants(7,8,9) ||
|
||||
checkWinVariants(1,5,9) || checkWinVariants(3,5,7);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,17 +20,17 @@ public class TicTacToe {
|
|||
/**
|
||||
* ## Die Bedienung:
|
||||
*
|
||||
* Um diese innovative TicTacToe Edition spielen zu können,
|
||||
* müssen Sie folgende Befehle eingeben.
|
||||
*
|
||||
* Um das TicTacToe spielen zu können, müssen Sie folgende Befehle eingeben:
|
||||
* g.placeField( Hier das gewünschte Feld als Zahl )
|
||||
*
|
||||
* Um die Sprache im Spiel zu wechseln, geben Sie folgendes Befehl ein:
|
||||
* g.changeLanguage("de");
|
||||
*
|
||||
* Um ein neues Spiel starten zu können, initialisieren Sie eine
|
||||
* neue Klasse mit folgendem Befehl:
|
||||
* Game g = new Game();
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Game g1 = new Game();
|
||||
|
||||
g1.placeField(1);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
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;
|
||||
|
||||
/**
|
||||
* Klasse GameTest
|
||||
*
|
||||
|
@ -15,37 +8,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
*/
|
||||
class GameTest {
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
System.setOut(standardOut);
|
||||
}
|
||||
|
||||
|
||||
// Konstruktive Testfälle
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void placeOneField() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGameShouldbeTie() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
game.placeField(2);
|
||||
game.placeField(3);
|
||||
|
@ -57,22 +30,190 @@ class GameTest {
|
|||
game.placeField(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant147() {
|
||||
Game game = new Game();
|
||||
|
||||
// Destruktive Testfälle
|
||||
game.placeField(1);
|
||||
game.placeField(2);
|
||||
game.placeField(4);
|
||||
game.placeField(3);
|
||||
game.placeField(7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeNotExistingField() {
|
||||
public void Player2WinVariant147() {
|
||||
Game game = new Game();
|
||||
|
||||
System.out.println("Hello Baeldung Readers!!");
|
||||
System.out.println("Hello Baeldung Readers!!");
|
||||
System.out.println("Hello Baeldung Readers!!");
|
||||
System.out.println("Hello Baeldung Readers!!");
|
||||
game.placeField(2);
|
||||
game.placeField(1);
|
||||
game.placeField(3);
|
||||
game.placeField(4);
|
||||
game.placeField(6);
|
||||
game.placeField(7);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant123() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
game.placeField(4);
|
||||
game.placeField(2);
|
||||
game.placeField(5);
|
||||
game.placeField(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player2WinVariant123() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(4);
|
||||
game.placeField(1);
|
||||
game.placeField(5);
|
||||
game.placeField(2);
|
||||
game.placeField(7);
|
||||
game.placeField(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant159() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
game.placeField(3);
|
||||
game.placeField(5);
|
||||
game.placeField(2);
|
||||
game.placeField(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player2WinVariant159() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(3);
|
||||
game.placeField(1);
|
||||
game.placeField(2);
|
||||
game.placeField(5);
|
||||
game.placeField(6);
|
||||
game.placeField(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant258() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(2);
|
||||
game.placeField(3);
|
||||
game.placeField(5);
|
||||
game.placeField(4);
|
||||
game.placeField(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player2WinVariant258() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
game.placeField(2);
|
||||
game.placeField(4);
|
||||
game.placeField(5);
|
||||
game.placeField(3);
|
||||
game.placeField(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant369() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(3);
|
||||
game.placeField(1);
|
||||
game.placeField(6);
|
||||
game.placeField(2);
|
||||
game.placeField(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player2WinVariant369() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
game.placeField(3);
|
||||
game.placeField(2);
|
||||
game.placeField(6);
|
||||
game.placeField(4);
|
||||
game.placeField(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant456() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(4);
|
||||
game.placeField(1);
|
||||
game.placeField(5);
|
||||
game.placeField(2);
|
||||
game.placeField(6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player2WinVariant456() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(7);
|
||||
game.placeField(4);
|
||||
game.placeField(1);
|
||||
game.placeField(5);
|
||||
game.placeField(2);
|
||||
game.placeField(6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant789() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(7);
|
||||
game.placeField(2);
|
||||
game.placeField(8);
|
||||
game.placeField(1);
|
||||
game.placeField(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player2WinVariant789() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
game.placeField(7);
|
||||
game.placeField(2);
|
||||
game.placeField(8);
|
||||
game.placeField(4);
|
||||
game.placeField(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player1WinVariant357() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(3);
|
||||
game.placeField(2);
|
||||
game.placeField(5);
|
||||
game.placeField(4);
|
||||
game.placeField(7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Player2WinVariant357() {
|
||||
Game game = new Game();
|
||||
|
||||
game.placeField(1);
|
||||
game.placeField(3);
|
||||
game.placeField(2);
|
||||
game.placeField(5);
|
||||
game.placeField(4);
|
||||
game.placeField(7);
|
||||
|
||||
assertEquals("Hello Baeldung Readers!!\n" +
|
||||
"Hello Baeldung Readers!!\n" +
|
||||
"Hello Baeldung Readers!!\n" +
|
||||
"Hello Baeldung Readers!!", outputStreamCaptor.toString()
|
||||
.trim());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue