First update

master
Suemeyra61 2023-01-14 19:54:50 +01:00
parent ce9784233d
commit eb98e011d2
5 changed files with 194 additions and 0 deletions

BIN
TicTacToe/.DS_Store vendored 100644

Binary file not shown.

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 19 [19]">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
TicTacToe/.project 100644
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TicTacToe</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,30 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] feld = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
Spiel.zeigeFeld(feld);
while (true) {
Spiel.spieler(feld, sc);
if (Spiel.spielzuEnde(feld)) {
break;
}
Spiel.zeigeFeld(feld);
Spiel.computerSpielt(feld);
if (Spiel.spielzuEnde(feld)) {
break;
}
Spiel.zeigeFeld(feld);
}
sc.close();
System.out.println();
}
}

View File

@ -0,0 +1,137 @@
import java.util.Scanner;
public class Spiel {
public static void zeigeFeld(char[][] feld) {
System.out.println(feld[0][0] + "|" + feld[0][1] + "|" + feld[0][2]);
System.out.println("-----");
System.out.println(feld[1][0] + "|" + feld[1][1] + "|" + feld[1][2]);
System.out.println("-----");
System.out.println(feld[2][0] + "|" + feld[2][1] + "|" + feld[2][2]);
}
public static void spieler(char[][] feld, Scanner sc) {
String eingabe;
while (true) {
System.out.println("Sie sind Spieler X und der Computer ist O\n" + "Wo möchten Sie spielen (1-9)");
eingabe = sc.nextLine();
System.out.println(eingabe);
if (istFeldleer(feld, Integer.parseInt(eingabe))) {
break;
} else {
System.out.println(eingabe + " nicht möglich");
}
}
spielPosition(feld, eingabe, 'X');
}
public static void computerSpielt(char[][] feld) {
int randomcomputer;
while (true) {
randomcomputer = (int) (Math.random() * 9) + 1;
if (istFeldleer(feld, randomcomputer)) {
break;
}
}
System.out.println("Computer spielte " + randomcomputer);
spielPosition(feld, Integer.toString(randomcomputer), 'O');
}
public static void spielPosition(char[][] feld, String position, char symbol) {
switch (position) {
case "1":
feld[0][0] = symbol;
break;
case "2":
feld[0][1] = symbol;
break;
case "3":
feld[0][2] = symbol;
break;
case "4":
feld[1][0] = symbol;
break;
case "5":
feld[1][1] = symbol;
break;
case "6":
feld[1][2] = symbol;
break;
case "7":
feld[2][0] = symbol;
break;
case "8":
feld[2][1] = symbol;
break;
case "9":
feld[2][2] = symbol;
break;
default:
System.out.println(":(");
}
}
public static boolean istFeldleer(char[][] feld, int position) {
switch (position) {
case 1:
return (feld[0][0] == ' ');
case 2:
return (feld[0][1] == ' ');
case 3:
return (feld[0][2] == ' ');
case 4:
return (feld[1][0] == ' ');
case 5:
return (feld[1][1] == ' ');
case 6:
return (feld[1][2] == ' ');
case 7:
return (feld[2][0] == ' ');
case 8:
return (feld[2][1] == ' ');
case 9:
return (feld[2][2] == ' ');
default:
return false;
}
}
public static boolean spielkontrolle(char[][] feld, char symbol) {
if ((feld[0][0] == symbol && feld[0][1] == symbol && feld[0][2] == symbol)
|| (feld[1][0] == symbol && feld[1][1] == symbol && feld[1][2] == symbol)
|| (feld[2][0] == symbol && feld[2][1] == symbol && feld[2][2] == symbol) ||
(feld[0][0] == symbol && feld[1][0] == symbol && feld[2][0] == symbol)
|| (feld[0][1] == symbol && feld[1][1] == symbol && feld[2][1] == symbol)
|| (feld[0][2] == symbol && feld[1][2] == symbol && feld[2][2] == symbol) ||
(feld[0][0] == symbol && feld[1][1] == symbol && feld[2][2] == symbol)
|| (feld[0][2] == symbol && feld[1][1] == symbol && feld[2][0] == symbol)) {
return true;
}
return false;
}
public static boolean spielzuEnde(char[][] feld) {
if (spielkontrolle(feld, 'X')) {
zeigeFeld(feld);
System.out.println("Spieler X hat gewonnen");
return true;
} else if (spielkontrolle(feld, 'O')) {
zeigeFeld(feld);
System.out.println("Computer hat gewonnen");
return true;
}
for (int i = 0; i < feld.length; i++) {
for (int j = 0; j < feld[i].length; j++) {
if (feld[i][j] == ' ') {
return false;
}
}
}
zeigeFeld(feld);
System.out.println("Unentschieden");
return true;
}
}