2021-10-07 21:30:49 +02:00
|
|
|
/**
|
|
|
|
* Klasse: Game
|
|
|
|
*
|
|
|
|
* Diese Klasse beinhaltet die Logik für das Spiel.
|
2021-10-08 09:15:24 +02:00
|
|
|
* Der Benutzer kann mit dieser Klasse die Felder des Spielfeldes setzen
|
2021-10-07 21:30:49 +02:00
|
|
|
*
|
|
|
|
* Klasse Language und Gamefield wird im Konstruktor initialisiert
|
|
|
|
*/
|
2021-10-07 16:56:04 +02:00
|
|
|
public class Game {
|
2021-10-07 17:47:59 +02:00
|
|
|
|
|
|
|
private Gamefield gamefield;
|
2021-10-08 08:26:39 +02:00
|
|
|
private Language language;
|
2021-10-07 17:47:59 +02:00
|
|
|
|
2021-10-14 14:34:12 +02:00
|
|
|
private int player1 = 1;
|
|
|
|
private int player2 = 2;
|
2021-10-07 21:30:49 +02:00
|
|
|
private int playerPlaying = 1;
|
2021-10-07 17:24:24 +02:00
|
|
|
|
2021-10-07 21:30:49 +02:00
|
|
|
private boolean gameFinished = false;
|
2021-10-07 17:24:24 +02:00
|
|
|
|
|
|
|
public Game() {
|
2021-10-07 17:47:59 +02:00
|
|
|
gamefield = new Gamefield();
|
|
|
|
language = new Language("de");
|
2021-10-09 12:31:56 +02:00
|
|
|
language.outputGameStartedText();
|
2021-10-07 17:47:59 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 21:30:49 +02:00
|
|
|
/**
|
|
|
|
* Methode: placeField
|
|
|
|
*
|
|
|
|
* Spieler setzt sein Symbol in das gewünschte Feld.
|
|
|
|
* Die Methode überprüft auch, ob der Spieler gewonnen hat.
|
|
|
|
* Wenn der Spieler gewonnen hat, dann wird
|
|
|
|
*
|
|
|
|
* @param field vom Spieler markiertes Feld
|
|
|
|
*/
|
2021-10-07 17:47:59 +02:00
|
|
|
public void placeField(int field) {
|
2021-10-09 15:57:54 +02:00
|
|
|
gamefield.outputSeparator();
|
2021-10-07 21:30:49 +02:00
|
|
|
if (gameFinished) {
|
2021-10-09 12:31:56 +02:00
|
|
|
language.outputGameOverText();
|
|
|
|
return;
|
2021-10-07 21:30:49 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 09:39:24 +02:00
|
|
|
if ((field > 9 || field < 1) || !gamefield.setField(field, playerPlaying)) {
|
2021-10-09 12:31:56 +02:00
|
|
|
language.outputWrongFieldSelected();
|
2021-10-07 17:47:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-09 12:31:56 +02:00
|
|
|
language.outputMoveText(gamefield.countSetFields(), playerPlaying);
|
|
|
|
gamefield.outputField();
|
2021-10-07 17:47:59 +02:00
|
|
|
|
2021-10-09 12:31:56 +02:00
|
|
|
if(checkForWin()) {
|
2021-10-08 09:15:24 +02:00
|
|
|
language.outputWinnerText(playerPlaying);
|
2021-10-09 15:50:38 +02:00
|
|
|
language.outputGameOverText();
|
2021-10-09 12:31:56 +02:00
|
|
|
gameFinished = true;
|
|
|
|
} else if (checkForTie()) {
|
2021-10-09 15:44:36 +02:00
|
|
|
language.outputTieText();
|
2021-10-09 12:31:56 +02:00
|
|
|
language.outputGameOverText();
|
|
|
|
gameFinished = true;
|
2021-10-08 08:26:39 +02:00
|
|
|
}
|
2021-10-07 21:30:49 +02:00
|
|
|
switchPlayer();
|
2021-10-07 17:47:59 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 21:30:49 +02:00
|
|
|
/**
|
|
|
|
* Methode: switchPlayer
|
|
|
|
*
|
|
|
|
* Diese Methode wechselt den Spieler. Dieser wird von
|
2021-10-09 12:31:56 +02:00
|
|
|
* Methode placeField aufgerufen.
|
|
|
|
* Der Spieler wird automatisch gewechselt.
|
2021-10-07 21:30:49 +02:00
|
|
|
*/
|
2021-10-09 15:50:38 +02:00
|
|
|
private void switchPlayer() {
|
2021-10-07 21:30:49 +02:00
|
|
|
if (playerPlaying == 1) {
|
|
|
|
playerPlaying = player2;
|
|
|
|
} else {
|
|
|
|
playerPlaying = player1;
|
|
|
|
}
|
2021-10-07 17:24:24 +02:00
|
|
|
}
|
2021-10-07 17:47:59 +02:00
|
|
|
|
2021-10-08 08:26:39 +02:00
|
|
|
/**
|
|
|
|
* Diese Methode ändert die Spielsprache
|
|
|
|
* @param language Spielsprache welche zur Verfügung stehen "de","en","vn"
|
|
|
|
*/
|
2021-10-07 17:24:24 +02:00
|
|
|
public void changeLanguage(String language) {
|
2021-10-15 08:24:21 +02:00
|
|
|
this.gamefield.outputSeparator();
|
2021-10-07 17:24:24 +02:00
|
|
|
this.language.setOutputLanguage(language);
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-08 10:57:32 +02:00
|
|
|
* Methode: checkForWin
|
2021-10-08 08:26:39 +02:00
|
|
|
*
|
|
|
|
* Diese Methode prüft ob das Spiel einen Gewinner hat
|
|
|
|
* @return bei erkanntem Gewinner true sonst false
|
2021-10-15 00:21:52 +02:00
|
|
|
*
|
2021-10-08 08:26:39 +02:00
|
|
|
*/
|
2021-10-08 10:57:32 +02:00
|
|
|
private boolean checkForWin() {
|
2021-10-15 00:21:52 +02:00
|
|
|
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);
|
2021-10-07 22:18:34 +02:00
|
|
|
}
|
2021-10-08 10:57:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Methode: checkWinVariants
|
|
|
|
*
|
|
|
|
* Diese Methode prüft ob 3 Werte denselben entsprechen und grösser als 0 sind.
|
|
|
|
* Methode wird von checkForWin Methode verwendet.
|
|
|
|
*
|
|
|
|
* @param field1 erstes zu prüfende Feld
|
|
|
|
* @param field2 zweites zu prüfende Feld
|
|
|
|
* @param field3 drittes zu prüfende Feld
|
|
|
|
* @return bei erkannten gleichen Zahlen grösser als 0 true sonst false
|
|
|
|
*/
|
2021-10-08 08:26:39 +02:00
|
|
|
private boolean checkWinVariants(int field1, int field2, int field3) {
|
2021-10-08 10:57:32 +02:00
|
|
|
return ((gamefield.getField(field1) == gamefield.getField(field2)) &&
|
|
|
|
(gamefield.getField(field3) == gamefield.getField(field1)) &&
|
2021-10-11 09:39:24 +02:00
|
|
|
(gamefield.getField(field1) == playerPlaying));
|
2021-10-08 08:26:39 +02:00
|
|
|
}
|
2021-10-08 09:15:24 +02:00
|
|
|
|
2021-10-09 15:57:54 +02:00
|
|
|
/**
|
|
|
|
* Methode: chechForTie
|
|
|
|
*
|
|
|
|
* Diese Methode prüft ob bereits alle Felder von den Spielern belegt wurden.
|
|
|
|
* @return falls Unentschieden true sonst false
|
|
|
|
*/
|
|
|
|
|
2021-10-09 15:50:38 +02:00
|
|
|
private boolean checkForTie() {
|
2021-10-09 12:31:56 +02:00
|
|
|
return gamefield.countSetFields() == 9;
|
|
|
|
}
|
2021-10-07 16:56:04 +02:00
|
|
|
}
|