Hallo,
ich habe Probleme beim Programmieren des Computergegners bei meinem TicTacToe-Spiel. Mein Problem ist, das der Computer immer das letzte Feld besetzten möchte, dabei aber auch alle anderen Felder vor diesem Feld besetzt.
Hier meine Klasse für den Computer:
ich habe Probleme beim Programmieren des Computergegners bei meinem TicTacToe-Spiel. Mein Problem ist, das der Computer immer das letzte Feld besetzten möchte, dabei aber auch alle anderen Felder vor diesem Feld besetzt.
Hier meine Klasse für den Computer:
Java:
import java.util.ArrayList;
import java.util.HashMap;
public class Computer {
private static String[][] tempField = new String[3][3];
private static String[][] tempField2 = new String[3][3];
private static HashMap<Integer, String> options = new HashMap<>(); // K: option V: field
private static int winWithOneMove = 9;
private static int winWithTwoMoves = 8;
private static int winWithThreeMoves = 7;
private static int winWithFourMoves = 6;
private static int winWithFiveMoves = 5;
private static int winWithSixMoves = 4;
private static int winWithSevenMoves = 3;
private static int winWithEightMoves = 2;
private static int winWithNineMoves = 1;
private static int tie = 0;
private static int looseWithOneMove = -9;
private static int looseWithTwoMoves = -8;
private static int looseWithThreeMoves = -7;
private static int looseWithFourMoves = -6;
private static int looseWithFiveMoves = -5;
private static int looseWithSixMoves = -4;
private static int looseWithSevenMoves = -3;
private static int looseWithEightMoves = -2;
private static int looseWithNineMoves = -1;
public static void evaluateMove() {
tempField = Field.getFields();
// Möglichkeiten mit nur einem Zug überprüfen
Player.setPlayer("O");
for (int y = 0; y <= 2; y++) {
for (int x = 0; x <= 2; x++) {
if (tempField[y][x].equals("" + new Integer(3 * y + x + 1))) {
tempField[y][x] = Player.getPlayer();
if (checkingWinO())
options.put(winWithOneMove, "" + new Integer(3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + new Integer(3 * y + x +1));
tempField = Field.getFields();
}
}
}
Player.setPlayer("X");
for (int y = 0; y <= 2; y++) {
for (int x = 0; x <= 2; x++) {
if (tempField[y][x].equals("" + new Integer(3 * y + x + 1))) {
tempField[y][x] = Player.getPlayer();
if (checkingWinO())
options.put(looseWithOneMove, "" + new Integer(3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + new Integer(3 * y + x +1));
tempField = Field.getFields();
}
}
}
// Möglichkeiten für mehrere Züge überprüfen
Player.changePlayer();
ArrayList<String> usedFirstMoves = new ArrayList<>();
ArrayList<String> usedSecondMoves = new ArrayList<>();
ArrayList<String> usedThirdMoves = new ArrayList<>();
ArrayList<String> usedFourthMoves = new ArrayList<>();
ArrayList<String> usedFifthMoves = new ArrayList<>();
ArrayList<String> usedSixthMoves = new ArrayList<>();
ArrayList<String> usedSeventhMoves = new ArrayList<>();
ArrayList<String> usedEightMoves = new ArrayList<>();
ArrayList<String> usedNinthMoves = new ArrayList<>();
for (int i = 0; i < 9; i++) {
for (int y = 0; y <= 2; y++) {
for (int x = 0; x <= 2; x++) {
if (!tempField[y][x].equals("" + (3 * y + x + 1))) {
usedFirstMoves.add("" + (3 * y + x + 1));
usedSecondMoves.add("" + (3 * y + x + 1));
usedThirdMoves.add("" + (3 * y + x + 1));
usedFourthMoves.add("" + (3 * y + x + 1));
usedFifthMoves.add("" + (3 * y + x + 1));
usedSixthMoves.add("" + (3 * y + x + 1));
usedSeventhMoves.add("" + (3 * y + x + 1));
usedEightMoves.add("" + (3 * y + x + 1));
usedNinthMoves.add("" + (3 * y + x + 1));
}
}
}
Player.changePlayer();
for (int y = 0; y <= 2; y++) {
for (int x = 0; x <= 2; x++) {
if (!usedFirstMoves.contains("" + (3 * y + x + 1))) {
usedFirstMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
Player.changePlayer();
} else if (!usedSecondMoves.contains("" + (3 * y + x + 1))) {
usedSecondMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithTwoMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithTwoMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedThirdMoves.contains("" + (3 * y + x + 1))) {
usedThirdMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithThreeMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithThreeMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedFourthMoves.contains("" + (3 * y + x + 1))) {
usedFourthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithFourMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithFourMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedFifthMoves.contains("" + (3 * y + x + 1))) {
usedFifthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithFiveMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithFiveMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedSixthMoves.contains("" + (3 * y + x + 1))) {
usedSixthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithSixMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithSixMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedSeventhMoves.contains("" + (3 * y + x + 1))) {
usedSeventhMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithSevenMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithSevenMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedEightMoves.contains("" + (3 * y + x + 1))) {
usedEightMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithEightMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithEightMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedNinthMoves.contains("" + (3 * y + x + 1))) {
usedNinthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithNineMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithNineMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
}
}
}
}
Player.setPlayer("O");
tempField2 = Field.getFields();
for (int i = 0; i < 9; i++) {
for (int y = 0; y <= 2; y++) {
for (int x = 0; x <= 2; x++) {
if (!usedFirstMoves.contains("" + (3 * y + x + 1))) {
usedFirstMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
Player.changePlayer();
} else if (!usedSecondMoves.contains("" + (3 * y + x + 1))) {
usedSecondMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithTwoMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithTwoMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedThirdMoves.contains("" + (3 * y + x + 1))) {
usedThirdMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithThreeMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithThreeMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedFourthMoves.contains("" + (3 * y + x + 1))) {
usedFourthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithFourMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithFourMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedFifthMoves.contains("" + (3 * y + x + 1))) {
usedFifthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithFiveMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithFiveMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedSixthMoves.contains("" + (3 * y + x + 1))) {
usedSixthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithSixMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithSixMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedSeventhMoves.contains("" + (3 * y + x + 1))) {
usedSeventhMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithSevenMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithSevenMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedEightMoves.contains("" + (3 * y + x + 1))) {
usedEightMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithEightMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithEightMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
} else if (!usedNinthMoves.contains("" + (3 * y + x + 1))) {
usedNinthMoves.add("" + (3 * y + x + 1));
tempField2[y][x] = Player.getPlayer();
if (checkingWinX())
options.put(looseWithNineMoves, "" + (3 * y + x + 1));
else if (checkingWinO())
options.put(winWithNineMoves, "" + (3 * y + x + 1));
else if (checkingTie())
options.put(tie, "" + (3 * y + x + 1));
Player.changePlayer();
}
}
}
}
setMove();
}
private static void setMove() {
System.out.println("t");
Player.setPlayer("O");
if (options.containsKey(winWithOneMove)) {
Field.Fields[Field.getYOfField(options.get(winWithOneMove))][Field
.getXOfField(options.get(winWithOneMove))] = Player.getPlayer();
} else if (options.containsKey(winWithTwoMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithTwoMoves))][Field
.getXOfField(options.get(winWithTwoMoves))] = Player.getPlayer();
} else if (options.containsKey(winWithThreeMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithThreeMoves))][Field
.getXOfField(options.get(winWithThreeMoves))] = Player.getPlayer();
} else if (options.containsKey(winWithFourMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithFourMoves))][Field
.getXOfField(options.get(winWithFourMoves))] = Player.getPlayer();
} else if (options.containsKey(winWithFiveMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithFiveMoves))][Field
.getXOfField(options.get(winWithFiveMoves))] = Player.getPlayer();
} else if (options.containsKey(winWithSixMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithSixMoves))][Field
.getXOfField(options.get(winWithSixMoves))] = Player.getPlayer();
} else if (options.containsKey(winWithSevenMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithSevenMoves))][Field
.getXOfField(options.get(winWithSevenMoves))] = Player.getPlayer();
} else if (options.containsKey(winWithEightMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithEightMoves))][Field
.getXOfField(options.get(winWithEightMoves))] = Player.getPlayer();
} else if (options.containsKey(winWithNineMoves)) {
Field.Fields[Field.getYOfField(options.get(winWithNineMoves))][Field
.getXOfField(options.get(winWithNineMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithOneMove)) {
Field.Fields[Field.getYOfField(options.get(looseWithOneMove))][Field
.getXOfField(options.get(looseWithOneMove))] = Player.getPlayer();
} else if (options.containsKey(looseWithTwoMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithTwoMoves))][Field
.getXOfField(options.get(looseWithTwoMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithThreeMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithThreeMoves))][Field
.getXOfField(options.get(looseWithThreeMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithFourMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithFourMoves))][Field
.getXOfField(options.get(looseWithFourMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithFiveMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithFiveMoves))][Field
.getXOfField(options.get(looseWithFiveMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithSixMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithSixMoves))][Field
.getXOfField(options.get(looseWithSixMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithSevenMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithSevenMoves))][Field
.getXOfField(options.get(looseWithSevenMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithEightMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithEightMoves))][Field
.getXOfField(options.get(looseWithEightMoves))] = Player.getPlayer();
} else if (options.containsKey(looseWithNineMoves)) {
Field.Fields[Field.getYOfField(options.get(looseWithNineMoves))][Field
.getXOfField(options.get(looseWithNineMoves))] = Player.getPlayer();
}
else if (options.containsKey(tie)) {
Field.Fields[Field.getYOfField(options.get(tie))][Field.getXOfField(options.get(tie))] = Player.getPlayer();
}
else {
//erstes freies Feld belegen
for (int y = 0; y <= 2; y++) {
for (int x = 0; x<= 2; x++) {
if (Field.Fields[y][x].equals("" + (new Integer(3 * y + x + 1))))
Field.Fields[y][x] = Player.getPlayer();
}
}
}
}
private static boolean checkingWinX() {
// waagerecht
if ((tempField[0][0].equals("X")) && (tempField[0][1].equals("X")) && (tempField[0][2].equals("X")))
return true;
else if ((tempField[1][0].equals("X")) && (tempField[1][1].equals("X")) && (tempField[1][2].equals("X")))
return true;
else if ((tempField[2][0].equals("X")) && (tempField[2][1].equals("X")) && (tempField[2][2].equals("X")))
return true;
// senkrecht
else if ((tempField[0][0].equals("X")) && (tempField[1][0].equals("X")) && (tempField[2][0].equals("X")))
return true;
else if ((tempField[0][1].equals("X")) && (tempField[1][1].equals("X")) && (tempField[2][1].equals("X")))
return true;
else if ((tempField[0][2].equals("X")) && (tempField[1][2].equals("X")) && (tempField[2][2].equals("X")))
return true;
// diagonal
else if ((tempField[0][0].equals("X")) && (tempField[1][1].equals("X")) && (tempField[2][2].equals("X")))
return true;
else if ((tempField[0][2].equals("X")) && (tempField[1][1].equals("X")) && (tempField[2][0].equals("X")))
return true;
else
return false;
}
private static boolean checkingWinO() {
//waagerecht
if ((tempField[0][0].equals("O")) && (tempField[0][1].equals("O")) && (tempField[0][2].equals("O")))
return true;
else if ((tempField[1][0].equals("O")) && (tempField[1][1].equals("O")) && (tempField[1][2].equals("O")))
return true;
else if ((tempField[2][0].equals("O")) && (tempField[2][1].equals("O")) && (tempField[2][2].equals("O")))
return true;
// senkrecht
else if ((tempField[0][0].equals("O")) && (tempField[1][0].equals("O")) && (tempField[2][0].equals("O")))
return true;
else if ((tempField[0][1].equals("O")) && (tempField[1][1].equals("O")) && (tempField[2][1].equals("O")))
return true;
else if ((tempField[0][2].equals("O")) && (tempField[1][2].equals("O")) && (tempField[2][2].equals("O")))
return true;
// diagonal
else if ((tempField[0][0].equals("O")) && (tempField[1][1].equals("O")) && (tempField[2][2].equals("O")))
return true;
else if ((tempField[0][2].equals("O")) && (tempField[1][1].equals("O")) && (tempField[2][0].equals("O")))
return true;
else
return false;
}
private static boolean checkingTie() {
if (!checkingWinX() && !checkingWinO() && !Field.playable())
return true;
else
return false;
}
}