Lösung zu Uebung_02

main
Hanin Aljalab 2024-10-10 12:15:03 +02:00
commit e235ee579d
8 changed files with 240 additions and 0 deletions

38
.gitignore vendored 100644
View File

@ -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

8
.idea/.gitignore vendored 100644
View File

@ -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

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

View File

@ -0,0 +1,12 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="GrazieInspection" enabled="false" level="GRAMMAR_ERROR" enabled_by_default="false" />
<inspection_tool class="LanguageDetectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>

25
.idea/misc.xml 100644
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<selected-state>
<State>
<id>Proofreading</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml 100644
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

17
pom.xml 100644
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Uebung_02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -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
}
}