<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[random int &amp;amp; char sollen nicht doppelt vorkommen]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe folgendes Problem in meinem C++ Code:<br />
Ich entwerfe das Spiel Schiffeversenken mit den mir bekannten Möglichkeiten.<br />
In dieser Funktion, versteckt die &quot;KI&quot; ihre Schiffe. Das funktioniert auch, doch sobald eine Koordinate (z.B. 3A) doppelt vorkommt, werden am Ende anzahlSchiffe-1 ausgegeben.</p>
<p>Ich habe schon versucht, mit einer do-while Schleife und der while-Bedingung &quot;bool&quot; eine Lösung zu finden. Leider wurde der Code compiliert, doch ohne die Ausgabe des Koordinatensystems am Ende.<br />
Auch mit einer if-Bedingung bin ich nicht weiter gekommen.</p>
<p>ich vermute mal, dass ich der Lösung nahe bin, doch hoffe ich hier einen Hinweis, oder eine Lösungsmöglichkeit zu bekommen.</p>
<pre><code>void spielfeldKI() {
    int X {0};
    int Y {0};

    string buchstabe;

    if(anzahlSchiffe==3) {
        X = 5;
        Y = 5;
        buchstabe = &quot;ABCDE&quot;;
    }
    else if(anzahlSchiffe==5) {
        X = 5;
        Y = 6;
        buchstabe = &quot;ABCDEF&quot;;
    }
    else {
        X = 5;
        Y = 8;
        buchstabe = &quot;ABCDEFGH&quot;;
    }

    char Spielfeld[X][Y];
    int x, y;
    for(x = 0; x &lt; X; x++) {
        for(y = 0; y &lt;  Y; y++) {
            Spielfeld[x][y] = '.';
        }
    }

    srand(time(0));
    int koordinateX;
    char koordinateY;
    for(int i = 0; i &lt;= anzahlSchiffe; i++) {
        koordinateX = rand() % X + 1;
        koordinateY = buchstabe[rand()%buchstabe.size()];
        int schiffX = koordinateX;
        int schiffY = koordinateY - 'A';
        Spielfeld[schiffX][schiffY] = 'x';
        }

        cout &lt;&lt; &quot;     1  2  3  4  5\n&quot;;
        for(y = 0; y &lt; Y; y++) {
            cout &lt;&lt; (char)('A'+y) &lt;&lt; &quot;  &quot;;
            for(x = 0; x &lt; X; x++) {
                cout &lt;&lt; &quot;  &quot; &lt;&lt; Spielfeld[x][y];
            }
            cout &lt;&lt; endl;

    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/333153/random-int-amp-char-sollen-nicht-doppelt-vorkommen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 05:34:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/333153.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 15 Jun 2015 19:51:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to random int &amp;amp; char sollen nicht doppelt vorkommen on Mon, 15 Jun 2015 19:51:48 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe folgendes Problem in meinem C++ Code:<br />
Ich entwerfe das Spiel Schiffeversenken mit den mir bekannten Möglichkeiten.<br />
In dieser Funktion, versteckt die &quot;KI&quot; ihre Schiffe. Das funktioniert auch, doch sobald eine Koordinate (z.B. 3A) doppelt vorkommt, werden am Ende anzahlSchiffe-1 ausgegeben.</p>
<p>Ich habe schon versucht, mit einer do-while Schleife und der while-Bedingung &quot;bool&quot; eine Lösung zu finden. Leider wurde der Code compiliert, doch ohne die Ausgabe des Koordinatensystems am Ende.<br />
Auch mit einer if-Bedingung bin ich nicht weiter gekommen.</p>
<p>ich vermute mal, dass ich der Lösung nahe bin, doch hoffe ich hier einen Hinweis, oder eine Lösungsmöglichkeit zu bekommen.</p>
<pre><code>void spielfeldKI() {
    int X {0};
    int Y {0};

    string buchstabe;

    if(anzahlSchiffe==3) {
        X = 5;
        Y = 5;
        buchstabe = &quot;ABCDE&quot;;
    }
    else if(anzahlSchiffe==5) {
        X = 5;
        Y = 6;
        buchstabe = &quot;ABCDEF&quot;;
    }
    else {
        X = 5;
        Y = 8;
        buchstabe = &quot;ABCDEFGH&quot;;
    }

    char Spielfeld[X][Y];
    int x, y;
    for(x = 0; x &lt; X; x++) {
        for(y = 0; y &lt;  Y; y++) {
            Spielfeld[x][y] = '.';
        }
    }

    srand(time(0));
    int koordinateX;
    char koordinateY;
    for(int i = 0; i &lt;= anzahlSchiffe; i++) {
        koordinateX = rand() % X + 1;
        koordinateY = buchstabe[rand()%buchstabe.size()];
        int schiffX = koordinateX;
        int schiffY = koordinateY - 'A';
        Spielfeld[schiffX][schiffY] = 'x';
        }

        cout &lt;&lt; &quot;     1  2  3  4  5\n&quot;;
        for(y = 0; y &lt; Y; y++) {
            cout &lt;&lt; (char)('A'+y) &lt;&lt; &quot;  &quot;;
            for(x = 0; x &lt; X; x++) {
                cout &lt;&lt; &quot;  &quot; &lt;&lt; Spielfeld[x][y];
            }
            cout &lt;&lt; endl;

    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2456781</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2456781</guid><dc:creator><![CDATA[Roflor69]]></dc:creator><pubDate>Mon, 15 Jun 2015 19:51:48 GMT</pubDate></item><item><title><![CDATA[Reply to random int &amp;amp; char sollen nicht doppelt vorkommen on Mon, 15 Jun 2015 21:38:43 GMT]]></title><description><![CDATA[<p>Dann musst du eben entweder prüfen, ob das Feld bereits besetzt ist; oder du musst eine Liste mit den noch freien Feldern führen, aus der du die zu setzenden Koordinaten ziehst. Wenn das nicht funktioniert, wirst du wohl was falsch gemacht haben, aber ohne Code können wir dir wohl kaum sagen, was genau.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2456792</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2456792</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 15 Jun 2015 21:38:43 GMT</pubDate></item></channel></rss>