Cali
Cali

Reputation: 25

Do-loop isn't terminating in tic-tac-toe game

the wincondition is in siegBedingung(). I want the do-loop to stop if siegBedingung() is true but even tho it seems like the wincondition is met, the loop doesn't terminate. ausgewählteReihe is the chosenRow and ausgewählteSpalte is the chosenColumn of the player.

My thoughts: If player 1 choses 2,2 and he got already his mark on 1,2 and 3,2 it should verify the second win condition and return true, so the while condition of the do loop is met and it terminates.

import java.util.Scanner;

public class TTT {
    static Scanner sc =  new Scanner(System.in);
    static final int spalte = 3;
    static final int reihe = 3;
    static int[][] feld = new int[reihe][spalte];
    static int leer = 0;
    static int kreuz = 1;
    static int kreis = 2;
    static int spieler;
    static int ausgewählteReihe;
    static int ausgewählteSpalte;
    static int spielerSymbol;
    static boolean siegBedingung;

public static void main(String[] args) {


    start();
    do {
    brett();
    zug();
    siegBedingung();
    }while(siegBedingung == false);
    System.out.println("Spieler " + spielerSymbol + " gewinnt.");
}

public static void start() {
    for(int zählerReihe = 0; zählerReihe < reihe; zählerReihe++) {
        for(int zählerSpalte = 0; zählerSpalte < spalte; zählerSpalte++) {
            feld[zählerReihe][zählerSpalte] = leer;
        }
    }
    spielerSymbol = 1;
}

public static void zug() {
    boolean validInput = false;
    do {
        if(spielerSymbol == 1) {
            System.out.println("Spieler 1 (Kreuz) ist am Zug."); 
        }
        if(spielerSymbol == 2) {
            System.out.println("Spieler 2 (Kreis) ist am Zug.");
        }
        System.out.println("Geben Sie die gewünschte Reihe ein.");
        ausgewählteReihe = sc.nextInt() -1;
        System.out.println("Geben Sie die gewünschte Spalte ein.");
        ausgewählteSpalte = sc.nextInt() -1;
        if(ausgewählteReihe >= 0 && ausgewählteReihe < reihe && ausgewählteSpalte >= 0 && ausgewählteSpalte < spalte && feld[ausgewählteReihe][ausgewählteSpalte] == leer) {
            feld[ausgewählteReihe][ausgewählteSpalte] = spielerSymbol;
            validInput=true;
        } else {
            System.out.println("Das ausgewählte Feld ist nicht gültig.");
        }
    }while(!validInput==true);
    spielerSymbol = (spielerSymbol == 1) ? 2 : 1;
}

public static boolean siegBedingung() {
    return(feld[ausgewählteReihe][0] == spielerSymbol && feld[ausgewählteReihe][1] == spielerSymbol && feld[ausgewählteReihe][2] == spielerSymbol
    || feld[0][ausgewählteSpalte] == spielerSymbol && feld[1][ausgewählteSpalte] == spielerSymbol && feld[2][ausgewählteSpalte] == spielerSymbol
    || feld[1][1] == spielerSymbol && feld[2][2] == spielerSymbol && feld[0][0] == spielerSymbol
    || feld[0][2] == spielerSymbol && feld[1][1] == spielerSymbol && feld[2][0] == spielerSymbol);
}
public static void brett() {
    for(int zählerReihe = 0; zählerReihe < reihe; zählerReihe++) {
        for(int zählerSpalte = 0; zählerSpalte < spalte; zählerSpalte++) {
            System.out.print("|");
            zelle(feld[zählerReihe][zählerSpalte]);
        }
        System.out.print("|");
        System.out.println();
    }
}

public static void zelle(int inhalt) {
    switch(inhalt) {
    case 0: System.out.print("_"); break;
    case 1: System.out.print("X"); break;
    case 2: System.out.print("O"); break;
    }
}

}

Upvotes: 1

Views: 62

Answers (1)

JohnnyAW
JohnnyAW

Reputation: 2876

so I tested your code and it worked. I needed this things to change:

start();
do {
    spielerSymbol = (spielerSymbol == 1) ? 2 : 1;
    brett();
    zug();
} while (!siegBedingung());
System.out.println("Spieler " + spielerSymbol + " gewinnt.");

in zug() remove the last line

change siegBedingung like the othe user suggested:

public static boolean siegBedingung() {
    return (feld[ausgewählteReihe][0] == spielerSymbol && feld[ausgewählteReihe][1] == spielerSymbol
            && feld[ausgewählteReihe][2] == spielerSymbol)
            || (feld[0][ausgewählteSpalte] == spielerSymbol && feld[1][ausgewählteSpalte] == spielerSymbol
                    && feld[2][ausgewählteSpalte] == spielerSymbol)
            || (feld[1][1] == spielerSymbol && feld[2][2] == spielerSymbol && feld[0][0] == spielerSymbol)
            || (feld[0][2] == spielerSymbol && feld[1][1] == spielerSymbol && feld[2][0] == spielerSymbol);
}

Upvotes: 1

Related Questions