8 Damen Problem



  • Aufgabenstellung:
    Beim 8-Damen-Problem müssen auf einem Schachbrett 8 Damen so positioniert werden, dass sie sich gegenseitig nicht bedrohen.
    Schreiben Sie ein Programm zur Unterstützung bei der Suche nach einer Lösung. Es soll möglich sein, durch Eingabe der Feldkoordinaten in der üblichen Schachnotation Damen auf dem Feld zu positionieren. Falls die gewählte Position bedroht ist, soll das Programm eine Meldung ausgeben. Andernfalls wird eine Dame an diese Position gesetzt. Die Eingabe kann so lange wiederholt werden, bis 8 Damen positioniert wurden.

    Ich darf zur Lösung dieser Aufgabe keine Funktionen verwenden.
    Mein bisheriger Lösungsansatz sieht bisher so aus:

    #include<iostream>
    using namespace std;
    
    int main(){
        bool schachbrett[8][8];
        char feldkoordinaten_b;
        int feldkoordinaten_z;
    
        int position0, position00;
        int position1, position11;
    
        bool kollision = true;
        int damen = 0;
    
        for(int i = 0; i < 8; ++i){
            for(int j = 0; j < 8; ++j){
                schachbrett[i][j] = true;
            }
        }
    
        while(damen < 8){
    
            while(kollision == true){
    
                cout << "Eingabe Feldkoordinaten: ";
                cin >> feldkoordinaten_b >> feldkoordinaten_z;
    
                //Aus Feldkoordinaten Indexposition im Array ermitteln
                position0 = feldkoordinaten_b - 64  -1;
                position1 = feldkoordinaten_z  -1;
    
                //Kollision prüfen
                for(int k = 0; k < 8; ++k){
                    for(int l = 0; l < 8; ++l){
                        if(schachbrett[k][l] == false && k == position0 && l == position1){
                            kollision = true;
                        }else {
                            kollision = false;
                        }
                    }
                }
            }
    
            cout << "Kollision" << kollision << endl;
    
            position00 = position0;
            position11 = position1;
    
            //Spalte
            for(int j = 0; j < 8; ++j){
                schachbrett[j][position11] = false;
            }
    
            //Zeile
            for(int i = 0; i < 8; ++i){
                schachbrett[position00][i] = false;
            }
    
            //Diagnale nach links oben
            position00 = position0;
            position11 = position1;
    
            for(int i = position00-1; i >= 0; --i){
                for(int j = position11-1; j >= 0; --j){
                    schachbrett[position00][position11] = false;
                    position00--;
                    position11--;
                }
            }
    
            //Diagonale nach rechts unten
            position00 = position0;
            position11 = position1;
    
            for(int i = position00+1; i <= 8; ++i){
                for(int j = position11+1; j <= 8; ++j){
                    schachbrett[position00][position11] = false;
                    position00++;
                    position11++;
                }
            }
    
            //Diagonale nach rechts oben
            position00 = position0;
            position11 = position1;
    
            cout << position00 << position11 << endl;
    
            for(int i = position00-1; i >= 0; --i){
                for(int j = position11+1; j >= 0; --j){
                    schachbrett[position00][position11] = false;
                    position00--;
                    position11++;
                }
            }
    
            //Diagonale nach links unten
            position00 = position0;
            position11 = position1;
    
            cout << position00 << position11 << endl;
    
            for(int i = position00+1; i <= 8; ++i){
                for(int j = position11; j >=0; --j){
                    schachbrett[position00][position11] = false;
                    position00++;
                    position11--;
                }
            }
    
            for(int i = 0; i < 8; ++i){
                for(int j = 0; j < 8; ++j){
                    cout << schachbrett[i][j];
                }
                cout << endl;
            }
    
            damen++;
    
        }
    
        return 0;
    
    }
    

    Mein Problem ist, dass meine Kollisionsprüfung noch nicht nach meinen Vorstellungen funktioniert.
    Vielleicht könnte mir jemand weiterhelfen.

    Freue mich über jede Unterstützung

    Vielen lieben Dank



  • kollission muss für einen neuen versuch zurückgestellt werden.

    while(damen < 8){
            bool kollision = true;
            while(kollision == true){
    

    oder besser dort eine do-schleife nehmen.

    Und darfst es in der prüfung nicht wieder auf false setzen.

    //Kollision prüfen
                kollision = false;
                for(int k = 0; k < 8; ++k){
                    for(int l = 0; l < 8; ++l){
                        if(schachbrett[k][l] == false && k == position0 && l == position1){
                            kollision = true;
                        }
                    }
                }
    

    oder besser einfach nur

    //Kollision prüfen
                kollision = false;
                if(schachbrett[k][l] == false){
                    kollision = true;
                }
    


  • Danke für den Tipp mit der do while Schleife.
    Natürlich muss nach jedem Durchlauf Kollision wieder false sein.
    Das habe ich jetzt in meinem Programm beachtet.

    Auch deine verkürzte Form der Kollision war sehr hilfreich.

    Vielen lieben Dank



  • estrella schrieb:

    char feldkoordinaten_b;
        int feldkoordinaten_z;
                //Aus Feldkoordinaten Indexposition im Array ermitteln
                position0 = feldkoordinaten_b - 64  -1;
                position1 = feldkoordinaten_z  -1;
    

    würde ich noch machen zu

    char feldkoordinaten_b;
        char feldkoordinaten_z;
                position0 = feldkoordinaten_b - 'A';
                position1 = feldkoordinaten_z - '0';
    


  • Danke für den weiteren Tipp.
    Ich habe diesen im meinem Programm beherzigt.
    Leider funktioniert es noch nicht wirklich.

    Mein Code sieht jetzt so aus:

    #include<iostream>
    using namespace std;
    
    int main(){
        bool schachbrett[8][8];
        char feldkoordinaten_b;
        char feldkoordinaten_z;
    
        int position0, position00;
        int position1, position11;
    
        bool kollision = true;
        int damen = 0;
    
        for(int i = 0; i < 8; ++i){
            for(int j = 0; j < 8; ++j){
                schachbrett[i][j] = true;
            }
        }
    
        while(damen < 8){
    
            do{
                cout << "Eingabe Feldkoordinaten: ";
                cin >> feldkoordinaten_b >> feldkoordinaten_z;
    
                //Aus Feldkoordinaten Indexposition im Array ermitteln
                position0 = feldkoordinaten_b - 'A';
                position1 = feldkoordinaten_z - '0';
                // -1 um richtigen Index im Array zu erhalten
                position1 = position1-1;
    
                //position0 = feldkoordinaten_b - 64  -1;
                //position1 = feldkoordinaten_z  -1;
    
                //Kollision prüfen
                for(int k = 0; k < 8; ++k){
                    for(int l = 0; l < 8; ++l){
    
                        kollision = false;
                        if(schachbrett[k][l] == false){
                            kollision = true;
                        }
    
                    }
                }
    
            }while(kollision == true);
    
            position00 = position0;
            position11 = position1;
    
            //Spalte
            for(int j = 0; j < 8; ++j){
                schachbrett[j][position11] = false;
            }
    
            //Zeile
            for(int i = 0; i < 8; ++i){
                schachbrett[position00][i] = false;
            }
    
            //Diagnale nach links oben
            position00 = position0;
            position11 = position1;
    
            for(int i = position00-1; i >= 0; --i){
                for(int j = position11-1; j >= 0; --j){
                    schachbrett[position00][position11] = false;
                    position00--;
                    position11--;
                }
            }
    
            //Diagonale nach rechts unten
            position00 = position0;
            position11 = position1;
    
            for(int i = position00+1; i <= 8; ++i){
                for(int j = position11+1; j <= 8; ++j){
                    schachbrett[position00][position11] = false;
                    position00++;
                    position11++;
                }
            }
    
            //Diagonale nach rechts oben
            position00 = position0;
            position11 = position1;
    
            cout << position00 << position11 << endl;
    
            for(int i = position00-1; i >= 0; --i){
                for(int j = position11+1; j >= 0; --j){
                    schachbrett[position00][position11] = false;
                    position00--;
                    position11++;
                }
            }
    
            //Diagonale nach links unten
            position00 = position0;
            position11 = position1;
    
            cout << position00 << position11 << endl;
    
            for(int i = position00+1; i <= 8; ++i){
                for(int j = position11; j >=0; --j){
                    schachbrett[position00][position11] = false;
                    position00++;
                    position11--;
                }
            }
    
            for(int i = 0; i < 8; ++i){
                for(int j = 0; j < 8; ++j){
                    cout << schachbrett[i][j];
                }
                cout << endl;
            }
    
            damen++;
    
            }
    
        return 0;
    
    }
    

    Es gibt mehrere Probleme mit den ich Schwierigkeiten habe.
    Zum einen wenn ich als erstes C4 eingebe, erhalte ich hier:

    //Diagonale nach rechts oben
            position00 = position0;
            position11 = position1;
    
            cout << position00 << position11 << endl;
    
            for(int i = position00-1; i >= 0; --i){
                for(int j = position11+1; j >= 0; --j){
                    schachbrett[position00][position11] = false;
                    position00--;
                    position11++;
                }
            }
    

    in dieser Zeile:
    schachbrett[position00][position11] = false;
    den Fehler
    Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff67bff790)

    Teste ich andere Werte wie zum Beispiel A1
    00000000
    00111111
    01011111
    01101111
    01110111
    01111011
    01111101
    01111110
    erster Durchlauf vollkommen in Ordnung.
    Zweiter Durchlauf mit B3 geht nicht bzw. mein Programm denkt es würde zu einer Kollision führen.

    Ich habe auch noch weitere Fälle gefunden die nicht funktionieren.

    Vielleicht sieht jemand den Fehler in meinem Programm?
    Danke



  • Mir scheint, position00 und position11 können aus dem gültigen Bereich 0-7 rauslaufen.

    ich dächte daran, gleich auf schachbrett[i][j] zu arbeiten und position00 und position11 ganz wegzulassen.

    edit: halt! die schleife soll gar nicht verschachtelt sein.

    kannst position00 und position11 nehmen und nur eine i-schleife drum. und abbrechen (mit break), wenn position00 oder position11 rauslaufen.



  • Ich habe mittlerweile meinen Fehler gefunden.

    for(int i = position00-1; i >= 0; --i){
        for(int j = position11+1; j >= 0; --j){
              schachbrett[position00][position11] = false;
              position00--;
              position11++;
        }
    }
    

    meine innere for Schleife läuft aus dem Bereich.
    Sollte so aussehen:

    for(int i = position00-1; i >= 0; --i){
        for(int j = position11+1; j < 8; ++j){
              schachbrett[position00][position11] = false;
              position00--;
              position11++;
        }
    }
    

Anmelden zum Antworten