Tic tac toe

dotacola

Mitglied
hihi ihr Leute :) ich hab ein Programm in Java geschrieben das das spiel TIC TAC TOE nachstellen soll. ich komm nur leider bei dem Punkt nicht weiter, bei dem nicht beide Spieler auf ein Feld setzten können :shock: . Wenn jemand gerade Zeit/Lust hat mir bei diesem kleinen Problem zu helfen würde ich ihm den Quelltext schicken:) Wäre für eure Hilfe echt dankbar!
Lg dotacola
 

Gossi

Bekanntes Mitglied
Poste den Code doch hier und schreibe dazu was dein Problem ist und wie du versucht hast es zu Lösen, sonst kommt es hier so rüber als wenn jemand deine Arbeit machen soll weil du zu faul bist ;)
 

dotacola

Mitglied
Java:
import Prog1Tools.*;

public class Tic4 {

  public static void main (String[] args) {
    go();
  }
  
  public static void go (  ) {

    //Initialisierung
    String a1=" ";
    String a2=" ";
    String a3=" ";
    String b1=" ";
    String b2=" ";
    String b3=" ";
    String c1=" ";
    String c2=" ";
    String c3=" ";
    int S1=0;
    int S2=0;
    int A=0;
    int G1=0;
    int G2=0;
    
    //Anleitung
    System.out.println("****************************TIC-TAC-TOE******************************");
    System.out.println("**                           ANLEITUNG:                            **");
    System.out.println("**                                                                 **");
    System.out.println("**   Gegeben ist folgendes Feld mit 9 nummerierten Feldern:        **");
    System.out.println("**                                                                 **");
    System.out.println("**                            1 |2 |3                              **");
    System.out.println("**                            -- -- --                             **");
    System.out.println("**                            4 |5 |6                              **");
    System.out.println("**                            -- -- --                             **");
    System.out.println("**                            7 |8 |9                              **");
    System.out.println("**                                                                 **");
    System.out.println("**   Ihr koennt nun abwechselnd eure Felder waehlen.               **");
    System.out.println("**   Gewonnen hat der Spieler, der zuerst 3 Felder hintereinander: **");
    System.out.println("**                          -Senkrecht                             **");
    System.out.println("**                          -Waagrecht                             **");
    System.out.println("**                          -Diagonal                              **");
    System.out.println("**   gesetzt hat.                                                  **");
    System.out.println("**   Viel Spass beim Spielen von TIC-TAC-TOE:                      **");
    System.out.println("*********************************************************************");
    System.out.println("");
    System.out.println("DAS SPIEL BEGINNT:");
    System.out.println("");


   //Schleife
    do {
   
    //Abfrage Kreuz
    S1=IOTools.readInteger("Kreuz, waehle ein Feld: ");
     if ( S1==1 ) {
      if ( a1==" " ) {
        a1="X";

      }
     }
     if ( S1==2 ) {
      if ( a2==" " ) {
        a2="X";

      }
     }
     if ( S1==3 ) {
      if ( a3==" " ) {
        a3="X";

      }
     }
     if ( S1==4 ) {
      if ( b1==" " ) {
        b1="X";

      }
     }
     if ( S1==5 ) {
      if ( b2==" " ) {
        b2="X";

      }
     }
     if ( S1==6 ) {
      if ( b3==" " ) {
        b3="X";

      }
     }
     if ( S1==7 ) {
      if ( c1==" " ) {
        c1="X";
      }
     }
     if ( S1==8 ) {
      if ( c2==" " ) {
        c2="X";

      }
     }
     if ( S1==9 ) {
      if ( c3==" " ) {
        c3="X";

      }
     }


    //Anzeige
    System.out.println("\n"+a1+" |"+a2+" |"+a3+"      1 |2 |3");
    System.out.println("-- -- --     -- -- --");
    System.out.println(b1+" |"+b2+" |"+b3+"      4 |5 |6");
    System.out.println("-- -- --     -- -- --");
    System.out.println(c1+" |"+c2+" |"+c3+"      7 |8 |9\n");
    
    //Kontrolle X
     if ( a1=="X" ) {
      if ( a2=="X" ) {
        if (a3=="X" ) {
         G1=1;
         A=1;
        }
      }
     }
     if ( b1=="X" ) {
      if ( b2=="X" ) {
        if (b3=="X" ) {
         G1=1;
         A=1;
        }
      }
     }
     if ( c1=="X" ) {
      if ( c2=="X" ) {
        if (c3=="X" ) {
         G1=1;
         A=1;
        }
      }
     }
     if ( a1=="X" ) {
      if ( b1=="X" ) {
        if (c1=="X" ) {
         G1=1;
         A=1;
        }
      }
     }
     if ( a2=="X" ) {
      if ( b2=="X" ) {
        if (c2=="X" ) {
         G1=1;
         A=1;
        }
      }
     }
     if ( a3=="X" ) {
      if ( b3=="X" ) {
        if (c3=="X" ) {
         G1=1;
         A=1;
        }
      }
     }
     if ( a1=="X" ) {
      if ( b2=="X" ) {
        if (c3=="X" ) {

         G1=1;
         A=1;
        }
      }
     }
     if ( a3=="X" ) {
      if ( b2=="X" ) {
        if (c1=="X" ) {
         G1=1;
         A=1;
        }
      }
     }

    // Kontrolle O
     if ( a1=="O" ) {
      if ( a2=="O" ) {
        if (a3=="O" ) {
         G2=1;
         A=1;
        }
      }
     }
     if ( b1=="O" ) {
      if ( b2=="O" ) {
        if (b3=="O" ) {
         G2=1;
         A=1;
        }
      }
     }
     if ( c1=="O" ) {
      if ( c2=="O" ) {
        if (c3=="O" ) {
         G2=1;
         A=1;
        }
      }
     }
     if ( a1=="O" ) {
      if ( b1=="O" ) {
        if (c1=="O" ) {
         G2=1;
         A=1;
        }
      }
     }
     if ( a2=="O" ) {
      if ( b2=="O" ) {
        if (c2=="O" ) {
         G2=1;
         A=1;
        }
      }
     }
     if ( a3=="O" ) {
      if ( b3=="O" ) {
        if (c3=="O" ) {
         G2=1;
         A=1;
        }
      }
     }
     if ( a1=="O" ) {
      if ( b2=="O" ) {
        if (c3=="O" ) {
         G2=1;
         A=1;
        }
      }
     }
     if ( a3=="O" ) {
      if ( b2=="O" ) {
        if (c1=="O" ) {
         G2=1;
         A=1;
        }
      }
     }


    //Kontrolle Unentschieden
     if ( a1!=" " ) {
       if ( a2!=" " ) {
         if ( a3!=" " ) {
           if ( b1!=" " ) {
             if ( b2!=" " ) {
               if ( b3!=" " ) {
                 if ( c1!=" " ) {
                   if ( c2!=" " ) {
                     if ( c3!=" " ) {
                      A=1;
                     }
                   }
                 }
               }
             }
           }
         }
       }
     }

    if ( A!=1 ) {
    
    //Abfrage Kreis
    S2=IOTools.readInteger("Kreis, waehle ein Feld: ");
     if ( S2==1 ) {
      if ( a1==" " ) {
        a1="O";
      }
     }
     if ( S2==2 ) {
      if ( a2==" " ) {
        a2="O";
      }
     }
     if ( S2==3 ) {
      if ( a3==" " ) {
        a3="O";
      }
     }
     if ( S2==4 ) {
      if ( b1==" " ) {
        b1="O";
      }
     }
     if ( S2==5 ) {
      if ( b2==" " ) {
        b2="O";
      }
     }
     if ( S2==6 ) {
      if ( b3==" " ) {
        b3="O";
      }
     }
     if ( S2==7 ) {
      if ( c1==" " ) {
        c1="O";
      }
     }
     if ( S2==8 ) {
      if ( c2==" " ) {
        c2="O";
      }
     }
     if ( S2==9 ) {
      if ( c3==" " ) {
        c3="O";
      }
     }

    //Anzeige
    System.out.println("\n"+a1+" |"+a2+" |"+a3+"      1 |2 |3");
    System.out.println("-- -- --     -- -- --");
    System.out.println(b1+" |"+b2+" |"+b3+"      4 |5 |6");
    System.out.println("-- -- --     -- -- --");
    System.out.println(c1+" |"+c2+" |"+c3+"      7 |8 |9\n");

    }
    
    //Kontrolle X
    if ( a1=="X" ) {
      if ( a2=="X" ) {
        if (a3=="X" ) {
         G1=1;
         A=1;
        }
      }
    }
    if ( b1=="X" ) {
      if ( b2=="X" ) {
        if (b3=="X" ) {
         G1=1;
         A=1;
        }
      }
    }
    if ( c1=="X" ) {
      if ( c2=="X" ) {
        if (c3=="X" ) {
         G1=1;
         A=1;
        }
      }
    }
    if ( a1=="X" ) {
      if ( b1=="X" ) {
        if (c1=="X" ) {
         G1=1;
         A=1;
        }
      }
    }
    if ( a2=="X" ) {
      if ( b2=="X" ) {
        if (c2=="X" ) {
         G1=1;
         A=1;
        }
      }
    }
    if ( a3=="X" ) {
      if ( b3=="X" ) {
        if (c3=="X" ) {
         G1=1;
         A=1;
        }
      }
    }
    if ( a1=="X" ) {
      if ( b2=="X" ) {
        if (c3=="X" ) {
         G1=1;
         A=1;
        }
      }
    }
    if ( a3=="X" ) {
      if ( b2=="X" ) {
        if (c1=="X" ) {
         G1=1;
         A=1;
        }
      }
    }

    // Kontrolle O
    if ( a1=="O" ) {
      if ( a2=="O" ) {
        if (a3=="O" ) {
         G2=1;
         A=1;
        }
      }
    }
    if ( b1=="O" ) {
      if ( b2=="O" ) {
        if (b3=="O" ) {
         G2=1;
         A=1;
        }
      }
    }
    if ( c1=="O" ) {
      if ( c2=="O" ) {
        if (c3=="O" ) {
         G2=1;
         A=1;
        }
      }
    }
    if ( a1=="O" ) {
      if ( b1=="O" ) {
        if (c1=="O" ) {
         G2=1;
         A=1;
        }
      }
    }
    if ( a2=="O" ) {
      if ( b2=="O" ) {
        if (c2=="O" ) {
         G2=1;
         A=1;
        }
      }
    }
    if ( a3=="O" ) {
      if ( b3=="O" ) {
        if (c3=="O" ) {
         G2=1;
         A=1;
        }
      }
    }
    if ( a1=="O" ) {
      if ( b2=="O" ) {
        if (c3=="O" ) {
         G2=1;
         A=1;
        }
      }
    }
    if ( a3=="O" ) {
      if ( b2=="O" ) {
        if (c1=="O" ) {
         G2=1;
         A=1;
        }
      }
    }

    //Kontrolle Unentschieden
     if ( a1!=" " ) {
       if ( a2!=" " ) {
         if ( a3!=" " ) {
           if ( b1!=" " ) {
             if ( b2!=" " ) {
               if ( b3!=" " ) {
                 if ( c1!=" " ) {
                   if ( c2!=" " ) {
                     if ( c3!=" " ) {
                      A=1;
                     }
                   }
                 }
               }
             }
           }
         }
       }
     }
    } while ( A!=1 );
   
     // Gewinnbedingung - Unentschieden
     if ( G1==1 ) {
     System.out.println("*************************************");
     System.out.println("***** K R E U Z   G E W I N N T *****");
     System.out.println("*************************************");
     System.out.println("*  -...-  *");
     System.out.println("*************************************");
     }
     if ( G2==1 ) {
     System.out.println("*************************************");
     System.out.println("****  K R E I S   G E W I N N T  ****");
     System.out.println("*************************************");
     System.out.println("*  -...-  *");
     System.out.println("*************************************");
     }
     if ( G1!=1 ) {
      if ( G2!=1 ) {
       System.out.println("***********************************");
       System.out.println("*****U N E N T S C H I E D E N*****");
       System.out.println("***********************************");
       System.out.println("* -by ...- *");
       System.out.println("***********************************");
       }
     }
    }
}
 

Gossi

Bekanntes Mitglied
Dein Code ist sehr umständlich geschrieben und dadurch schwer zu lesen, schau dir mal Listen oder Arrays an.

Somit könntest du z.B. sowas machen:
Java:
String[] felder = new String[9];
boolean found = false;

//Bei der Überprüfung
S1=IOTools.readInteger("Kreuz, waehle ein Feld: ");
while(!found) {
     if(felder[S1-1].equals("")) {
          felder[S1-1] = "X";
          found = true;
     } else {
          S1=IOTools.readInteger("Falsches Feld, wählen sie erneut: ");
     }
}
found = false;

Und das gleiche nochmal für S2, macht den Code sauberer
 

xehpuk

Top Contributor
String hat da nichts zu suchen. Nutz stattdessen enum. Dann dürfen die Vergleiche auch mit
Code:
==
und
Code:
!=
erfolgen.
Ansonsten, wie bereits erwähnt, Arrays oder Listen nutzen, wobei sich hier Ersteres wohl besser eignet. Mehrfach vorhandener Code ist nicht nur unschön, sondern auch fehleranfällig und sehr schlecht erweiterbar.
 

Neue Themen


Oben