changed method checkForWin and created method checkWinVariants

This commit is contained in:
Leonardo Brandenberger 2021-10-08 08:26:39 +02:00
parent 96a94aa12d
commit 2bbd349988
1 changed files with 33 additions and 10 deletions

View File

@ -8,7 +8,7 @@
public class Game { public class Game {
private Gamefield gamefield; private Gamefield gamefield;
Language language; private Language language;
private int player1 = 1; private int player1 = 1;
private int player2 = 2; private int player2 = 2;
@ -42,7 +42,10 @@ public class Game {
gamefield.setField(field, player1); gamefield.setField(field, player1);
// Hier überprüfen, ob der Spieler gewonnen hat if(checkForWin()){
// Hier überprüfen, ob der Spieler gewonnen hat
language.outputWinnerText(playerPlaying);
}
switchPlayer(); switchPlayer();
} }
@ -61,34 +64,54 @@ public class Game {
} }
} }
/**
* Diese Methode ändert die Spielsprache
* @param language Spielsprache welche zur Verfügung stehen "de","en","vn"
*/
public void changeLanguage(String language) { public void changeLanguage(String language) {
this.language.setOutputLanguage(language); this.language.setOutputLanguage(language);
} }
/**
* Methode : checkForWin
*
* Diese Methode prüft ob das Spiel einen Gewinner hat
* @return bei erkanntem Gewinner true sonst false
*/
public boolean checkForWin() { public boolean checkForWin() {
if((gamefield.getField(1) == gamefield.getField(2)) && (gamefield.getField(3) == gamefield.getField(1))){ if (checkWinVariants(1,4,9)) {
return true; return true;
} }
else if((gamefield.getField(4) == gamefield.getField(5)) && (gamefield.getField(6) == gamefield.getField(4))){ else if (checkWinVariants(2,5,8)) {
return true; return true;
} }
else if((gamefield.getField(7) == gamefield.getField(8)) && (gamefield.getField(9) == gamefield.getField(7))){ else if (checkWinVariants(3,6,9)) {
return true; return true;
} }
else if((gamefield.getField(1) == gamefield.getField(4)) && (gamefield.getField(7) == gamefield.getField(1))){ else if (checkWinVariants(1,2,3)) {
return true; return true;
} }
else if((gamefield.getField(2) == gamefield.getField(5)) && (gamefield.getField(8) == gamefield.getField(2))){ else if (checkWinVariants(4,5,6)) {
return true; return true;
} }
else if((gamefield.getField(3) == gamefield.getField(6)) && (gamefield.getField(9) == gamefield.getField(3))){ else if (checkWinVariants(7,8,9)) {
return true; return true;
} }
else if((gamefield.getField(1) == gamefield.getField(5)) && (gamefield.getField(9) == gamefield.getField(1))){ else if (checkWinVariants(1,5,9)) {
return true; return true;
} }
else if((gamefield.getField(3) == gamefield.getField(5)) && (gamefield.getField(7) == gamefield.getField(3))){ else if (checkWinVariants(3,5,7)) {
return true; return true;
} }
return false; return false;
} }
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;
}
}
} }