<?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[4 gewinnt problem]]></title><description><![CDATA[<p>Guten abend,</p>
<p>zur zeit schreibe ich an einem 4 gewinnt spiel. Ich habe dafür die Klasse Spielfeld entwickelt:</p>
<p>s4 gewinnt.hpp</p>
<pre><code class="language-cpp">#include &lt;ctime&gt;
#include &lt;cstdlib&gt;
#include &lt;cassert&gt;
#include &lt;iostream&gt;

class spielfeld {
    private:
        class proxy {
            public:
                short int operator[](short int x) ;
                short int inhalt[7];
        };
        short int hoehe[7];
        proxy feld[6];
        bool aktu_spieler;
    public:
    //Konstruktur
        spielfeld();

    //Gewonnen ja oder nein, gibt nicht den spieler, der gewonnen hat zurück
        bool gewinn_ueberpruefung(short int x);

    // die position auf der x achse wo er auftritt
        bool stein_faellt(short int x);

        proxy&amp; operator[](short int y) ;
};
</code></pre>
<p>und dazu die implementierung</p>
<pre><code class="language-cpp">#include &quot;4 gewinnt.hpp&quot;

// das feld wird auf 0 gesetzt

using namespace std;
spielfeld::spielfeld() {
    int i, j;
    for( i=1; i&lt;=6; ++i) {
        for( j=1; j&lt;=7; ++j)
             feld[i].inhalt[j] = 0;
        hoehe[i] = 6;
    }
    hoehe[7] = 6;

}

//
// der überladene Operator []

short int spielfeld::proxy::operator[](const short int y) {
    assert((y&lt;=7) &amp;&amp; (y &gt; 0));
        return inhalt[y];
}

spielfeld::proxy&amp; spielfeld::operator[](const short int x) {
    assert((x&lt;=6) &amp;&amp; (x &gt; 0));
        return feld[x];
}

// Stein fällt

bool spielfeld::stein_faellt(short int x) {
    assert((x&lt;=7) &amp;&amp; (x &gt; 0));
        if (hoehe[x] &gt; 0) { // zählt die felder die noch leer sind
            cout &lt;&lt; &quot;Speicher Spieler:&quot; &lt;&lt;&amp;aktu_spieler &lt;&lt; &quot; Speicher feld:&quot; &lt;&lt; &amp;feld[5].inhalt[7] &lt;&lt; endl; // die ausgabe der addressen
            feld[hoehe[x&rsqb;&rsqb;.inhalt[x] = aktu_spieler + 1;
            --hoehe[x];
            aktu_spieler = 1 - aktu_spieler; // wird immer gewechselt;
            return 0;
        } else
            return 1;

}
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;Header\4 gewinnt.hpp&quot;

using namespace std;

int main()
{
    int i, j, ende;
    spielfeld test;
    cout &lt;&lt; endl;
    // nur für testzzwecke
    do {
        for( i=1; i&lt;=6; ++i) {
            for( j=1; j&lt;=7; ++j)
                cout &lt;&lt; test[i][j];
            cout &lt;&lt; endl;
        }
        cin &gt;&gt; ende;
        cout &lt;&lt; endl;
        test.stein_faellt(ende);
    }while (ende != 0);

}
</code></pre>
<p>sooo. hier wollte ich nun mit der Gewinn überrüfung weitermachen. Allerdings fiel mir ein fehler auf.<br />
Alls ich ein paar steine fallen ließ, hat das feld [7][5] immer seinen Inhalt gewechselt. und zwar immer zum inhalt das aktu_spieler. Ich habe mir mal die Speicheraddresen der beiden angeschaut und sah, das sie identisch waren. Ich weiß aber nicht warum.</p>
<p>Kann mir bitte einer erklären warum die speicheraddressen der beiden identisch sind und eine lösung nennen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/239473/4-gewinnt-problem</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 10:03:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/239473.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 23 Apr 2009 20:43:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 4 gewinnt problem on Thu, 23 Apr 2009 20:43:36 GMT]]></title><description><![CDATA[<p>Guten abend,</p>
<p>zur zeit schreibe ich an einem 4 gewinnt spiel. Ich habe dafür die Klasse Spielfeld entwickelt:</p>
<p>s4 gewinnt.hpp</p>
<pre><code class="language-cpp">#include &lt;ctime&gt;
#include &lt;cstdlib&gt;
#include &lt;cassert&gt;
#include &lt;iostream&gt;

class spielfeld {
    private:
        class proxy {
            public:
                short int operator[](short int x) ;
                short int inhalt[7];
        };
        short int hoehe[7];
        proxy feld[6];
        bool aktu_spieler;
    public:
    //Konstruktur
        spielfeld();

    //Gewonnen ja oder nein, gibt nicht den spieler, der gewonnen hat zurück
        bool gewinn_ueberpruefung(short int x);

    // die position auf der x achse wo er auftritt
        bool stein_faellt(short int x);

        proxy&amp; operator[](short int y) ;
};
</code></pre>
<p>und dazu die implementierung</p>
<pre><code class="language-cpp">#include &quot;4 gewinnt.hpp&quot;

// das feld wird auf 0 gesetzt

using namespace std;
spielfeld::spielfeld() {
    int i, j;
    for( i=1; i&lt;=6; ++i) {
        for( j=1; j&lt;=7; ++j)
             feld[i].inhalt[j] = 0;
        hoehe[i] = 6;
    }
    hoehe[7] = 6;

}

//
// der überladene Operator []

short int spielfeld::proxy::operator[](const short int y) {
    assert((y&lt;=7) &amp;&amp; (y &gt; 0));
        return inhalt[y];
}

spielfeld::proxy&amp; spielfeld::operator[](const short int x) {
    assert((x&lt;=6) &amp;&amp; (x &gt; 0));
        return feld[x];
}

// Stein fällt

bool spielfeld::stein_faellt(short int x) {
    assert((x&lt;=7) &amp;&amp; (x &gt; 0));
        if (hoehe[x] &gt; 0) { // zählt die felder die noch leer sind
            cout &lt;&lt; &quot;Speicher Spieler:&quot; &lt;&lt;&amp;aktu_spieler &lt;&lt; &quot; Speicher feld:&quot; &lt;&lt; &amp;feld[5].inhalt[7] &lt;&lt; endl; // die ausgabe der addressen
            feld[hoehe[x&rsqb;&rsqb;.inhalt[x] = aktu_spieler + 1;
            --hoehe[x];
            aktu_spieler = 1 - aktu_spieler; // wird immer gewechselt;
            return 0;
        } else
            return 1;

}
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;Header\4 gewinnt.hpp&quot;

using namespace std;

int main()
{
    int i, j, ende;
    spielfeld test;
    cout &lt;&lt; endl;
    // nur für testzzwecke
    do {
        for( i=1; i&lt;=6; ++i) {
            for( j=1; j&lt;=7; ++j)
                cout &lt;&lt; test[i][j];
            cout &lt;&lt; endl;
        }
        cin &gt;&gt; ende;
        cout &lt;&lt; endl;
        test.stein_faellt(ende);
    }while (ende != 0);

}
</code></pre>
<p>sooo. hier wollte ich nun mit der Gewinn überrüfung weitermachen. Allerdings fiel mir ein fehler auf.<br />
Alls ich ein paar steine fallen ließ, hat das feld [7][5] immer seinen Inhalt gewechselt. und zwar immer zum inhalt das aktu_spieler. Ich habe mir mal die Speicheraddresen der beiden angeschaut und sah, das sie identisch waren. Ich weiß aber nicht warum.</p>
<p>Kann mir bitte einer erklären warum die speicheraddressen der beiden identisch sind und eine lösung nennen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1700451</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1700451</guid><dc:creator><![CDATA[Fettpet]]></dc:creator><pubDate>Thu, 23 Apr 2009 20:43:36 GMT</pubDate></item><item><title><![CDATA[Reply to 4 gewinnt problem on Thu, 23 Apr 2009 20:47:43 GMT]]></title><description><![CDATA[<p>Du hast <code>feld</code> nur mit einer Grösse von 6 deklariert, greifst aber auf den Index 7 zu, also zwei Indizes jenseits des Arrays. Gültige Indizes wären 0 bis 5.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1700456</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1700456</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 23 Apr 2009 20:47:43 GMT</pubDate></item><item><title><![CDATA[Reply to 4 gewinnt problem on Sun, 26 Apr 2009 09:50:58 GMT]]></title><description><![CDATA[<p>Dank dir, jetzt Funktionierts^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1701382</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1701382</guid><dc:creator><![CDATA[Fettpet]]></dc:creator><pubDate>Sun, 26 Apr 2009 09:50:58 GMT</pubDate></item></channel></rss>