Programm gibt nur begrenzt Ergebnisse aus



  • Hi!
    Habe ein Programm geschrieben, dass erstmal nur zufällige Sudokus anzeigen lassen soll. Von oben links nach unten rechts zieht sich eine kette von 3 zufällig erstellten würfeln.
    Funktioniert nach einem komplexen Prinzip.Zuerst sollte es nur den b-würfel berechnen, um die rechenweise zu checken.
    es zeigt aber bei dem b-würfel nur b1,b2,b4 und b5 an. Ich hab alles zwei und dreifach überprüft, finde den fehler aber einfach nicht.
    Könnte mir vielleicht einer nen Tipp geben? 😕 😞
    p.s.:Ja, ich könnte auch backtracking verwenden, aber hier ist es eben anders.

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int Random(int n)
    {
    return rand() % n ;
    }
    
    int main(){
    int p,q;
    int feld[9][9];
    
    srand(time(0));
    
    int w1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    
    random_shuffle(w1, w1 + 9, Random);
    
    int a1=w1[0];
    int a2=w1[1];
    int a3=w1[2];
    int a4=w1[3];
    int a5=w1[4];
    int a6=w1[5];
    int a7=w1[6];
    int a8=w1[7];
    int a9=w1[8];
    
    ////////////////////////////////////
    ////////////////////////////////////
    
    int w5[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    
    random_shuffle(w5, w5 + 9,Random);
    
    int e1=w5[0];
    int e2=w5[1];
    int e3=w5[2];
    int e4=w5[3];
    int e5=w5[4];
    int e6=w5[5];
    int e7=w5[6];
    int e8=w5[7];
    int e9=w5[8];
    
    ///////////////////////////////////
    ///////////////////////////////////
    
    int w9[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    
    random_shuffle(w9, w9 + 9,Random);
    
    int i1=w9[0];
    int i2=w9[1];
    int i3=w9[2];
    int i4=w9[3];
    int i5=w9[4];
    int i6=w9[5];
    int i7=w9[6];
    int i8=w9[7];
    int i9=w9[8];
    
    int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0,b7=0,b8=0,b9=0;
    int c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0;
    int d1=0,d2=0,d3=0,d4=0,d5=0,d6=0,d7=0,d8=0,d9=0;
    int f1=0,f2=0,f3=0,f4=0,f5=0,f6=0,f7=0,f8=0,f9=0;
    int g1=0,g2=0,g3=0,g4=0,g5=0,g6=0,g7=0,g8=0,g9=0;
    int h1=0,h2=0,h3=0,h4=0,h5=0,h6=0,h7=0,h8=0,h9=0;
    
    for(int t=1;t<9;t++)
    {
    
    if(a1!=t&&a2!=t&&a3!=t)
    {
    if(e1!=t&&e4!=t&&e7!=t)
    {
    if(b1==0)b1=t;
    }
    else if(e2!=t&&e5!=t&&e8!=t)
    {
    if(b2==0)b2=t;
    }
    else if(e3!=t&&e6!=t&&e9!=t)
    {
    if(b3==0)b3=t;
    }}
    
    else if(a4!=t&&a5!=t&&a6!=t)
    {
    if(e1!=t&&e4!=t&&e7!=t)
    {
    if(b4==0)b4=t;
    }
    else if(e2!=t&&e5!=t&&e8!=t)
    {
    if(b5==0)b5=t;
    }
    else if(e3!=t&&e6!=t&&e9!=t)
    {
    if(b6==0)b6=t;
    }}
    
    else if(a7!=t&&a8!=t&&a9!=t)
    {
    if(e1!=t&&e4!=t&&e7!=t)
    {
    if(b7==0)b7=t;
    }
    else if(e2!=t&&e5!=t&&e8!=t)
    {
    if(b8==0)b8=t;
    }
    else if(e3!=t&&e6!=t&&e9!=t)
    {
    if(b9==0)b9=t;
    }}}
    
    cout<<a1<<" "<<a2<<" "<<a3<<"  "<<b1<<" "<<b2<<" "<<b3<<"  "<<c1<<" "<<c2<<" "<<c3<<"\n";
    
    cout<<a4<<" "<<a5<<" "<<a6<<"  "<<b4<<" "<<b5<<" "<<b6<<"  "<<c4<<" "<<c5<<" "<<c6<<"\n";
    
    cout<<a7<<" "<<a8<<" "<<a9<<"  "<<b7<<" "<<b8<<" "<<b9<<"  "<<c7<<" "<<c8<<" "<<c9<<"\n\n";
    
    cout<<d1<<" "<<d2<<" "<<d3<<"  "<<e1<<" "<<e2<<" "<<e3<<"  "<<f1<<" "<<f2<<" "<<f3<<"\n";
    
    cout<<d4<<" "<<d5<<" "<<d6<<"  "<<e4<<" "<<e5<<" "<<e6<<"  "<<f4<<" "<<f5<<" "<<f6<<"\n";
    
    cout<<d7<<" "<<d8<<" "<<d9<<"  "<<e7<<" "<<e8<<" "<<e9<<"  "<<f7<<" "<<f8<<" "<<f9<<"\n\n";
    
    cout<<g1<<" "<<g2<<" "<<g3<<"  "<<h1<<" "<<h2<<" "<<h3<<"  "<<i1<<" "<<i2<<" "<<i3<<"\n";
    
    cout<<g4<<" "<<g5<<" "<<g6<<"  "<<h4<<" "<<h5<<" "<<h6<<"  "<<i4<<" "<<i5<<" "<<i6<<"\n";
    
    cout<<g7<<" "<<g8<<" "<<g9<<"  "<<h7<<" "<<h8<<" "<<h9<<"  "<<i7<<" "<<i8<<" "<<i9<<"\n\n";
    
    return 0;
    }
    


  • Was soll denn das mit diesen tausenden Variablen a1,a2,a3 etc? Du hast doch schon Arrays gefunden und kannst dir die Arbeit vermutlich erleichtern, indem du anstelle dieses Variablenhaufens ein (evt. mehrdimensionales) Array verwendest.


  • Mod

    Und wenn du schon dabei bist: Ein paar Leerzeichen tun einem Quelltext auch ganz gut.
    Beispiel

    if(a1!=t&&a2!=t&&a3!=t)
    

    Das liest sich noch nicht einmal jemand durch, weil es einfach nur aussieht wie hingekotzt.

    if(a1 != t && a2 != t && a3 != t)
    

    Schon viel besser. Vielleicht sollte man noch die logische Struktur deutlich machen. So:

    if((a1 != t) && (a2 != t) && (a3 != t))
    

    Oder auch so:

    if (a1 != t 
     && a2 != t 
     && a3 != t)
    

    Das kann ein einigermaßen erfahrener Programmierer sogar querlesen, so dass eine Chance besteht, dass sich jemand einen Quelltext der nur aus if-Abfragen besteht tatsächlich durchliest. Und dabei Fehler möglicherweise sogar auf den ersten Blick sieht.

    P.S.: Bedauerlicherweise hast du keinen einzigen der Tipps aus den anderen Threads umgesetzt, wie du besseren Code schreiben könntest. Nicht einmal Arrays hast du dir angeguckt - du benutzt sie, ohne sie zu verstehen. Viele Leute haben keine Lust zu helfen, wenn sie wissen, dass sie ignoriert werden.

    Außerdem kann man sich das formatieren natürlich auch automatisieren lassen, wenn man extrem schreibfaul ist:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <algorithm>
    
    using namespace std;
    
    int
    Random (int n)
    {
      return rand () % n;
    }
    
    int
    main ()
    {
      int p, q;
      int feld[9][9];
    
      srand (time (0));
    
      int w1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
      random_shuffle (w1, w1 + 9, Random);
    
      int a1 = w1[0];
      int a2 = w1[1];
      int a3 = w1[2];
      int a4 = w1[3];
      int a5 = w1[4];
      int a6 = w1[5];
      int a7 = w1[6];
      int a8 = w1[7];
      int a9 = w1[8];
    
    ////////////////////////////////////
    ////////////////////////////////////
    
      int w5[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
      random_shuffle (w5, w5 + 9, Random);
    
      int e1 = w5[0];
      int e2 = w5[1];
      int e3 = w5[2];
      int e4 = w5[3];
      int e5 = w5[4];
      int e6 = w5[5];
      int e7 = w5[6];
      int e8 = w5[7];
      int e9 = w5[8];
    
    ///////////////////////////////////
    ///////////////////////////////////
    
      int w9[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
      random_shuffle (w9, w9 + 9, Random);
    
      int i1 = w9[0];
      int i2 = w9[1];
      int i3 = w9[2];
      int i4 = w9[3];
      int i5 = w9[4];
      int i6 = w9[5];
      int i7 = w9[6];
      int i8 = w9[7];
      int i9 = w9[8];
    
      int b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0, b7 = 0, b8 = 0, b9 = 0;
      int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0, c9 = 0;
      int d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0, d7 = 0, d8 = 0, d9 = 0;
      int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, f6 = 0, f7 = 0, f8 = 0, f9 = 0;
      int g1 = 0, g2 = 0, g3 = 0, g4 = 0, g5 = 0, g6 = 0, g7 = 0, g8 = 0, g9 = 0;
      int h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0, h7 = 0, h8 = 0, h9 = 0;
    
      for (int t = 1; t < 9; t++)
        {
    
          if (a1 != t && a2 != t && a3 != t)
            {
              if (e1 != t && e4 != t && e7 != t)
                {
                  if (b1 == 0)
                    b1 = t;
                }
              else if (e2 != t && e5 != t && e8 != t)
                {
                  if (b2 == 0)
                    b2 = t;
                }
              else if (e3 != t && e6 != t && e9 != t)
                {
                  if (b3 == 0)
                    b3 = t;
                }
            }
    
          else if (a4 != t && a5 != t && a6 != t)
            {
              if (e1 != t && e4 != t && e7 != t)
                {
                  if (b4 == 0)
                    b4 = t;
                }
              else if (e2 != t && e5 != t && e8 != t)
                {
                  if (b5 == 0)
                    b5 = t;
                }
              else if (e3 != t && e6 != t && e9 != t)
                {
                  if (b6 == 0)
                    b6 = t;
                }
            }
    
          else if (a7 != t && a8 != t && a9 != t)
            {
              if (e1 != t && e4 != t && e7 != t)
                {
                  if (b7 == 0)
                    b7 = t;
                }
              else if (e2 != t && e5 != t && e8 != t)
                {
                  if (b8 == 0)
                    b8 = t;
                }
              else if (e3 != t && e6 != t && e9 != t)
                {
                  if (b9 == 0)
                    b9 = t;
                }
            }
        }
    
      cout << a1 << " " << a2 << " " << a3 << "  " << b1 << " " << b2 << " " << b3
        << "  " << c1 << " " << c2 << " " << c3 << "\n";
    
      cout << a4 << " " << a5 << " " << a6 << "  " << b4 << " " << b5 << " " << b6
        << "  " << c4 << " " << c5 << " " << c6 << "\n";
    
      cout << a7 << " " << a8 << " " << a9 << "  " << b7 << " " << b8 << " " << b9
        << "  " << c7 << " " << c8 << " " << c9 << "\n\n";
    
      cout << d1 << " " << d2 << " " << d3 << "  " << e1 << " " << e2 << " " << e3
        << "  " << f1 << " " << f2 << " " << f3 << "\n";
    
      cout << d4 << " " << d5 << " " << d6 << "  " << e4 << " " << e5 << " " << e6
        << "  " << f4 << " " << f5 << " " << f6 << "\n";
    
      cout << d7 << " " << d8 << " " << d9 << "  " << e7 << " " << e8 << " " << e9
        << "  " << f7 << " " << f8 << " " << f9 << "\n\n";
    
      cout << g1 << " " << g2 << " " << g3 << "  " << h1 << " " << h2 << " " << h3
        << "  " << i1 << " " << i2 << " " << i3 << "\n";
    
      cout << g4 << " " << g5 << " " << g6 << "  " << h4 << " " << h5 << " " << h6
        << "  " << i4 << " " << i5 << " " << i6 << "\n";
    
      cout << g7 << " " << g8 << " " << g9 << "  " << h7 << " " << h8 << " " << h9
        << "  " << i7 << " " << i8 << " " << i9 << "\n\n";
    
      return 0;
    }
    

    Bamm! Mit einem Schlag von unlesbar zu strukturiert.



  • @sjepp: doch , natürlich hab ich mir arrays angesehen.
    das hier anzuwenden geht nur mit einem md-array wie eben

    int feld[9][9];
    

    ich hab versucht es da reinzubringen, hab aber nicht sofort verstanden wie.
    solange hab ich es anders versucht.

    und ja, sorry, ich habs vergessen vor dem posten die konstruktionen anschaulicher zu machen, werde es gleich editieren.



  • ahh, hast du ja schon gemacht.


Anmelden zum Antworten