From e7f3576bb840c83a1a6699784f80205ce694995b Mon Sep 17 00:00:00 2001 From: Andres Cruz <1912548@stud.hs-mannheim.de> Date: Sat, 2 Nov 2024 05:40:48 +0100 Subject: [PATCH] V2 is V1 shortened --- src/main/java/Uebung_02/TicTacToe_AC1.java | 2 +- src/main/java/Uebung_02/TicTacToe_AC2.java | 130 +++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Uebung_02/TicTacToe_AC2.java diff --git a/src/main/java/Uebung_02/TicTacToe_AC1.java b/src/main/java/Uebung_02/TicTacToe_AC1.java index 04fda54..0256ba1 100644 --- a/src/main/java/Uebung_02/TicTacToe_AC1.java +++ b/src/main/java/Uebung_02/TicTacToe_AC1.java @@ -84,7 +84,7 @@ public class TicTacToe_AC1 { } public static boolean gameHasEnded() { - return noMovesLeft() || gameWasWon(); + return gameWasWon() || noMovesLeft(); } public static boolean noMovesLeft() { diff --git a/src/main/java/Uebung_02/TicTacToe_AC2.java b/src/main/java/Uebung_02/TicTacToe_AC2.java new file mode 100644 index 0000000..815cb03 --- /dev/null +++ b/src/main/java/Uebung_02/TicTacToe_AC2.java @@ -0,0 +1,130 @@ +package Uebung_02; + +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; + +public class TicTacToe_AC2 { + + public static final String EMPTYFIELD = " "; + public static final String X = "X"; + public static final String O = "O"; + public static String[] gameFields = new String[9]; + + public static void main(String[] args) { + initializeGameStatus(); + computerMove(); + printGrid(); + + Scanner scanner = new Scanner(System.in); + while(true) { + humanMove(scanner); + printGrid(); + if(gameHasEnded()) { + System.out.println("\nDu hast gewonnen!"); + break; + } + + computerMove(); + printGrid(); + if(gameHasEnded()) { + System.out.println("\nDer Roboter hat gewonnen!"); + break; + } + } + scanner.close(); + } + + + public static void initializeGameStatus() { + Arrays.fill(gameFields, EMPTYFIELD); + } + + public static void computerMove() { + int randomNumber = -1; + + do { + randomNumber = generateNumber(); + } while (!isFieldChoosable(randomNumber)); + + gameFields[randomNumber] = X; + } + + public static void humanMove(Scanner scanner) { + printOptionsMenu(); + int choice = -1; + + do { + choice = scanner.nextInt(); + } while (!isFieldChoosable(choice-1)); + + gameFields[choice-1] = O; + } + + public static int generateNumber() { + Random random = new Random(); + return random.nextInt(9); + } + + public static boolean isFieldChoosable(int index) { + return gameFields[index].equals(EMPTYFIELD); + } + + public static boolean gameHasEnded() { + return gameWasWon() || noMovesLeft(); + } + + public static boolean noMovesLeft() { + for (String s: gameFields) { + if (s.equals(EMPTYFIELD)) return false; + } + System.out.println("\nUnentschieden!"); + return true; + } + + public static boolean gameWasWon() { + return isDiagonalWin() || isHorizontalWin() || isVerticalWin(); + } + + public static boolean isDiagonalWin() { + if (isLineComplete(0,4,8) || isLineComplete(2,4,6)) return true; + return false; + } + + public static boolean isHorizontalWin() { + if (isLineComplete(0,1,2) || isLineComplete(3,4,5) || isLineComplete(6,7,8)) return true; + return false; + } + + public static boolean isVerticalWin() { + if (isLineComplete(0,3,6) || isLineComplete(1,4,7) || isLineComplete(2,5,8)) return true; + return false; + } + + public static boolean isLineComplete(int first, int second, int third) { + return !gameFields[first].equals(EMPTYFIELD) && + gameFields[first].equals(gameFields[second]) && + gameFields[first].equals(gameFields[third]); + } + + //Below is section for helper methods - Strings and outputs// + public static void printGrid() { + System.out.println("______________________________________________________________________"); + String grid = ""; + for (int i = 0; i < 9; i++) { + if (i % 3 == 0) grid += "\n"; + grid += "[" + gameFields[i] + "]"; + } + System.out.println(grid); + } + + public static void printOptionsMenu() { + System.out.println("\n\nDu hast die folgende leeren Felder: "); + String grid = ""; + for (int i = 0; i < 9; i++) { + if (i % 3 == 0) grid += "\n"; + grid += isFieldChoosable(i) ? (i + 1)+" " : " "; + } + System.out.println(grid+"\nGib die Zahl ein, wo du deinen Kreis setzen willst: "); + } +}