Tic-Tac-Toe - falscher Gewinner!



  • hallo!

    also die aufgabenstellung lautet, dass wir ein programm schreiben soll, das alles beinhaltet, was wir bis jetzt gelernt haben (for-, while-loop, if-anweisung, arrays, unterprogramme bzw funktionen, adressoperator, pointer).
    tja - ich hab ein tic-tac-toe geschrieben....und nicht wirklich viele sachen davon verwendet 🙄

    abgesehen davon funktioniert es nicht richtig. das prob ist, dass es nach kurzer zeit einen gewinner ausspuckt - obwohl das spiel noch gar nicht aus ist.
    ich hab zwar schon bei anderen tic-tac-toe codes nachgeschaut, aber die sind irgendwie alle ganz anders aufgebaut als meins.

    und wie kann ich es machen, dass die felder leer sind - bis jmd sein zeichen reinsetzt?

    ich hoffe, irgendjemand hat zeit mir mal kurz unter die arme zu greifen... 🙂

    das ist der code:

    //Programm Tic-Tac-Toe
    
    #include <iostream>
    using namespace std;
    
     bool x = true;
     bool y = false;
     int i, g;
     int j, h;
     int mat[3][3] = {{9, 9, 9}, {9, 9, 9}, {9, 9, 9}};
     int c;
    
    int main() {
    
        cout << "Programm Tic-Tac-Toe.cpp" << endl << endl << endl << endl;
    
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "| " << " 00" << " | " << " 01" << " | " << " 02" << " |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "| " << " 10" << " | " << " 11" << " | " << " 12" << " |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "| " << " 20" << " | " << " 21" << " | " << " 22" << " |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << endl << endl << endl;
        cout << " '9' bdeutet 'leeres Feld!'" << endl;
    
    for (int n = 1; n < 10;) {
    
        cout << "Spieler 0 (0): Welche Zeile?" << endl;
        cin >> i;
        cout << endl << "Welche Spalte?" << endl;
        cin >> j;
        cout << endl;
        cout << "Feld[" << i << "][" << j << "]" << endl;
    
        mat[i][j] = y;
    
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << mat[0][0] << "  |  " << mat[0][1] << "  |  " << mat[0][2] << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << mat[1][0] << "  |  " << mat[1][1] << "  |  " << mat[1][2] << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << mat[2][0] << "  |  " << mat[2][1] << "  |  " << mat[2][2] << "  |"  << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << endl << endl << endl;
    
        cout << "Spieler 1 (1): Welche Zeile?" << endl;
        cin >> i;
        cout << endl << "Welche Spalte?" << endl;
        cin >> j;
        cout << endl;
        cout << "Feld[" << i << "][" << j << "]" << endl;
    
        mat[i][j] = x;
    
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << mat[0][0] << "  |  " << mat[0][1] << "  |  " << mat[0][2] << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << mat[1][0] << "  |  " << mat[1][1] << "  |  " << mat[1][2] << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << mat[2][0] << "  |  " << mat[2][1] << "  |  " << mat[2][2] << "  |"  << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << endl << endl << endl;
    
    int *gewinner = 0;
    
    if ((mat[0][0] == mat[0][1] == mat[0][2])||(mat[0][0] == mat[1][0] == mat[2][0])||(mat[0][0] == mat[1][1] == mat[2][2])) {
    
                  gewinner = & mat[0][0];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
    
    if ((mat[1][0] == mat[1][1] == mat[1][2])||(mat[0][1] == mat[1][1] == mat[2][1])) {
    
                  gewinner = & mat[1][1];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
    
    if ((mat[2][0] == mat[2][1] == mat[2][2])||(mat[0][2] == mat[1][2] == mat[2][2])) {
    
                  gewinner = & mat[2][2];
    
                   cout << "Der Gewinner ist Spieler " << *gewinner << endl;
    }
    }
    }
    else {n = n+1;}
    
    }
    
        cin >> c;
    
    }
    


  • Hallo

    Zum eigentlichen Problem : So ein Dreifachvergleich ist die Ursache

    (mat[0][0] == mat[0][1] == mat[0][2])
    

    Denn der Compiler wertet diese Vergleiche nicht wie du denkst auf einmal aus sondern nacheinander.
    Also zuerst

    mat[0][0] == mat[0][1]
    

    Da kommt ein boolscher Wert raus (true oder false)
    und dieser Ergebniswert wird dann mit dem dritten Argument verglichen (in ein int umgewandelt), nicht aber der eigentliche Feldinhalt!

    (bool) == mat[0][2]
    

    Umgehen kannst du das nur mit sauberer Klammerung

    ((mat[0][0] == mat[0][1]) && (mat[0][1] == mat[0][2]))
    

    bis bald
    akari



  • aha - dankeschön :p

    und weißt du auch vl wie ich es machen kann, damit das feld am anfang leer ist?



  • Hallo

    Ich verstehe nicht wo das Problem ist. Du initialisierst ja die Felder des Arrays schon mit 9, was bei dir bedeutet das es leer ist. Also must du die Ausgabe nur umschreiben, so das bei einem Feldwert von 9 ein Leerzeichen ausgegeben wird, ansonsten der eigentliche Wert.

    bis bald
    akari



  • nein, das lässte er nicht zu. wahrscheinlich, weil ich das ganze als "int" definiert hab...

    bool x = true;
     bool y = false;
     int i, g;
     int j, h;
     int mat[3][3] = {{ ,  ,  }, { ,  ,  }, { ,  ,  }};
     int c;
    

    dabei markiert er mir die zeile mit der def. der matrix als fehler, wenn ich die 9er rausstreiche.

    und das mit dem logischen oder hab ich auch gemacht, aber wenn jmd gewonnen hat, spielt er einfach weiter. vl stimmt was mit der klammersetzung nicht?

    int *gewinner = 0;
    
    if (((mat[0][0] == mat[0][1]) && (mat[0][1] == mat[0][2])) || ((mat[0][0] == mat[1][0]) && (mat[1][0] == mat[2][0])) || ((mat[0][0] == mat[1][1]) && (mat[1][1] == mat[2][2]))) {
    
                  gewinner = & mat[0][0];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
    
    if (((mat[1][0] == mat[1][1]) && (mat[1][1] == mat[1][2])) || ((mat[0][1] == mat[1][1]) && (mat[1][1] == mat[2][1]))) {
    
                  gewinner = & mat[1][1];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
    
    if (((mat[2][0] == mat[2][1]) && (mat[2][1] == mat[2][2])) || ((mat[0][2] == mat[1][2]) && (mat[1][2] == mat[2][2]))) {
    
                  gewinner = & mat[2][2];
    
                   cout << "Der Gewinner ist Spieler " << *gewinner << endl;
    }
    }
    }
    


  • Hallo

    nein, das lässte er nicht zu. wahrscheinlich, weil ich das ganze als "int" definiert hab...

    Na du must schon ein bißchen mehr machen. Zum Beispiel eine Funktion schreiben...

    char Out(int value)
    {
      if (value == 9)
        return ' ';
      if (value == 0)
        return '0';
      return '1';
    }
    
    // Aufrufbeispiel :
    cout << Out(mat[0][0]);
    

    und das mit dem logischen oder hab ich auch gemacht, aber wenn jmd gewonnen hat, spielt er einfach weiter. vl stimmt was mit der klammersetzung nicht?

    Du must ja auch in den Sieg-Fällen die Schleife bzw. das Programm verlassen.

    bis bald
    akari



  • ok - ich hab jetzt am anfang leere kästchen.

    aber jetzt lässt er jeden nur einmal drankommen und schreibt dann "der gewinner ist spieler 9 "

    ich poste nochmal den ganzen code 😕

    //Programm Tic-Tac-Toe
    
    #include <iostream>
    using namespace std;
    
     bool x = true;
     bool y = false;
     int i, g;
     int j, h;
     int mat[3][3] = {{9 ,9 ,9 }, {9 ,9 ,9 }, {9 ,9 ,9 }};
     int c;
    
    //Prototyp:
    
    char Out(int value);
    
    char Out(int value){
    
      if (value == 9)
        return ' ';
      if (value == 0)
        return '0';
      return '1';
    }
    
    int main() {
    
        cout << "Programm Tic-Tac-Toe.cpp" << endl << endl << endl << endl;
    
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "| " << " 00" << " | " << " 01" << " | " << " 02" << " |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "| " << " 10" << " | " << " 11" << " | " << " 12" << " |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "| " << " 20" << " | " << " 21" << " | " << " 22" << " |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << endl << endl << endl;
    
    for (int n = 1; n < 10;) {
    
        cout << "Spieler 0 (0): Welche Zeile?" << endl;
        cin >> i;
        cout << endl << "Welche Spalte?" << endl;
        cin >> j;
        cout << endl;
        cout << "Feld[" << i << "][" << j << "]" << endl;
    
        mat[i][j] = y;
    
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << Out(mat[0][0]) << "  |  " << Out(mat[0][1]) << "  |  " << Out(mat[0][2]) << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << Out(mat[1][0]) << "  |  " << Out(mat[1][1])<< "  |  " << Out(mat[1][2]) << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << Out(mat[2][0]) << "  |  " << Out(mat[2][1]) << "  |  " << Out(mat[2][2]) << "  |"  << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << endl << endl << endl;
    
        cout << "Spieler 1 (1): Welche Zeile?" << endl;
        cin >> i;
        cout << endl << "Welche Spalte?" << endl;
        cin >> j;
        cout << endl;
        cout << "Feld[" << i << "][" << j << "]" << endl;
    
        mat[i][j] = x;
    
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << Out(mat[0][0]) << "  |  " << Out(mat[0][1]) << "  |  " << Out(mat[0][2]) << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << Out(mat[1][0]) << "  |  " << Out(mat[1][1])<< "  |  " << Out(mat[1][2]) << "  |" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << "|  " << Out(mat[2][0]) << "  |  " << Out(mat[2][1]) << "  |  " << Out(mat[2][2]) << "  |"  << endl;
        cout << "          " << "| " << "   " << " | " << "   " << " | " << "   " << " |" << endl;
        cout << "          " << " -----------------" << endl;
        cout << endl << endl << endl;
    
    int *gewinner = 0;
    
    if (((mat[0][0] == mat[0][1]) && (mat[0][1] == mat[0][2])) || ((mat[0][0] == mat[1][0]) && (mat[1][0] == mat[2][0])) || ((mat[0][0] == mat[1][1]) && (mat[1][1] == mat[2][2]))) {
    
                  gewinner = & mat[0][0];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
                  break;
    
    }       
    if (((mat[1][0] == mat[1][1]) && (mat[1][1] == mat[1][2])) || ((mat[0][1] == mat[1][1]) && (mat[1][1] == mat[2][1]))) {
    
                  gewinner = & mat[1][1];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
                  break;
    }
    if (((mat[2][0] == mat[2][1]) && (mat[2][1] == mat[2][2])) || ((mat[0][2] == mat[1][2]) && (mat[1][2] == mat[2][2]))) {
    
                  gewinner = & mat[2][2];
    
                   cout << "Der Gewinner ist Spieler " << *gewinner << endl;
                   break;
    
    }
    else {n = n+1;}
    
    }
    
        cin >> c;
    
    }
    


  • if (((mat[0][0] == mat[0][1]) && (mat[0][1] == mat[0][2])) || ((mat[0][0] == mat[1][0]) && (mat[1][0] == mat[2][0])) || ((mat[0][0] == mat[1][1]) && (mat[1][1] == mat[2][2]))) {
    ***
    }
    

    Öhh du vergleichst ob Feld[0][0] dasselbe ist wie Feld [0][1] und wenn dieses Feld dasselbe ist wie Feld[0][2] hat derjenige gewonnen.

    Ist auch logisch, denn du vergleichst ja nicht den Inhalt. 😉 Sprich: Wenn alle Felder z.B. in der obere waagerechten Zeile leer sind, wird reingesprungen und gesagt 'gewonnen'. Du musst das so abändern das der Inhalt vergleicht wird, nicht die Felder an sich.

    //Edit

    Das gilt für jede Gewinner Zeile.



  • ah - stimmt.....

    ich hab jetzt einfach die bedingung gestellt, dass die werte entweder 0 oder 1 sein müssen.

    nur.....*gg*.....jetzt kommt wieder gar kein gewinner 😞 .... :p

    int *gewinner = 0;
    
    if (((mat[0][0] == mat[0][1]) && (mat[0][1] == mat[0][2]) && (mat[0][0] == (0 && 1))) || ((mat[0][0] == mat[1][0]) && (mat[1][0] == mat[2][0]) && (mat[0][0] == (0 && 1))) || ((mat[0][0] == mat[1][1]) && (mat[1][1] == mat[2][2]) && (mat[0][0] == (0 && 1)))) {
    
                  gewinner = & mat[0][0];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
                  break;
    
    }       
    if ((((mat[1][0] == mat[1][1]) && (mat[1][1] == mat[1][2]) && (mat[1][0] == (1 && 0))) || ((mat[0][1] == mat[1][1]) && (mat[1][1] == mat[2][1]) && (mat[0][1] == (1 && 0))))) {
    
                  gewinner = & mat[1][1];
    
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
                  break;
    }
    if ((((mat[2][0] == mat[2][1]) && (mat[2][1] == mat[2][2]) && (mat[2][0] == (1 && 0))) || ((mat[0][2] == mat[1][2]) && (mat[1][2] == mat[2][2]) && (mat[0][2] == (1 && 0))))) {
    
                  gewinner = & mat[2][2];
    
                   cout << "Der Gewinner ist Spieler " << *gewinner << endl;
                   break;
    
    }
    else {n = n+1;}
    


  • Dharma schrieb:

    int *gewinner = 0;
        
    if (((mat[0][0] == mat[0][1]) && (mat[0][1] == mat[0][2]) && (mat[0][0] == (0 && 1))) || ((mat[0][0] == mat[1][0]) && (mat[1][0] == mat[2][0]) && (mat[0][0] == (0 && 1))) || ((mat[0][0] == mat[1][1]) && (mat[1][1] == mat[2][2]) && (mat[0][0] == (0 && 1)))) {
                  
                  gewinner = & mat[0][0];
                  
                  cout << "Der Gewinner ist Spieler " << *gewinner << endl;
                  break;
                  
    }     ...
    

    Dein Problem ist der Schnipsel mat[0][0] == (0 && 1). Das was du da geschrieben hast kann nicht funktionieren. Du verlangst als auswertung das mat[0][0] gleichzeitig 0 und 1 ist, um es übersichtlich zu machen solltest du anstelle davon, das hier machen:

    (mat[0][0] == 0 || mat[0][0] == 1)
    

    oder um es richtig gut übersichtlich zu machen, so: 😉

    int gewinner = 0;
    
    for(gewinner = 0;gewinner <= 1;gewinner++)
    {
        if((mat[0][0] == gewinner && mat[0][1] == gewinner && mat[0][2] == gewinner) ||
           (mat[1][0] == gewinner && mat[1][1] == gewinner && mat[1][2] == gewinner) ||
           (mat[2][0] == gewinner && mat[2][1] == gewinner && mat[2][2] == gewinner))    //frage die horizontale  ab.
        {
            cout << "Der Gewinner ist Spieler " << gewinner << endl;
            break;
        }
    
        if((mat[0][0] == gewinner && mat[1][0] == gewinner && mat[2][0] == gewinner) ||
           (mat[0][1] == gewinner && mat[1][1] == gewinner && mat[2][1] == gewinner) ||
           (mat[0][2] == gewinner && mat[1][2] == gewinner && mat[2][2] == gewinner))    //frage die vertikalen ab.
        {
            cout << "Der Gewinner ist Spieler " << gewinner << endl;
            break;
        }
    
        if((mat[0][0] == gewinner && mat[1][1] == gewinner && mat[2][2] == gewinner) ||
           (mat[0][2] == gewinner && mat[1][1] == gewinner && mat[2][0] == gewinner))    //frage die diagonalen ab.
        {
            cout << "Der Gewinner ist Spieler " << gewinner << endl;
            break;
        }
    }
    

    //Edit

    Und wenn du jetzt noch eine bool Variable reinsetzt die sagt das ein Spieler gewonnen hast kannst du darauf auch noch dementsprechend reagieren, Spiel neustarten, etc... was mir allerdings nicht ganz klar ist... wieso hast du diese Zeile

    else{n = n+1;}
    

    abhängig vom Gewinn gemacht?

    Mach ein

    n++;
    

    draus und gut ist dem, funzt auch. 😉



  • oh....dankeschön 😃

    jetzt muss ich dann noch falsche eingaben abfangen.

    danke für die hilfe ihr beiden!

    lg
    Dharma


Anmelden zum Antworten