V1 no objects no bot

master
WSIESO 2024-12-24 22:58:56 +01:00
commit c948549907
6 changed files with 193 additions and 0 deletions

5
src/Bot.java 100644
View File

@ -0,0 +1,5 @@
public class Bot {
boolean archive;
}

5
src/Game.java 100644
View File

@ -0,0 +1,5 @@
import java.util.ArrayList;
public class Game {
ArrayList<Grid> game = new ArrayList<>();
}

26
src/GameLogic.java 100644
View File

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

123
src/GamePlay.java 100644
View File

@ -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("-------------");
}
}

29
src/Grid.java 100644
View File

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

5
src/Main.java 100644
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
GamePlay.run();
}
}