commit e235ee579dbff698eb4ec21f21c13f09fb4fcffb
Author: 3009857 <3009857@stud.hs-mannheim.de>
Date: Thu Oct 10 12:15:03 2024 +0200
Lösung zu Uebung_02
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5ff6309
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..aa00ffa
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..5535e8f
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..585308c
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+
+
+
+ Proofreading
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..306b89a
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,17 @@
+
+
+ 4.0.0
+
+ org.example
+ Uebung_02
+ 1.0-SNAPSHOT
+
+
+ 22
+ 22
+ UTF-8
+
+
+
\ No newline at end of file
diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java
new file mode 100644
index 0000000..df023f5
--- /dev/null
+++ b/src/main/java/org/example/Main.java
@@ -0,0 +1,127 @@
+package org.example;
+/**
+ * Tic Tac Toe Game
+ * @author: Hanin Aljalab
+ */
+
+
+import java.util.Random;
+import java.util.Scanner;
+
+public class Main {
+ public static void main(String[] args) {
+
+ // Decleration of the variables
+ char[][] board = {{' ', ' ', ' '},
+ {' ', ' ', ' '},
+ {' ', ' ', ' '}
+ };
+ int player;
+ boolean win;
+
+ // Initialize the variables
+ player = 1;
+ win = false;
+
+ // creating the "Scanner"
+ Scanner sc = new Scanner(System.in);
+
+ // Start the game
+ while (!win && !isBoardFull(board)) {
+ spielfeldAusgeben(board);
+
+ if (player == 1) {
+ // Player's turn
+ spielerZug(board, sc);
+ } else {
+ // Computer's turn
+ computerZug(board);
+ }
+
+ // Check for a win or draw
+ win = isWin(board);
+ if (!win) {
+ player = (player == 1) ? 2 : 1; // change the player
+ }
+ }
+
+ spielfeldAusgeben(board);
+
+ if (win) {
+ System.out.println("Spieler " + player + " gewinnt!");
+ } else {
+ System.out.println("Unentschieden!");
+ }
+ }
+
+ // print the game board
+ public static void spielfeldAusgeben(char[][] board) {
+ for (int i = 0; i < board.length; i++) {
+ for (int j = 0; j < board[i].length; j++) {
+ System.out.print(" [" + board[i][j] + "] ");
+ }
+ System.out.println();
+ }
+ }
+
+ //
+ public static void spielerZug(char[][] board, Scanner sc) {
+ System.out.println("Spieler, geben Sie Ihre Position ein (1-9): ");
+ int input = sc.nextInt();
+
+ // Convert input in row and column
+ int row = (input - 1) / 3;
+ int col = (input - 1) % 3;
+
+ // Check whether the position is valid and the field is not occupied
+ if (input >= 1 && input <= 9 && board[row][col] == ' ') {
+ board[row][col] = 'O';
+ } else {
+ System.out.println("Ungültige Position. Bitte erneut versuchen.");
+ spielerZug(board, sc); // Recursive call for new input
+ }
+ }
+
+ public static void computerZug(char[][] board) {
+ Random ra = new Random();
+ int row, col;
+
+ do {
+ row = ra.nextInt(board.length);
+ col = ra.nextInt(board.length);
+ } while (board[row][col] != ' ');
+
+ board[row][col] = 'X';
+ }
+
+ public static boolean isWin(char[][] board) {
+ // Check for profit in rows, columns and diagonals
+ for (int i = 0; i < 3; i++) {
+ // Profit in rows
+ if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
+ return true;
+ }
+ // Profit in columns
+ if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
+ return true;
+ }
+ }
+ // Profit in diagonals
+ if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')) {
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isBoardFull(char[][] board) {
+ // Check whether the pitch is full (draw)
+ for (int i = 0; i < board.length; i++) {
+ for (int j = 0; j < board[i].length; j++) {
+ if (board[i][j] == ' ') {
+ return false; // There are still empty positions
+ }
+ }
+ }
+ return true; // All positions have been filled
+ }
+}