in Tik Tak Toe "KI" Fehler

Bitte aktiviere JavaScript!
Bitte kann mir jemand helfen ich finde den Fehler.
Normalerweise müsste er berechnen wo es am besten ist zu platzieren macht er aber nicht.

Java:
import java.util.Scanner;

public class Main {

    static void print(int[] a) {
        System.out.print("     1  2  3");
        System.out.printf("%n");
        System.out.print("A   ");
        switch (a[0]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖  ");
                break;
            case 2:
                System.out.print("⚪  ");
                break;
        }
        switch (a[1]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖  ");
                break;
            case 2:
                System.out.print("⚪  ");
                break;
        }
        switch (a[2]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖");
                break;
            case 2:
                System.out.print("⚪");
                break;
        }
        System.out.printf("%n");
        System.out.print("B   ");
        switch (a[3]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖  ");
                break;
            case 2:
                System.out.print("⚪  ");
                break;
        }
        switch (a[4]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖  ");
                break;
            case 2:
                System.out.print("⚪  ");
                break;
        }
        switch (a[5]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖");
                break;
            case 2:
                System.out.print("⚪");
                break;
        }
        System.out.printf("%n");
        System.out.print("C   ");
        switch (a[6]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖  ");
                break;
            case 2:
                System.out.print("⚪  ");
                break;
        }
        switch (a[7]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖  ");
                break;
            case 2:
                System.out.print("⚪  ");
                break;
        }
        switch (a[8]) {
            case 0:
                System.out.print("   ");
                break;
            case 1:
                System.out.print("✖");
                break;
            case 2:
                System.out.print("⚪");
                break;
        }
    }

    static int win(int[] a) {

        if (a[0] == a[1] && a[1] == a[2] && a[0] != 0) {
            return a[0];
        }
        if (a[3] == a[4] && a[4] == a[5] && a[3] != 0) {
            return a[3];
        }
        if (a[6] == a[7] && a[7] == a[8] && a[8] != 0) {
            return a[6];
        }

        if (a[0] == a[3] && a[3] == a[6] && a[0] != 0) {
            return a[0];
        }

        if (a[1] == a[4] && a[4] == a[7] && a[1] != 0) {
            return a[1];
        }

        if (a[2] == a[5] && a[5] == a[8] && a[2] != 0) {
            return a[2];
        }

        if (a[2] == a[5] && a[5] == a[8] && a[2] != 0) {
            return a[2];
        }

        if (a[0] == a[4] && a[4] == a[8] && a[0] != 0) {
            return a[0];
        }
        if (a[2] == a[4] && a[4] == a[6] && a[2] != 0) {
            return a[2];
        }
        return 0;
    }

    static int minmax(int[] a) {
        int p1 = 0;
        int p2 = 0;
        int p3 = 0;
        int p4 = 0;
        int pmax = 0;
        int max = 0;

        for (int i = 9; i > a.length; i++) {

            if (a[i] == 0) {

                a[i] = 2;
                for (int j = 9; j > a.length; j++) {
                    if (a[j] == 0) {
                        a[j] = 1;
                        if (win(a) == 2) {
                            p4 += 1;
                        } else if (win(a) == 1) {
                            p4 += -1;
                        } else
                            p4 += 0;
                        for (int k = 9; k > a.length; k++) {
                            if (a[k] == 0) {
                                a[k] = 1;
                                if (win(a) == 2) {
                                    p3 += 1;
                                } else if (win(a) == 1) {
                                    p3 += -1;
                                } else
                                    p3 += 0;

                                for (int l = 9; l > a.length; l++) {
                                    if (a[l] == 0) {
                                        a[l] = 1;
                                        if (win(a) == 2) {
                                            p2 += 1;
                                        } else if (win(a) == 1) {
                                            p2 += -1;
                                        } else
                                            p2 += 0;

                                        for (int m = 9; m > a.length; m++) {
                                            if (a[m] == 0) {
                                                a[m] = 2;
                                                if (win(a) == 2) {
                                                    p1 += 1;
                                                } else if (win(a) == 1) {
                                                    p1 += -1;
                                                } else
                                                    p1 += 0;
                                                a[m] = 0;
                                            }
                                        }
                                    }
                                    if (p1 > p2) {
                                        p2 = p1;

                                    }
                                    a[l] = 0;
                                    p1 = 0;

                                }
                            }
                            if (p2 > p3) {
                                p2 = p3;

                            }
                            p3 = 0;
                            a[k] = 0;
                        }
                    }
                    if (p3 > p4) {
                        p4 = p3;

                    }
                    p3 = 0;
                    a[j] = 0;

                }
            }

            if (p4 > pmax) {
                pmax = p4;
                max = i;
            }
            p4 = 0;
            a[i] = 2;
        }
        return max;
    }


    static void clean() {
        for (int i = 0; i > 20; i++) {
            System.out.print("\n");
        }
    }

