2021-10-07 21:30:49 +02:00
|
|
|
/**
|
|
|
|
* Klasse: Game
|
|
|
|
*
|
|
|
|
* Diese Klasse beinhaltet die Logik für das Spiel.
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
Language language;
|
|
|
|
|
|
|
|
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-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-07 21:30:49 +02:00
|
|
|
// Hier überprüfen, ob der Spieler gewonnen hat
|
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-07 17:24:24 +02:00
|
|
|
public void changeLanguage(String language) {
|
|
|
|
this.language.setOutputLanguage(language);
|
|
|
|
}
|
2021-10-07 22:18:34 +02:00
|
|
|
public boolean checkForWin() {
|
|
|
|
if((gamefield.getField(1) == gamefield.getField(2)) && (gamefield.getField(3) == gamefield.getField(1))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if((gamefield.getField(4) == gamefield.getField(5)) && (gamefield.getField(6) == gamefield.getField(4))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if((gamefield.getField(7) == gamefield.getField(8)) && (gamefield.getField(9) == gamefield.getField(7))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if((gamefield.getField(1) == gamefield.getField(4)) && (gamefield.getField(7) == gamefield.getField(1))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if((gamefield.getField(2) == gamefield.getField(5)) && (gamefield.getField(8) == gamefield.getField(2))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if((gamefield.getField(3) == gamefield.getField(6)) && (gamefield.getField(9) == gamefield.getField(3))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if((gamefield.getField(1) == gamefield.getField(5)) && (gamefield.getField(9) == gamefield.getField(1))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if((gamefield.getField(3) == gamefield.getField(5)) && (gamefield.getField(7) == gamefield.getField(3))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-07 16:56:04 +02:00
|
|
|
}
|