Hallo!
Ich versuche mich gerade daran ein TicTacToe Spiel in Java zu schreiben. Der Spieler spielt gegen den Computer, der verwendet den MiniMax Algorithmus zum spielen. Nur bei mir scheint er immer nur das Feld aufzufüllen, was mache ich falsch?
lg
Bin jetzt schon dankbar für Vorschläge und Ideen, sitz nämlich schon ein paar Tage drann
Ich versuche mich gerade daran ein TicTacToe Spiel in Java zu schreiben. Der Spieler spielt gegen den Computer, der verwendet den MiniMax Algorithmus zum spielen. Nur bei mir scheint er immer nur das Feld aufzufüllen, was mache ich falsch?
lg
Java:
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Game {
public char[][] _field;
public char _lastplayer;
public char _winnerChar;
public char _firstplayer;
public TicTacToeField _zug;
public TicTacToeField _t;
public Game(char player) {
_field = new char[3][3];
_t = new TicTacToeField();
int j = 0;
int i = 0;
// Every position is initialized with 0//
while (i < 3) {
_field[j][i] = '0';
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
if (player == 'c') {
_firstplayer = 'c';
_lastplayer = 'm';
}
else {
_lastplayer = 'c';
_firstplayer = 'm';
}
_zug = _t;
}
public boolean isCompleted(char[][] a) {
if (a[0][0] != '0' && a[0][0] == a[0][1] && a[0][1] == a[0][2]) {
_winnerChar = a[0][0];
return true;
} else if (a[1][0] != '0' && a[1][0] == a[1][1] && a[1][1] == a[1][2]) {
_winnerChar = a[1][0];
return true;
} else if (a[2][0] != '0' && a[2][0] == a[2][1] && a[2][1] == a[2][2]) {
_winnerChar = a[2][0];
return true;
} else if (a[0][0] != '0' && a[0][0] == a[1][0] && a[1][0] == a[2][0]) {
_winnerChar = a[0][0];
return true;
} else if (a[0][1] != '0' && a[0][1] == a[1][1] && a[1][1] == a[2][1]) {
_winnerChar = a[0][1];
return true;
} else if (a[0][2] != '0' && a[0][2] == a[1][2] && a[1][2] == a[2][2]) {
_winnerChar = a[0][2];
return true;
} else if (a[0][0] != '0' && a[0][0] == a[1][1] && a[1][1] == a[2][2]) {
_winnerChar = a[0][0];
return true;
} else if (a[0][2] != '0' && a[0][2] == a[1][1] && a[1][1] == a[2][0]) {
_winnerChar = a[0][2];
return true;
}
else
return false;
}
public boolean isAlreadyPositioned(int row, int column, char[][] field) {
if (field[row][column] == 'X' || field[row][column] == 'O') {
return true;
}
else
return false;
}
public boolean gameIsFinished(char[][] a) {
int j = 0;
int i = 0;
// Every position is initialized with 0//
while (i < 3) {
if (a[j][i] == '0') {
return false;
}
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
return true;
}
public void startTheGame() {
int j = 0;
int i = 0;
// Every position is initialized with 0//
while (i < 3) {
_field[j][i] = '0';
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
int row = 0;
int column = 0;
while (!isCompleted(_field) && !gameIsFinished(_field)) {
if (_lastplayer == 'c') {
do {
do {
System.out.println("Ihre Reihe?");
Scanner scanner = new Scanner(System.in);
row = scanner.nextInt();
System.out.println("Ihre Spalte?");
column = scanner.nextInt();
} while ((row < 0 || row > 2) && column < 0 || column > 2);
} while (isAlreadyPositioned(row, column, _field));
_field[row][column] = 'X';
StringMaker(_field);
_lastplayer = 'm';
}
else if (_lastplayer == 'm') {
do {
Random r = new Random();
row = r.nextInt(3);
column = r.nextInt(3);
} while (isAlreadyPositioned(row, column, _field));
_field[row][column] = 'O';
StringMaker(_field);
_lastplayer = 'c';
}
}
if (gameIsFinished(_field) && !isCompleted(_field)) {
System.out.println("Es steht unentschieden!");
}
if (isCompleted(_field) && _lastplayer == 'c') {
System.out.println("Leider haben Sie gegen den Computer verloren!");
}
if (isCompleted(_field) && _lastplayer == 'm') {
System.out.println("GRATULATION, Sie haben gewonnen!");
}
System.out.println(evaluate(_field, 'm'));
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println(evaluate(_field, 'c'));
System.out.println("Wollen sie nocheinmal spielen? YES or NO?");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
String s1 = "YES";
if (s.equals(s1)) {
startTheGame();
}
else
System.out.println("Danke fürs spielen");
}
public boolean isTerm(char[][] a) {
if (isCompleted(a) || gameIsFinished(a)) {
return true;
}
else
return false;
}
public char[][] copyField(char[][] field) {
char[][] field2 = new char[3][3];
int j = 0;
int i = 0;
while (i < 3) {
field2[j][i] = field[j][i];
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
return field2;
}
public ArrayList<TicTacToeField> getAllFields(TicTacToeField a) {
ArrayList<TicTacToeField> allfields = new ArrayList<TicTacToeField>();
int x = 0;
int o = 0;
int j = 0;
int i = 0;
while (i < 3) {
if (a._field[j][i] == 'X') {
x++;
}
if (a._field[j][i] == 'O') {
o++;
}
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
if (x >= o) {
j = 0;
i = 0;
while (i < 3) {
if (a._field[j][i] == '0') {
TicTacToeField field2 = new TicTacToeField(a);
field2._field[j][i] = 'O';
allfields.add(field2);
}
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
}
else if (o > x) {
j = 0;
i = 0;
while (i < 3) {
if (a._field[j][i] == '0') {
TicTacToeField field2 = new TicTacToeField(a);
field2._field[j][i] = 'X';
allfields.add(field2);
}
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
}
return allfields;
}
public int evaluate(char[][] field, char player) {
int value = 0;
if (gameIsFinished(field) && !isCompleted(field)) {
return 0;
}
else if (isCompleted(field) && _winnerChar == 'O' && player == 'c') {
value = 10;
int i = 0;
int j = 0;
while (i < 3) {
if (field[j][i] == '0') {
value++;
}
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
}
else if (isCompleted(field) && _winnerChar == 'X' && player == 'c') {
value = -10;
}
else if (isCompleted(field) && _winnerChar == 'O' && player == 'm') {
value = -10;
}
else if (isCompleted(field) && _winnerChar == 'X' && player == 'm') {
value = 10;
int i = 0;
int j = 0;
while (i < 3) {
if (field[j][i] == '0') {
value++;
}
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
}
return value;
}
public void StringMaker(char[][] field) {
int i = 0;
int j = 0;
System.out.println();
System.out.println();
while (i < 3) {
System.out.print(field[j][i] + " ");
i++;
if (i == 3 && j < 2) {
System.out.println();
i = 0;
j++;
}
}
System.out.println();
System.out.println();
}
public void startTheGameMiniMax() {
int j = 0;
int i = 0;
// Every position is initialized with 0//
while (i < 3) {
_t._field[j][i] = '0';
i++;
if (i == 3 && j < 2) {
i = 0;
j++;
}
}
int row = 0;
int column = 0;
while (!isCompleted(_t._field) && !gameIsFinished(_t._field)) {
if (_lastplayer == 'c') {
do {
do {
System.out.println("Ihre Reihe?");
Scanner scanner = new Scanner(System.in);
row = scanner.nextInt();
System.out.println("Ihre Spalte?");
column = scanner.nextInt();
} while ((row < 0 || row > 2) && column < 0 || column > 2);
} while (isAlreadyPositioned(row, column, _t._field));
_t._field[row][column] = 'X';
StringMaker(_t._field);
_lastplayer = 'm';
}
else if (_lastplayer == 'm') {
if (_firstplayer == 'c') {
max(_t, 0);
StringMaker(_t._field);
}
if (_firstplayer == 'm') {
min(_t, 0);
StringMaker(_t._field);
}
_lastplayer = 'c';
}
}
if (gameIsFinished(_t._field) && !isCompleted(_t._field)) {
System.out.println("Es steht unentschieden!");
}
if (isCompleted(_t._field) && _lastplayer == 'c') {
System.out.println("Leider haben Sie gegen den Computer verloren!");
}
if (isCompleted(_t._field) && _lastplayer == 'm') {
System.out.println("GRATULATION, Sie haben gewonnen!");
}
System.out.println("Wollen sie nocheinmal spielen? YES or NO?");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
String s1 = "YES";
if (s.equals(s1)) {
startTheGameMiniMax();
}
else
System.out.println("Danke fürs spielen");
}
public int max(TicTacToeField tfield, int tiefe) {
int t;
int maxValue = Integer.MIN_VALUE;
if (isTerm(tfield._field)) {
maxValue = evaluate(tfield._field, 'm');
return maxValue;
}
ArrayList<TicTacToeField> allChilds = getAllFields(tfield);
while (!allChilds.isEmpty()) {
t = min(allChilds.get(0), tiefe++);
if (t > maxValue) {
maxValue = t;
if (tiefe == 1) {
_t._field = copyField(allChilds.get(0)._field);
StringMaker(_t._field);
}
}
allChilds.remove(0);
}
return maxValue;
}
public int min(TicTacToeField tfield, int tiefe) {
int t;
int minValue = Integer.MAX_VALUE;
if (isTerm(tfield._field)) {
minValue = evaluate(tfield._field, 'c');
return minValue;
}
ArrayList<TicTacToeField> allChilds = getAllFields(tfield);
while (!allChilds.isEmpty()) {
t = max(allChilds.get(0), tiefe++);
if (t < minValue) {
minValue = t;
if (tiefe == 1) {
_t._field = copyField(allChilds.get(0)._field);
StringMaker(_t._field);
}
}
allChilds.remove(0);
}
return minValue;
}
}
Bin jetzt schon dankbar für Vorschläge und Ideen, sitz nämlich schon ein paar Tage drann