From 2bbd349988268074fa38fd013ca72c7f010b3f0a Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Fri, 8 Oct 2021 08:26:39 +0200 Subject: [PATCH] changed method checkForWin and created method checkWinVariants --- src/Game.java | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/Game.java b/src/Game.java index 391bba6..024a8cd 100644 --- a/src/Game.java +++ b/src/Game.java @@ -8,7 +8,7 @@ public class Game { private Gamefield gamefield; - Language language; + private Language language; private int player1 = 1; private int player2 = 2; @@ -42,7 +42,10 @@ public class Game { 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(); } @@ -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) { 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() { - if((gamefield.getField(1) == gamefield.getField(2)) && (gamefield.getField(3) == gamefield.getField(1))){ + if (checkWinVariants(1,4,9)) { return true; } - else if((gamefield.getField(4) == gamefield.getField(5)) && (gamefield.getField(6) == gamefield.getField(4))){ + else if (checkWinVariants(2,5,8)) { return true; } - else if((gamefield.getField(7) == gamefield.getField(8)) && (gamefield.getField(9) == gamefield.getField(7))){ + else if (checkWinVariants(3,6,9)) { return true; } - else if((gamefield.getField(1) == gamefield.getField(4)) && (gamefield.getField(7) == gamefield.getField(1))){ + else if (checkWinVariants(1,2,3)) { return true; } - else if((gamefield.getField(2) == gamefield.getField(5)) && (gamefield.getField(8) == gamefield.getField(2))){ + else if (checkWinVariants(4,5,6)) { return true; } - else if((gamefield.getField(3) == gamefield.getField(6)) && (gamefield.getField(9) == gamefield.getField(3))){ + else if (checkWinVariants(7,8,9)) { return true; } - else if((gamefield.getField(1) == gamefield.getField(5)) && (gamefield.getField(9) == gamefield.getField(1))){ + else if (checkWinVariants(1,5,9)) { 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 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; + } + } }