commit c9485499070d52ea0da93a32bd4b2d8477b239fb Author: WSIESO Date: Tue Dec 24 22:58:56 2024 +0100 V1 no objects no bot diff --git a/src/Bot.java b/src/Bot.java new file mode 100644 index 0000000..0a4ca65 --- /dev/null +++ b/src/Bot.java @@ -0,0 +1,5 @@ +public class Bot { + boolean archive; + + +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..28ee4b8 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,5 @@ +import java.util.ArrayList; + +public class Game { + ArrayList game = new ArrayList<>(); +} diff --git a/src/GameLogic.java b/src/GameLogic.java new file mode 100644 index 0000000..1931400 --- /dev/null +++ b/src/GameLogic.java @@ -0,0 +1,26 @@ +import java.util.Objects; + +public class GameLogic { + + public static boolean checkWin(String[] top, String[] mid, String[] bottom) { + boolean run = true; + if(!Objects.equals(top[0], " ") &&top[0].equals(top[1])&&top[1].equals(top[2])){ + run=false; + }else if (!Objects.equals(mid[0], " ") &&mid[0].equals(mid[1])&&mid[1].equals(mid[2])){ + run=false; + } else if ((!Objects.equals(bottom[0], " ") &&bottom[0].equals(bottom[1])&&bottom[1].equals(bottom[2]))) { + run=false; + }else if(!Objects.equals(top[0], " ")&&top[0].equals(mid[0])&&mid[0].equals(bottom[0])){ + run=false; + }else if(!Objects.equals(top[1], " ")&&top[1].equals(mid[1])&&mid[1].equals(bottom[1])){ + run=false; + }else if(!Objects.equals(top[2], " ")&&top[2].equals(mid[2])&&mid[2].equals(bottom[2])){ + run=false; + }else if(!Objects.equals(mid[1], " ")&&top[0].equals(mid[1])&&mid[1].equals(bottom[2])){ + run=false; + }else if(!Objects.equals(mid[1], " ")&&top[2].equals(mid[1])&&mid[1].equals(bottom[0])){ + run=false; + } + return run; + } +} diff --git a/src/GamePlay.java b/src/GamePlay.java new file mode 100644 index 0000000..c015189 --- /dev/null +++ b/src/GamePlay.java @@ -0,0 +1,123 @@ +import java.util.Scanner; + +public class GamePlay { + + public static void run() { + boolean running = true; + String[] top = {" ", " ", " "}; + String[] mid = {" ", " ", " "}; + String[] bottom = {" ", " ", " "}; + Scanner input = new Scanner(System.in); + int i = 2; + String o = " O "; + String x = " X "; + while (running) { + String icon = o; + if (i % 2 == 1) { + icon = o; + } else { + icon = x; + } + printGrid(top, mid, bottom); + System.out.println("player " + (i % 2 + 1) + " turn"); + String[][] newGrid = takeInput(icon, top, mid, bottom); + top = newGrid[0]; + mid = newGrid[1]; + bottom = newGrid[2]; + if (GameLogic.checkWin(top, mid, bottom)==false){ + printGrid(top, mid, bottom); + System.out.println("player " + (i % 2 + 1) + " wins"); + break; + } + if(i>9){ + printGrid(top, mid, bottom); + System.out.println("unentschieden"); + break; + } + i++; + } + } + + public static String[][] takeInput(String icon, String[] top, String[] mid, String[] bottom) { + Scanner input = new Scanner(System.in); + String[] zug = input.nextLine().split(""); + try { + if (Integer.parseInt(zug[1]) > 3) { + System.out.println("out of field"); + takeInput(icon, top, mid, bottom); + String[][] grid = {top, mid, bottom}; + return grid; + + } + } catch (NumberFormatException e) { + System.out.println("invalid input"); + takeInput(icon, top, mid, bottom); + String[][] grid = {top, mid, bottom}; + return grid; + + } + + switch (zug[0].toUpperCase()) { + case "A": + if (top[Integer.parseInt(zug[1]) - 1].equals(" ") == false) { + System.out.println("field already occupied"); + takeInput(icon, top, mid, bottom); + String[][] grid = {top, mid, bottom}; + return grid; + } + top[Integer.parseInt(zug[1]) - 1] = icon; + break; + case "B": + if (mid[Integer.parseInt(zug[1]) - 1].equals(" ") == false) { + System.out.println("fiel already occupied"); + takeInput(icon, top, mid, bottom); + String[][] grid = {top, mid, bottom}; + return grid; + } + mid[Integer.parseInt(zug[1]) - 1] = icon; + break; + case "C": + if (bottom[Integer.parseInt(zug[1]) - 1].equals(" ") == false) { + System.out.println("fiel already occupied"); + takeInput(icon, top, mid, bottom); + String[][] grid = {top, mid, bottom}; + return grid; + } + bottom[Integer.parseInt(zug[1]) - 1] = icon; + break; + + default: + System.out.println("out of field"); + takeInput(icon, top, mid, bottom); + String[][] grid = {top, mid, bottom}; + return grid; + + } + + String[][] grid = {top, mid, bottom}; + + return grid; + } + + public static void printGrid(String[] top, String[] mid, String[] bottom) { + + for (String s : top) { + System.out.print("|" + s); + } + System.out.println("|"); + + System.out.println("-------------"); + for (String s : mid) { + System.out.print("|" + s); + } + System.out.println("|"); + + System.out.println("-------------"); + for (String s : bottom) { + System.out.print("|" + s); + } + System.out.println("|"); + + System.out.println("-------------"); + } +} diff --git a/src/Grid.java b/src/Grid.java new file mode 100644 index 0000000..765bc36 --- /dev/null +++ b/src/Grid.java @@ -0,0 +1,29 @@ +public class Grid { + String[] top = {" ", " ", " "}; + String[] mid = {" ", " ", " "}; + String[] bottom = {" ", " ", " "}; + + public String[] getTop() { + return top; + } + + public void setTop(String[] top) { + this.top = top; + } + + public String[] getBottom() { + return bottom; + } + + public void setBottom(String[] bottom) { + this.bottom = bottom; + } + + public String[] getMid() { + return mid; + } + + public void setMid(String[] mid) { + this.mid = mid; + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..25d3953 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + GamePlay.run(); + } +} \ No newline at end of file