diff --git a/TicTacToe/.DS_Store b/TicTacToe/.DS_Store new file mode 100644 index 0000000..6510a16 Binary files /dev/null and b/TicTacToe/.DS_Store differ diff --git a/TicTacToe/.classpath b/TicTacToe/.classpath new file mode 100644 index 0000000..5606c09 --- /dev/null +++ b/TicTacToe/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/TicTacToe/.project b/TicTacToe/.project new file mode 100644 index 0000000..d6bf9b5 --- /dev/null +++ b/TicTacToe/.project @@ -0,0 +1,17 @@ + + + TicTacToe + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/TicTacToe/src/Main.java b/TicTacToe/src/Main.java new file mode 100644 index 0000000..5e05901 --- /dev/null +++ b/TicTacToe/src/Main.java @@ -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(); + + } + +} diff --git a/TicTacToe/src/Spiel.java b/TicTacToe/src/Spiel.java new file mode 100644 index 0000000..4ed8a99 --- /dev/null +++ b/TicTacToe/src/Spiel.java @@ -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; + } +}