    public static void main(String[] args) {
        int[] f = new int[9];


        for (int i = 0; i == 0; ) {
            print(f);
            Scanner sc = new Scanner(System.in);
            System.out.printf("%n");
            System.out.print("Feld:");
            int e = sc.nextInt();
            f[e] = 1;
            clean();


            if (win(f) == 1 && win(f) == 2) {
                i = 1;
            }

            f[minmax(f)] = 2;
            if (win(f) == 1 && win(f) == 2) {
                i = 1;
            }
            clean();
        }


    }
}
 
Sorry, aber evtl. einmal den Code etwas leserlicher schreibe, wenn du da Hilfe haben willst.

- Namen so vergeben, dass man weiß, was in einer Variable drin ist, oder was eine Methode macht.
- eine for Schleife zählt normalerweise, das tut Deine nicht. Und i der for Schleife ist nur 0 und 1? ==> while statt for und eine Variable für Abbruch vom Typ Boolean bitte.
- Code der sich wiederholt lagert man bitte in eine Methode aus. Also z.B. bei der Ausgabe eine Methode schreibe, die die auszugebenden Zeichen zurück geben

Und Dein Code ist unverständlich. Was macht win genau? Und was soll da dann der Sinn von win(f) == 1 && win(f) == 2 sein? f wird nicht verändert, daher wird das nie wahr sein. win(f) wird nicht 1 und 2 sein, sondern wenn dann doch 1 oder 2 ....
 
Wenn du den Wert der einen Spielers durch 1 und den des anderen Spielers durch -1 repräsentierst, statt durch 1 und 2, dann kannst du mit ganz einfacher Arithmetik den minmax-Algorithmus umsetzen. Und vorallem solltest du Rekursion oder zumindest Iteration verwenden und nicht zwanzig ineinander geschachtelte if-Statements bauen...
Mit Rekursion könnte das so aussehen:
Java:
import static java.util.stream.IntStream.range;
import java.util.stream.IntStream;
public class TicTacToe {
  private static final int NUM_CELLS = 9;
  private static final int LENGTH = 3;
  private static final int WIN_SCORE_BOOST = 10;
  private static final int PLAYER_ONE = 1;
  private static final int PLAYER_TWO = -1;
  private static final int NO_MOVE = -1;
  private static final int NO_PLAYER = 0;
  private static class IntHolder {
    private static final IntHolder DONT_CARE = new IntHolder();
    private int value = NO_MOVE;
  }
  private static IntStream columns() {
    return range(0, LENGTH);
  }
  private static IntStream rows() {
    return range(0, LENGTH);
  }
  private static IntStream diagonal() {
    return range(0, LENGTH);
  }
  private static int at(int[] board, int row, int col) {
    return board[row * LENGTH + col];
  }
  private static int reverse(int rowOrColumn) {
    return LENGTH-1 - rowOrColumn;
  }
  private static boolean secondaryDiagonalMatches(int[] board, int player) {
    return diagonal().allMatch(rowcol -> at(board, rowcol, reverse(rowcol)) == player);
  }
  private static boolean mainDiagonalMatches(int[] board, int player) {
    return diagonal().allMatch(rowcol -> at(board, rowcol, rowcol) == player);
  }
  private static boolean anyColumnMatches(int[] board, int player) {
    return columns().anyMatch(col -> rows().allMatch(row -> at(board, row, col) == player));
  }
  private static boolean anyRowMatches(int[] board, int player) {
    return rows().anyMatch(row -> columns().allMatch(col -> at(board, row, col) == player));
  }
  private static boolean hasWon(int[] board, int player) {
    return anyRowMatches(board, player) 
        || anyColumnMatches(board, player) 
        || mainDiagonalMatches(board, player) 
        || secondaryDiagonalMatches(board, player);
  }
  private static int winner(int[] board) {
    return hasWon(board, PLAYER_ONE) ? PLAYER_ONE : hasWon(board, PLAYER_TWO) ? PLAYER_TWO : NO_PLAYER;
  }
  private static int minimax(int[] board, int player, IntHolder moveOut) {
    int winner = winner(board);
    return winner != NO_PLAYER
        ? winner * player * WIN_SCORE_BOOST
        : range(0, NUM_CELLS)
         .filter(p -> board[p] == 0)
         .reduce(-2, (s, p) -> scoreMove(board, player, s, p, moveOut));
  }
  private static int scoreMove(int[] board, int player, int scoreSoFar, int p, IntHolder moveOut) {
    makeMove(board, p, player);
    int moveScore = -minimax(board, -player, IntHolder.DONT_CARE);
    if (moveScore > scoreSoFar) {
      scoreSoFar = moveScore;
      moveOut.value = p;
    }
    undoMove(board, p);
    return scoreSoFar;
  }
  private static int undoMove(int[] board, int p) {
    return board[p] = 0;
  }
  private static int makeMove(int[] board, int p, int player) {
    return board[p] = player;
  }
  public static void main(String[] args) {
    int[] board = {
      0, -1, 1,
      0, 0, 0,
      1, 0, 0,
    };
    IntHolder move = new IntHolder();
    minimax(board, PLAYER_ONE, move);
    System.out.println(move.value); // <- 4
  }
}
 
Zuletzt bearbeitet:
Passende Stellenanzeigen aus deiner Region:

Neue Themen

Oben