V1 no objects no bot
parent
c948549907
commit
de84a94773
225
src/Bot.java
225
src/Bot.java
|
|
@ -1,5 +1,230 @@
|
|||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Bot {
|
||||
boolean archive;
|
||||
int level = 0;
|
||||
boolean botFirst = false;
|
||||
int botWins =0;
|
||||
|
||||
|
||||
|
||||
|
||||
public Bot(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public Bot(boolean archive) {
|
||||
this.archive = archive;
|
||||
}
|
||||
|
||||
private int[] checkOpp(String icon, String[] top, String[] mid, String[] bottom){
|
||||
if (icon.equals(" O ")){
|
||||
icon=" X ";
|
||||
}else{
|
||||
icon=" O ";
|
||||
}
|
||||
return checkWin(icon, top, mid, bottom);
|
||||
}
|
||||
private int[] checkWin(String icon, String[] top, String[] mid, String[] bottom){
|
||||
String[] originaltop = top;
|
||||
String[] originalmid = mid;
|
||||
String[] originalbottom = bottom;
|
||||
|
||||
for (int row = 0; row <= 2 ; row++) {
|
||||
for (int column = 0; column <= 2 ; column++) {
|
||||
|
||||
|
||||
if (row == 1) {
|
||||
if (originaltop[column].equals(" ") == false) {
|
||||
continue;
|
||||
}
|
||||
} else if (row == 2) {
|
||||
if (originalmid[column].equals(" ") == false) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (originalbottom[column].equals(" ") == false) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
top = originaltop.clone();
|
||||
mid = originalmid.clone();
|
||||
bottom = originalbottom.clone();
|
||||
|
||||
String[][] newGrid = handleBotInput(icon, top, mid, bottom, row, column);
|
||||
|
||||
if (GameLogic.checkWin(newGrid[0], newGrid[1], newGrid[2])==false) {
|
||||
return new int[]{row, column,1};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return new int[]{(int) (Math.random()*3),(int) (Math.random()*3)};
|
||||
|
||||
}
|
||||
|
||||
public String[][] botInput(String icon, String[] top, String[] mid, String[] bottom) {
|
||||
String[] emptyRow = {" "," "," "};
|
||||
String[] midPoint = {" "," X "," "};
|
||||
String[] bottomCorner = {" O "," X "," O "};
|
||||
String[] bottomCornerX = {" O "," "," X "};
|
||||
|
||||
int row = 0;
|
||||
int column = 0;
|
||||
|
||||
if (level>=2) {
|
||||
int[] coords = checkWin(icon, top, mid, bottom);
|
||||
row = coords[0];
|
||||
column = coords[1];
|
||||
if(coords.length==3){
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}}
|
||||
|
||||
if (Arrays.equals(top, emptyRow) && Arrays.equals(mid, emptyRow) && Arrays.equals(bottom, emptyRow)) {
|
||||
botFirst=true;
|
||||
}
|
||||
|
||||
row=0;
|
||||
column=0;
|
||||
|
||||
if(level>=3){
|
||||
int[] coords = checkOpp(icon, top, mid, bottom);
|
||||
row = coords[0];
|
||||
column = coords[1];
|
||||
if (coords.length==3){
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}
|
||||
}
|
||||
|
||||
row=0;
|
||||
column=0;
|
||||
|
||||
if (level==4&&botFirst== false&&mid[1].equals(" ")){
|
||||
row=2;
|
||||
column=1;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}
|
||||
|
||||
if(level == 4 && botFirst==false&&Arrays.equals(top, emptyRow) && !mid[1].equals(" ") && Arrays.equals(bottom, emptyRow)){
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
|
||||
}
|
||||
|
||||
if (botFirst) {
|
||||
if (level==4&& Arrays.equals(top, emptyRow) && Arrays.equals(mid, emptyRow) && Arrays.equals(bottom, emptyRow)) {
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
|
||||
}else if(level==4&& !(Arrays.equals(mid, midPoint))&&!Arrays.equals(bottom,bottomCorner)){
|
||||
if (Arrays.equals(bottom,bottomCornerX)&&top[0].equals(" ")){
|
||||
row = 1;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}
|
||||
if (mid[0].equals(" ")&&top[2].equals(" ")) {
|
||||
column=2;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
} else if (mid[1].equals(" ")) {
|
||||
row=2;
|
||||
column=1;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
|
||||
}
|
||||
} else if (level==4&&Arrays.equals(bottom,bottomCorner)) {
|
||||
if(top[0].equals(" ")&&mid[0].equals(" ")){
|
||||
row=1;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}else if(top[2].equals(" ")&&mid[2].equals(" ")) {
|
||||
row= 1;
|
||||
column= 2;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (level==1) {
|
||||
row = (int) (Math.random()*3);
|
||||
column = (int) (Math.random()*3);
|
||||
|
||||
} else if (level>=2) {
|
||||
int[] coords = checkWin(icon, top, mid, bottom);
|
||||
row = coords[0];
|
||||
column = coords[1];
|
||||
if(coords.length==3){
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}
|
||||
}
|
||||
|
||||
if (level==4&&Arrays.equals(bottom,bottomCornerX)&&mid[0].equals(" X ")&&top[0].equals(" O ")&&top[2].equals(" ")) {
|
||||
row = 1;
|
||||
column = 2;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
|
||||
}
|
||||
|
||||
if(level>=3){
|
||||
int[] coords = checkOpp(icon, top, mid, bottom);
|
||||
row = coords[0];
|
||||
column = coords[1];
|
||||
}
|
||||
|
||||
if (level==4&&botFirst&&mid[1].equals(" X ")&&top[2].equals(" ")){
|
||||
row = 1;
|
||||
column = 2;
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}
|
||||
|
||||
return handleBotInput(icon, top, mid, bottom, row, column);
|
||||
}
|
||||
|
||||
private String[][] handleBotInput(String icon, String[] top, String[] mid, String[] bottom, int row, int column) {
|
||||
boolean emptyFields = false;
|
||||
for (int i = 0; i <= 2; i++) {
|
||||
if(top[i].equals(" ")||mid[i].equals(" ")||bottom[i].equals(" ")){
|
||||
emptyFields = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(emptyFields==false){
|
||||
String[][] grid = {top, mid, bottom};
|
||||
return grid;
|
||||
}
|
||||
|
||||
if (row == 1) {
|
||||
if (top[column].equals(" ") == false) {
|
||||
botInput(icon, top, mid, bottom);
|
||||
String[][] grid = {top, mid, bottom};
|
||||
return grid;
|
||||
}
|
||||
top[column] = icon;
|
||||
} else if (row == 2) {
|
||||
if (mid[column].equals(" ") == false) {
|
||||
botInput(icon, top, mid, bottom);
|
||||
String[][] grid = {top, mid, bottom};
|
||||
return grid;
|
||||
}
|
||||
mid[column] = icon;
|
||||
} else {
|
||||
if (bottom[column].equals(" ") == false) {
|
||||
botInput(icon, top, mid, bottom);
|
||||
String[][] grid = {top, mid, bottom};
|
||||
return grid;
|
||||
}
|
||||
bottom[column] = icon;
|
||||
}
|
||||
|
||||
|
||||
String[][] grid = {top, mid, bottom};
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,16 @@
|
|||
import java.util.ArrayList;
|
||||
|
||||
public class Game {
|
||||
ArrayList<Grid> game = new ArrayList<>();
|
||||
boolean winOrDraw;
|
||||
ArrayList<Grid> sequenz = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String toReturn = "";
|
||||
|
||||
for (Grid grid : sequenz) {
|
||||
toReturn=toReturn.concat(grid.toString());
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,35 +2,159 @@ import java.util.Scanner;
|
|||
|
||||
public class GamePlay {
|
||||
|
||||
static int botAWins = 0;
|
||||
static int botWins = 0;
|
||||
|
||||
public static void run() {
|
||||
boolean running = true;
|
||||
String[] top = {" ", " ", " "};
|
||||
String[] mid = {" ", " ", " "};
|
||||
String[] bottom = {" ", " ", " "};
|
||||
Game game = new Game();
|
||||
Grid grid = new Grid();
|
||||
Scanner input = new Scanner(System.in);
|
||||
int i = 2;
|
||||
String o = " O ";
|
||||
String x = " X ";
|
||||
while (running) {
|
||||
game.sequenz.add(grid);
|
||||
String icon = o;
|
||||
if (i % 2 == 1) {
|
||||
icon = o;
|
||||
} else {
|
||||
icon = x;
|
||||
}
|
||||
printGrid(top, mid, bottom);
|
||||
|
||||
System.out.println(grid);
|
||||
|
||||
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);
|
||||
String[][] newGrid = takeInput(icon, grid.top, grid.mid, grid.bottom);
|
||||
grid.top = newGrid[0];
|
||||
grid.mid = newGrid[1];
|
||||
grid.bottom = newGrid[2];
|
||||
if (GameLogic.checkWin(grid.top, grid.mid, grid.bottom)==false){
|
||||
System.out.println(grid);
|
||||
System.out.println("player " + (i % 2 + 1) + " wins");
|
||||
break;
|
||||
}
|
||||
if(i>9){
|
||||
printGrid(top, mid, bottom);
|
||||
System.out.println(grid);
|
||||
System.out.println("unentschieden");
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void runWithBot(int level) {
|
||||
boolean running = true;
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
// System.out.println("soll der bot das archiv nutzen?"); ------------> noch nicht ready
|
||||
// Bot bot = new Bot(input.nextBoolean());
|
||||
Bot bot = new Bot(level);
|
||||
|
||||
Game game = new Game();
|
||||
Grid grid = new Grid();
|
||||
int i = (int) (Math.random()*2)+1;
|
||||
String o = " O ";
|
||||
String x = " X ";
|
||||
while (running) {
|
||||
game.sequenz.add(grid);
|
||||
String icon = o;
|
||||
if (i % 2 == 1) {
|
||||
icon = o;
|
||||
} else {
|
||||
icon = x;
|
||||
}
|
||||
|
||||
System.out.println(grid);
|
||||
|
||||
if (i % 2 == 0) {
|
||||
System.out.println("player turn");
|
||||
}else{
|
||||
System.out.println("bot turn");
|
||||
}
|
||||
if (i % 2 == 0) {
|
||||
String[][] newGrid = takeInput(icon, grid.top, grid.mid, grid.bottom);
|
||||
grid.top = newGrid[0];
|
||||
grid.mid = newGrid[1];
|
||||
grid.bottom = newGrid[2];
|
||||
}else{
|
||||
String[][] newGrid = bot.botInput(icon, grid.top, grid.mid, grid.bottom);
|
||||
grid.top = newGrid[0];
|
||||
grid.mid = newGrid[1];
|
||||
grid.bottom = newGrid[2];
|
||||
|
||||
}
|
||||
if (GameLogic.checkWin(grid.top, grid.mid, grid.bottom)==false){
|
||||
System.out.println(grid);
|
||||
if (i % 2 == 0) {
|
||||
System.out.println("player wins");
|
||||
}else {
|
||||
System.out.println("bot wins");
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(i>9){
|
||||
System.out.println(grid);
|
||||
System.out.println("unentschieden");
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void botVsBot(int level1, int level2) {
|
||||
boolean running = true;
|
||||
Scanner input = new Scanner(System.in);
|
||||
Bot bot = new Bot(level1);
|
||||
Bot botA = new Bot(level2);
|
||||
// botA.archive=true; -----------------------------> noch nicht bereit
|
||||
Game game = new Game();
|
||||
Grid grid = new Grid();
|
||||
int i = (int) (Math.random()*2)+1;
|
||||
String o = " O ";
|
||||
String x = " X ";
|
||||
while (running) {
|
||||
game.sequenz.add(grid);
|
||||
String icon = o;
|
||||
if (i % 2 == 1) {
|
||||
icon = o;
|
||||
} else {
|
||||
icon = x;
|
||||
}
|
||||
|
||||
System.out.println(grid);
|
||||
|
||||
if (i % 2 == 0) {
|
||||
System.out.println("botA turn mit level: "+level2);
|
||||
}else{
|
||||
System.out.println("bot turn mit level: "+level1);
|
||||
}
|
||||
|
||||
if (i % 2 == 0&&i<10) {
|
||||
String[][] newGrid = botA.botInput(icon, grid.top, grid.mid, grid.bottom);
|
||||
grid.top = newGrid[0];
|
||||
grid.mid = newGrid[1];
|
||||
grid.bottom = newGrid[2];
|
||||
}else if(i<10){
|
||||
String[][] newGrid = bot.botInput(icon, grid.top, grid.mid, grid.bottom);
|
||||
grid.top = newGrid[0];
|
||||
grid.mid = newGrid[1];
|
||||
grid.bottom = newGrid[2];
|
||||
|
||||
}
|
||||
if (GameLogic.checkWin(grid.top, grid.mid, grid.bottom)==false){
|
||||
System.out.println(grid);
|
||||
if (i % 2 == 0) {
|
||||
botAWins++;
|
||||
System.out.println("botA wins mit level: "+level2);
|
||||
}else {
|
||||
botWins++;
|
||||
System.out.println("bot wins mit level: "+level1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(i>9){
|
||||
System.out.println(grid);
|
||||
System.out.println("unentschieden");
|
||||
break;
|
||||
}
|
||||
|
|
@ -99,25 +223,5 @@ public class GamePlay {
|
|||
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("-------------");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,27 +3,32 @@ public class Grid {
|
|||
String[] mid = {" ", " ", " "};
|
||||
String[] bottom = {" ", " ", " "};
|
||||
|
||||
public String[] getTop() {
|
||||
return top;
|
||||
}
|
||||
|
||||
public void setTop(String[] top) {
|
||||
this.top = top;
|
||||
}
|
||||
|
||||
public String[] getBottom() {
|
||||
return bottom;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
String toReturn = "";
|
||||
toReturn=toReturn.concat("-------------\n");
|
||||
for (String s : top) {
|
||||
toReturn=toReturn.concat("|" + s);
|
||||
}
|
||||
toReturn=toReturn.concat("|\n");
|
||||
|
||||
public void setBottom(String[] bottom) {
|
||||
this.bottom = bottom;
|
||||
}
|
||||
toReturn=toReturn.concat("-------------\n");
|
||||
for (String s : mid) {
|
||||
toReturn=toReturn.concat("|" + s);
|
||||
}
|
||||
toReturn=toReturn.concat("|\n");
|
||||
|
||||
public String[] getMid() {
|
||||
return mid;
|
||||
}
|
||||
toReturn=toReturn.concat("-------------\n");
|
||||
for (String s : bottom) {
|
||||
toReturn=toReturn.concat("|" + s);
|
||||
}
|
||||
toReturn=toReturn.concat("|\n");
|
||||
|
||||
public void setMid(String[] mid) {
|
||||
this.mid = mid;
|
||||
toReturn=toReturn.concat("-------------\n");
|
||||
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,37 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
GamePlay.run();
|
||||
// for (int i = 0; i < 20000; i++) {
|
||||
// GamePlay.botVsBot(4,1);
|
||||
//
|
||||
// }
|
||||
// System.out.println("botA wins: "+GamePlay.botAWins);
|
||||
// System.out.println("bot wins: "+GamePlay.botWins);
|
||||
// GamePlay.runWithBot(4);
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("1:Spieler vs Spieler\n2:Spieler vs bot\n3:Bot vs Bot\n>>");
|
||||
String gamemode = input.nextLine();
|
||||
|
||||
switch (gamemode){
|
||||
case "1":
|
||||
GamePlay.run();
|
||||
break;
|
||||
|
||||
case "2":
|
||||
System.out.println("level 1 - 4");
|
||||
GamePlay.runWithBot(Integer.parseInt(input.nextLine()));
|
||||
break;
|
||||
|
||||
case "3":
|
||||
System.out.println("level 1 - 4");
|
||||
GamePlay.botVsBot(Integer.parseInt(input.nextLine()),Integer.parseInt(input.nextLine()));
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("kein game mode");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue