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
|
|
|
|
|
|
|
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-08 10:41:55 +02:00
|
|
|
|
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-07 21:30:49 +02:00
|
|
|
if (gameFinished) {
|
|
|
|
// Fehler aufrufen, dass das Spiel zuende ist.
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:47:59 +02:00
|
|
|
if (field > 9 || field < 1) {
|
|
|
|
// Fehler aufrufen von Klasse Sprache
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gamefield.setField(field, player1);
|
|
|
|
|
2021-10-08 08:26:39 +02:00
|
|
|
if(checkForWin()){
|
|
|
|
// Hier überprüfen, ob der Spieler gewonnen hat
|
2021-10-08 09:15:24 +02:00
|
|
|
language.outputWinnerText(playerPlaying);
|
2021-10-08 08:26:39 +02:00
|
|
|
}
|
2021-10-07 17:47:59 +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
|
|
|
|
* Methode placeField aufgerufen
|
|
|
|
*/
|
2021-10-07 17:47:59 +02:00
|
|
|
public 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) {
|
|
|
|
this.language.setOutputLanguage(language);
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Methode : checkForWin
|
|
|
|
*
|
|
|
|
* Diese Methode prüft ob das Spiel einen Gewinner hat
|
|
|
|
* @return bei erkanntem Gewinner true sonst false
|
|
|
|
*/
|
2021-10-07 22:18:34 +02:00
|
|
|
public boolean checkForWin() {
|
2021-10-08 08:26:39 +02:00
|
|
|
if (checkWinVariants(1,4,9)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
else if (checkWinVariants(2,5,8)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
else if (checkWinVariants(3,6,9)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
else if (checkWinVariants(1,2,3)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
else if (checkWinVariants(4,5,6)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
else if (checkWinVariants(7,8,9)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
else if (checkWinVariants(1,5,9)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
else if (checkWinVariants(3,5,7)) {
|
2021-10-07 22:18:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-08 08:26:39 +02:00
|
|
|
private boolean checkWinVariants(int field1, int field2, int field3) {
|
|
|
|
if((gamefield.getField(field1) == gamefield.getField(field2)) &&
|
|
|
|
(gamefield.getField(field3) == gamefield.getField(field1)) && (gamefield.getField(field1) >= 1)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 09:15:24 +02:00
|
|
|
|
2021-10-07 16:56:04 +02:00
|
|
|
}
|