<?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[TicTacToe -- Hilfe?]]></title><description><![CDATA[<p>Ich möchte anfangen zu programmieren, da wir in meiner Schule auch programmieren lernen. Allerdings scheitere ich jetzt schon am ersten Projekt &quot;TicTacToe&quot; schon heftig. Hier mal der Code.</p>
<pre><code class="language-cpp">/* 
 * File:   main.cpp
 * Created on 24. Jänner 2011, 23:06
 */

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;curses.h&gt;

using namespace std;

const int maxfield = 9; 													//Constante für Feld
char cfield[maxfield] = { '1','2', '3','4','5','6','7','8','9'}; 		   //Feld
char cselectedfield;													   //welches Feld gewählt wurde
int nplayerturn = 1; 													  //Wer ist dran?
bool bgameover = false;													 //spielvorbei
int nwinner = 0; 														//wer gewinnt (1=player1, 2=player2, 3=unentschieden)

void drawfield (void); 													//zeichnet ein feld
void drawspace (void);												 	//rückt das feld 20 mal ein
void checkforwinner (void); 											//prüft wer der gewinner ist
void saywinner(void); 													//ausgabegewinner

int main(int argc, char** argv) 
{

    char cselector = 'X';

        do {

            if (nplayerturn == 1)
            {
                cselector = 'X';
            }
            else
            {
                cselector = 'O';
            }

			clear();

            drawfield();

            std::cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; &quot; geben Sie bitte eine Feldnummer ein: &quot; &lt;&lt; std::endl;
            cselectedfield = cin.get();

            cfield[cselectedfield--] = cselector;

            checkforwinner();

            saywinner();

            nplayerturn++;

            if (nplayerturn &gt; 2)
            {
            nplayerturn = 1;
            }

        } while ( !bgameover);
}

void drawspace (void)
{
    for (int i = 0; i &lt;= 20; i++)
    {
        cout &lt;&lt; &quot; &quot;;
    }
}

void drawfield ( void )
{
drawspace();
cout &lt;&lt; cfield[0]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[1]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[2] &lt;&lt; endl;
drawspace();
cout &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; endl;
drawspace();
cout &lt;&lt; cfield[3]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[4]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[5] &lt;&lt; endl;
drawspace();
cout &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; endl;
drawspace();
cout &lt;&lt; cfield[6]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[7]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[8] &lt;&lt; endl;

}

void checkforwinner (void)
{
	//oben
	if ( cfield[0] ==  cfield[1] &amp;&amp; cfield[1] == cfield[2] &amp;&amp; cfield[2] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//mitte
	else if (cfield[3] ==  cfield[4] &amp;&amp; cfield[4] == cfield[5] &amp;&amp; cfield[5] == cfield[3])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//unten
	else if (cfield[6] ==  cfield[7] &amp;&amp; cfield[7] == cfield[8] &amp;&amp; cfield[8] == cfield[6])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

	//links
	else if (cfield[0] ==  cfield[3] &amp;&amp; cfield[3] == cfield[6] &amp;&amp; cfield[6] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//mitte
	else if (cfield[1] ==  cfield[4] &amp;&amp; cfield[4] == cfield[7] &amp;&amp; cfield[7] == cfield[1])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//rechts
	else if ( cfield[2] ==  cfield[5] &amp;&amp; cfield[5] == cfield[8] &amp;&amp; cfield[8] == cfield[2])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

	//Schräg von links oben
	else if (cfield[0] ==  cfield[4] &amp;&amp; cfield[4] == cfield[8] &amp;&amp; cfield[8] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//Schräg von rechts oben
	else if ( cfield[2] ==  cfield[4] &amp;&amp; cfield[4] == cfield[6] &amp;&amp; cfield[6] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

}

void saywinner (void)
{
	if (nwinner == 1)
	{
		std::cout &lt;&lt; &quot;Glückwunsch, der Gewinner ist Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; std::endl;
	}
	else	if (nwinner == 2)
	{
		std::cout &lt;&lt; &quot;Glückwunsch, der Gewinner ist Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; std::endl;
	}
	else	if (nwinner == 3)
	{
		std::cout &lt;&lt; &quot;Schade es steht unentschieden&quot; &lt;&lt; std::endl;
	}

}
</code></pre>
<p>Ich benutze Ubuntu 10.10.Ich weiß das Spiel ist noch nicht vollständig (Überprüfung von der Eingabe fehlt usw.), allerdings möchte ich zuerst dass das Grundlegende funktioniert.Und welche Entwicklungsumgebung würdet Ihr mir empfehlen?</p>
<p>Danke &amp; mfg</p>
<p>Kuwiano</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/281499/tictactoe-hilfe</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 13:54:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/281499.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Feb 2011 21:05:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Wed, 02 Feb 2011 21:05:17 GMT]]></title><description><![CDATA[<p>Ich möchte anfangen zu programmieren, da wir in meiner Schule auch programmieren lernen. Allerdings scheitere ich jetzt schon am ersten Projekt &quot;TicTacToe&quot; schon heftig. Hier mal der Code.</p>
<pre><code class="language-cpp">/* 
 * File:   main.cpp
 * Created on 24. Jänner 2011, 23:06
 */

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;curses.h&gt;

using namespace std;

const int maxfield = 9; 													//Constante für Feld
char cfield[maxfield] = { '1','2', '3','4','5','6','7','8','9'}; 		   //Feld
char cselectedfield;													   //welches Feld gewählt wurde
int nplayerturn = 1; 													  //Wer ist dran?
bool bgameover = false;													 //spielvorbei
int nwinner = 0; 														//wer gewinnt (1=player1, 2=player2, 3=unentschieden)

void drawfield (void); 													//zeichnet ein feld
void drawspace (void);												 	//rückt das feld 20 mal ein
void checkforwinner (void); 											//prüft wer der gewinner ist
void saywinner(void); 													//ausgabegewinner

int main(int argc, char** argv) 
{

    char cselector = 'X';

        do {

            if (nplayerturn == 1)
            {
                cselector = 'X';
            }
            else
            {
                cselector = 'O';
            }

			clear();

            drawfield();

            std::cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; &quot; geben Sie bitte eine Feldnummer ein: &quot; &lt;&lt; std::endl;
            cselectedfield = cin.get();

            cfield[cselectedfield--] = cselector;

            checkforwinner();

            saywinner();

            nplayerturn++;

            if (nplayerturn &gt; 2)
            {
            nplayerturn = 1;
            }

        } while ( !bgameover);
}

void drawspace (void)
{
    for (int i = 0; i &lt;= 20; i++)
    {
        cout &lt;&lt; &quot; &quot;;
    }
}

void drawfield ( void )
{
drawspace();
cout &lt;&lt; cfield[0]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[1]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[2] &lt;&lt; endl;
drawspace();
cout &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; endl;
drawspace();
cout &lt;&lt; cfield[3]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[4]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[5] &lt;&lt; endl;
drawspace();
cout &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; endl;
drawspace();
cout &lt;&lt; cfield[6]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[7]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[8] &lt;&lt; endl;

}

void checkforwinner (void)
{
	//oben
	if ( cfield[0] ==  cfield[1] &amp;&amp; cfield[1] == cfield[2] &amp;&amp; cfield[2] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//mitte
	else if (cfield[3] ==  cfield[4] &amp;&amp; cfield[4] == cfield[5] &amp;&amp; cfield[5] == cfield[3])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//unten
	else if (cfield[6] ==  cfield[7] &amp;&amp; cfield[7] == cfield[8] &amp;&amp; cfield[8] == cfield[6])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

	//links
	else if (cfield[0] ==  cfield[3] &amp;&amp; cfield[3] == cfield[6] &amp;&amp; cfield[6] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//mitte
	else if (cfield[1] ==  cfield[4] &amp;&amp; cfield[4] == cfield[7] &amp;&amp; cfield[7] == cfield[1])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//rechts
	else if ( cfield[2] ==  cfield[5] &amp;&amp; cfield[5] == cfield[8] &amp;&amp; cfield[8] == cfield[2])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

	//Schräg von links oben
	else if (cfield[0] ==  cfield[4] &amp;&amp; cfield[4] == cfield[8] &amp;&amp; cfield[8] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//Schräg von rechts oben
	else if ( cfield[2] ==  cfield[4] &amp;&amp; cfield[4] == cfield[6] &amp;&amp; cfield[6] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

}

void saywinner (void)
{
	if (nwinner == 1)
	{
		std::cout &lt;&lt; &quot;Glückwunsch, der Gewinner ist Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; std::endl;
	}
	else	if (nwinner == 2)
	{
		std::cout &lt;&lt; &quot;Glückwunsch, der Gewinner ist Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; std::endl;
	}
	else	if (nwinner == 3)
	{
		std::cout &lt;&lt; &quot;Schade es steht unentschieden&quot; &lt;&lt; std::endl;
	}

}
</code></pre>
<p>Ich benutze Ubuntu 10.10.Ich weiß das Spiel ist noch nicht vollständig (Überprüfung von der Eingabe fehlt usw.), allerdings möchte ich zuerst dass das Grundlegende funktioniert.Und welche Entwicklungsumgebung würdet Ihr mir empfehlen?</p>
<p>Danke &amp; mfg</p>
<p>Kuwiano</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2015826</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2015826</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Wed, 02 Feb 2011 21:05:17 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Wed, 02 Feb 2011 21:15:15 GMT]]></title><description><![CDATA[<p>Wo ist hier was Linux/Unix spezifisches (falsches Forum)? Viel wichtiger ist jedoch, was funktioniert denn nicht? Wir raten und suchen ungerne.</p>
<p>Ich würde g++ verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2015832</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2015832</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Wed, 02 Feb 2011 21:15:15 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 14:45:42 GMT]]></title><description><![CDATA[<p>Sry das hab ich ganz vergessen - Wenn ich das Programm ausführe (mit g++) dan stehen anstatt Zahlen seltsame zeichen dort und wenn ich eine Zahl drücke wird diese nicht mit O und X überschrieben sondern es passiert einfach nichts.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016092</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Thu, 03 Feb 2011 14:45:42 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 15:01:31 GMT]]></title><description><![CDATA[<p>Du hast wenige kleine Fehler:</p>
<p>1. mache aus cselectedfield ein:</p>
<pre><code class="language-cpp">int cselectedfield;
</code></pre>
<p>2. entferne alle unbenötigten includes (also alles außer iostream und den für clear()<br />
3. ändere die Inkrementation auf preincrement oder mach einfach &quot;-1&quot;</p>
<pre><code class="language-cpp">cfield[--cselectedfield] = cselector;
</code></pre>
<p>4. verwende</p>
<pre><code class="language-cpp">std::cin &gt;&gt; cselectedfield;
</code></pre>
<p>anstelle von cin.get()</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016102</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Thu, 03 Feb 2011 15:01:31 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 17:14:45 GMT]]></title><description><![CDATA[<p>Wow, danke für die schnelle Antwort! Es funktioniert jetzt alles, muss nur noch die Fehlerüberprüfung machen. DANKE!</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016171</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016171</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Thu, 03 Feb 2011 17:14:45 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 19:13:52 GMT]]></title><description><![CDATA[<p>Ich habe jetzt eine Fehlerüberprüfung eingebaut und Sie funktioniert nicht. Starte ich das Programm, steht in der Konsole nur Speicherzugriffsfehler hier der veränderte Code:</p>
<pre><code class="language-cpp">int cselectedfield = 1;
</code></pre>
<pre><code class="language-cpp">while(!bError(cselectedfield))
			{
			//Eingabeaufforderung
            std::cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; &quot; geben Sie bitte eine Feldnummer ein: &quot; &lt;&lt; std::endl;
            std::cin &gt;&gt; cselectedfield;
			}
</code></pre>
<pre><code class="language-cpp">bool bError (int cselectedfield)
{
	bool berror = false;

	//Ist die Eingegeben Zahl zw. 1-9?
	if (cselectedfield != '1' ||cselectedfield != '2' ||cselectedfield != '3' ||
	    cselectedfield != '4' ||cselectedfield != '5' ||cselectedfield != '6' ||
	    cselectedfield != '7' ||cselectedfield != '8' ||cselectedfield != '9')
	{
		berror = true;
		std::cout &lt;&lt; &quot;Ungültige Eingabe&quot; &lt;&lt; std::endl;
	}

	return berror;
}
</code></pre>
<p>Entschuldige für falsches Forum <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> könnt ihr ihn ins richtige verschieben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016230</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016230</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Thu, 03 Feb 2011 19:13:52 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 19:46:26 GMT]]></title><description><![CDATA[<p>Warum übergibst du der Fuktion eine globale Variable? Die Variable ist nun ein int und kein Char, warum prüfst du nicht auf Zahlen anstatt auf Zeichen z.B.</p>
<pre><code class="language-cpp">if (cselectedfield &lt; 1 || cselectedfield &gt; 9)
</code></pre>
<p>Sollte es nicht so lauten?</p>
<pre><code class="language-cpp">while(bError(cselectedfield)
</code></pre>
<p>Sollte der Initialwert nicht cselectedfield = 0 sein, ansonsten würde ich die eine do-while Schleife ans Herz legen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016251</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Thu, 03 Feb 2011 19:46:26 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 21:16:30 GMT]]></title><description><![CDATA[<p>Gut, ich habe dass jetzt mit einer Do-While Schleife gemacht und funktioniert nur wie baue ich jetzt eine überprüfung ein die Prüft ob das Feld schon belegt ist? Ich habe es zwar versucht allerdings verhält sich dann mein gesammten Spiel sehr seltsam + es funktioniert nicht. Auch das mit der Globalen Variable hab ich ausgebessert -&gt; Danke für den Hinweis ist mir gar nicht aufgefallen.</p>
<pre><code class="language-cpp">else if (cfield[--iselectedfield] == 'X' || cfield[--iselectedfield] == 'O')
	{
		berror = true;
		cout &lt;&lt; &quot;Ungültige Eingabe&quot; &lt;&lt; endl;
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2016305</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016305</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Thu, 03 Feb 2011 21:16:30 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 21:46:11 GMT]]></title><description><![CDATA[<p>Noch ein Tipp lasse die ungarische Notation weg, ist viel Aufwand für nichts. Zu dem Problem: &quot;Was denkst du macht --?&quot; Es verändert die Variable! Lasse es weg und sieh was passiert.</p>
<p>Wenn cfield[6] selektiert ist ergibt die Abfrage mit dem Dekrement wenig Sinn:</p>
<pre><code class="language-cpp">if(cfield[5] == 'X' || cfield[4] == 'O') //....
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2016322</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016322</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Thu, 03 Feb 2011 21:46:11 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 22:15:57 GMT]]></title><description><![CDATA[<p>Ich sehe hier nichts Unix-spezifisches. curses benutzt Du soweit ich sehen kann ja auch nirgends, bzw. wenn doch, nimmt es keine zentrale Rolel in Deinem Programm ein.</p>
<p>Ich verschiebe Dich mal ins C++-Forum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016337</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016337</guid><dc:creator><![CDATA[nman]]></dc:creator><pubDate>Thu, 03 Feb 2011 22:15:57 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Thu, 03 Feb 2011 22:16:07 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/u4178" rel="nofollow">nman</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/f5" rel="nofollow">Linux/Unix</a> in das Forum <a href="http://www.c-plusplus.net/forum/f15" rel="nofollow">C++ (auch C++0x)</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016338</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016338</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Thu, 03 Feb 2011 22:16:07 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 14:04:38 GMT]]></title><description><![CDATA[<p>Ich habe alles angepasst was Ihr mir gesagt habt und veränder Hier nochmal der gesamte Code:</p>
<pre><code class="language-cpp">/* 
 * File:   main.cpp
 * Created on 24. Jänner 2011, 23:06
 */

#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

using namespace std;

const int maxfield = 9; 													//Constante für Feld
char cfield[maxfield] = { '1','2', '3','4','5','6','7','8','9'}; 		   //Feld
int iselectedfield;													  		//welches Feld gewählt wurde
int nplayerturn = 1; 													  //Wer ist dran?
bool bgameover = false;													 //spielvorbei
int nwinner = 0; 														//wer gewinnt (1=player1, 2=player2, 3=unentschieden)

bool bError ();
void drawfield (void); 													//zeichnet ein feld
void drawspace (void);												 	//rückt das feld 20 mal ein
void checkforwinner (void); 											//prüft wer der gewinner ist
void saywinner(void); 													//ausgabegewinner

int main(int argc, char** argv) 
{

    char cselector = 'X';

        do {

			//X oder O setzen 
            if (nplayerturn == 1)
            {
                cselector = 'X';
            }
            else
            {
                cselector = 'O';
            }

			//vorherigen Konsolenoutput löschen | Spielfeld Zeichnen
			system(&quot;clear&quot;);

            drawfield();

			do{				
			//Eingabeaufforderung
            std::cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; &quot; geben Sie bitte eine Feldnummer ein: &quot; &lt;&lt; std::endl;
            std::cin &gt;&gt; iselectedfield;
			}while(bError());

			//Feld ändern - X oder O
            cfield[--iselectedfield] = cselector;

			// vorherigen Konseloutput löschen | Feld Aktualisieren
			system(&quot;clear&quot;);

            drawfield();

			//Prüfen ob es einen Gewinner gibt &amp; diesen verkünden
            checkforwinner();

            saywinner();

			//Nächster Spieler ist dran
            nplayerturn++;

            if (nplayerturn &gt; 2)
            {
            nplayerturn = 1;
            }

        } while ( !bgameover);
}

bool bError (void)
{
	bool berror = false;
	//int ibackupselectedfield;

	//Ist die Eingegeben Zahl zw. 1-9?
	if (iselectedfield &lt; 1 || iselectedfield &gt; 9) 
	{
		berror = true;
		cout &lt;&lt; &quot;Ungültige Eingabe&quot; &lt;&lt; endl;	
	}

	else if (cfield[--iselectedfield] == 'X' || cfield[--iselectedfield] == 'O')
	{	
		berror = true;
		cout &lt;&lt; &quot;Ungültige Eingabe&quot; &lt;&lt; endl;
	}

	return berror;
}

// Spielfeld Einrücken um 20 spaces
void drawspace (void)
{
    for (int i = 0; i &lt;= 20; i++)
    {
        cout &lt;&lt; &quot; &quot;;
    }
}

//Spielfeld zeichne
void drawfield ( void )
{
drawspace();
cout &lt;&lt; cfield[0]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[1]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[2] &lt;&lt; endl;
drawspace();
cout &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; endl;
drawspace();
cout &lt;&lt; cfield[3]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[4]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[5] &lt;&lt; endl;
drawspace();
cout &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; &quot;+&quot; &lt;&lt; &quot;-&quot; &lt;&lt; endl;
drawspace();
cout &lt;&lt; cfield[6]  &lt;&lt; &quot;|&quot;  &lt;&lt; cfield[7]  &lt;&lt; &quot;|&quot; &lt;&lt;  cfield[8] &lt;&lt; endl;
}

//Prüfen Wer gewinnt 
void checkforwinner (void)
{
	//oben
	if ( cfield[0] ==  cfield[1] &amp;&amp; cfield[1] == cfield[2] &amp;&amp; cfield[2] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//mitte
	else if (cfield[3] ==  cfield[4] &amp;&amp; cfield[4] == cfield[5] &amp;&amp; cfield[5] == cfield[3])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//unten
	else if (cfield[6] ==  cfield[7] &amp;&amp; cfield[7] == cfield[8] &amp;&amp; cfield[8] == cfield[6])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

	//links
	else if (cfield[0] ==  cfield[3] &amp;&amp; cfield[3] == cfield[6] &amp;&amp; cfield[6] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//mitte
	else if (cfield[1] ==  cfield[4] &amp;&amp; cfield[4] == cfield[7] &amp;&amp; cfield[7] == cfield[1])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//rechts
	else if ( cfield[2] ==  cfield[5] &amp;&amp; cfield[5] == cfield[8] &amp;&amp; cfield[8] == cfield[2])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

	//Schräg von links oben
	else if (cfield[0] ==  cfield[4] &amp;&amp; cfield[4] == cfield[8] &amp;&amp; cfield[8] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}
	//Schräg von rechts oben
	else if ( cfield[2] ==  cfield[4] &amp;&amp; cfield[4] == cfield[6] &amp;&amp; cfield[6] == cfield[0])
	{
			nwinner = nplayerturn;
			bgameover = true;
	}

	//unentschieden
	else if ((cfield[0] !='1' &amp;&amp; cfield[1] != '2' &amp;&amp; cfield[2] !='3') &amp;&amp;
			 (cfield[3] !='4' &amp;&amp; cfield[4] != '5' &amp;&amp; cfield[5] !='6') &amp;&amp;
		     (cfield[6] !='7' &amp;&amp; cfield[7] != '8' &amp;&amp; cfield[8] !='9'))
	{
			nwinner = 3;
			bgameover = true;
	}

}

//verkündet den Gewinner
void saywinner (void)
{
	if (nwinner == 1)
	{
		std::cout &lt;&lt; &quot;Glückwunsch, der Gewinner ist Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; std::endl;
	}
	else	if (nwinner == 2)
	{
		std::cout &lt;&lt; &quot;Glückwunsch, der Gewinner ist Spieler &quot; &lt;&lt; nplayerturn &lt;&lt; std::endl;
	}
	else	if (nwinner == 3)
	{
		std::cout &lt;&lt; &quot;Schade es steht unentschieden&quot; &lt;&lt; std::endl;
	}

}
</code></pre>
<p>Das Spiel funktioniert allerdings: wenn ich z.B.: 3 eingebe ändert sich das Feld 2 O.o.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016536</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016536</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Fri, 04 Feb 2011 14:04:38 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 15:11:19 GMT]]></title><description><![CDATA[<p>Ein kleines Update: Ich benutzte jetzt Code::Blocks mit gcc compiler und es funktioniert toll mit einer Ausnahme: system(&quot;clear&quot;); funktioniert nicht mehr :/. Irgendwelche Lösungen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016566</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016566</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Fri, 04 Feb 2011 15:11:19 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 15:38:37 GMT]]></title><description><![CDATA[<p>Kuwiano schrieb:</p>
<blockquote>
<p>Ein kleines Update: Ich benutzte jetzt Code::Blocks mit gcc compiler und es funktioniert toll mit einer Ausnahme: system(&quot;clear&quot;); funktioniert nicht mehr :/. Irgendwelche Lösungen?</p>
</blockquote>
<p>Definiere &quot;funktioniert nicht mehr&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016582</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016582</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 04 Feb 2011 15:38:37 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 16:13:25 GMT]]></title><description><![CDATA[<p>Nach dem ich jetzt mal das Programm gestartet habe (außerhalb Code::Blocks) funktz wider dafür hab ich 1 Problem (hoffentlich das Letzte! Gebe ich einen Char ein (&quot;a&quot;) dann hängt sich das Programm auf und gibt Hundert mal de selbe Line aus (Bitte Geben Sie eine Zahl ein: ). Hört erst auf wenn Ich das Terminal schließe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016613</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Fri, 04 Feb 2011 16:13:25 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 16:54:53 GMT]]></title><description><![CDATA[<p>Das liegt daran, weil ein falsches Zeichen (keine Zahl wie int ja erwartet) eingegeben wurde und bei cin nun der Fehlerstatus verhängt ist. Das kannst du so lösen:</p>
<pre><code class="language-cpp">#include &lt;limits&gt;
if( !(std::cin &gt;&gt; variable) ) {
	std::cin.clear(); //fehlerstatus zurücksetzen
	std::cin.ignore(std::numeric_limits&lt;int&gt;::max(), '\n'); //fehlerhafte Zeichen überspringen
    continue; //oder sowas in der richtung
}
</code></pre>
<p>Dann sollte alles weitergehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016643</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016643</guid><dc:creator><![CDATA[Incocnito]]></dc:creator><pubDate>Fri, 04 Feb 2011 16:54:53 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 17:25:30 GMT]]></title><description><![CDATA[<p>Hey!</p>
<p>Vielen Dank jetzt funktioniert alles allerdings durchschaue ich den letzten Teil nicht ganz <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> --&gt; und ich will das verstehen was ich programmiere :D.</p>
<pre><code class="language-cpp">std::cin.ignore(std::numeric_limits&lt;int&gt;::max(), '\n'); //fehlerhafte Zeichen überspringen
</code></pre>
<p>Was bringt das ganze/was heißt std::numeric_limits&lt;int&gt;::max(), ?</p>
<p>Danke für eure Hilfe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016660</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016660</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Fri, 04 Feb 2011 17:25:30 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 18:06:37 GMT]]></title><description><![CDATA[<p>numeric_limits&lt;DATENTYP&gt;::max()/min() gibt die jeweilige größtmögliche/kleinstmögliche Zahl für den Datentyp an. Was std::cin.ignore() macht, erfährst du <a href="http://www.cplusplus.com/reference/iostream/istream/ignore/" rel="nofollow">hier</a>.</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016679</guid><dc:creator><![CDATA[Incocnito]]></dc:creator><pubDate>Fri, 04 Feb 2011 18:06:37 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 18:56:26 GMT]]></title><description><![CDATA[<p>Ok cin.ignore() hab ich verstanden und auch die Überprüfung ist logisch allerdings verwirrt mich das Dritte:</p>
<pre><code class="language-cpp">std::cin.ignore(std::numeric_limits&lt;int&gt;::max(), '\n'); //fehlerhafte Zeichen überspringen
</code></pre>
<p>Soweit ich das verstehen könnte ich auch genauso gut:</p>
<pre><code class="language-cpp">iselectedfield = 11;
</code></pre>
<p>damit wäre der Fehler doch genauso ausgewiesen?!? Oder verstehe ich da etwas falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016706</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016706</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Fri, 04 Feb 2011 18:56:26 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Fri, 04 Feb 2011 22:45:46 GMT]]></title><description><![CDATA[<p>Wenn du eine Variable vom Typ int hast und sie vom Standardeingabemedium eingeben lässt, werden die eingegeben Zeichen im Puffer gespeichert. Wenn also statt einer Zahl ein Buchstabe o.ä. eingegeben wird, dann hängen die Zeichen im Speicher rum. Du kannst dann solange den Fehlerstatus mit clear() auf 0 setzen wie du willst - durch die Zeichen, die immer noch da sind, wird er jedes mal aufs neue verhängt. Wenn du allerdings mit ignore() jedes fehlerhafte Zeichen aus dem Puffer &quot;verwirfst&quot;, dann sind keine fehlerhaften Zeichen mehr im Puffer und der Fehlerstatus wird erfolgreich dauerhaft auf 0 gesetzt - jetzt kann wieder eingegeben werden.<br />
Wenn ich was falsches gesagt habt korrigiert mich bitte, ich kenn mich da nicht so gut aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2016821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2016821</guid><dc:creator><![CDATA[Incocnito]]></dc:creator><pubDate>Fri, 04 Feb 2011 22:45:46 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Sat, 05 Feb 2011 16:58:14 GMT]]></title><description><![CDATA[<p>Ok, also funktioniert das so: das alles was in der Klammer steht heißt dann nur das in den Puffer der größt mögliche wert reingeschrieben wird was bei der Überprüfung wieder auffällt. Hab ich das jetzt richtig verstanden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017091</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017091</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Sat, 05 Feb 2011 16:58:14 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Sun, 06 Feb 2011 18:56:11 GMT]]></title><description><![CDATA[<p>OK, wieder 1 Problem bei der Namen Eingabe. Es wird 1 Buchstabe erkannt und dann heißt der Player hald kpieler1 statt Kuwiano, der Zweite Player wird garnicht erkannt oder verändert hier der Code:</p>
<pre><code class="language-cpp">char player1[] = &quot;Spieler1&quot;;                                            //Name für Spieler 1
char player2[] = &quot;Spieler2&quot;;                                            //Name für Spieler 2

void Playername(void)                                                   //Spieler Name Abfragen
{
    std::cout &lt;&lt; &quot;Spieler 1 gib deinen Namen ein: &quot;;
    std::cin &gt;&gt; player1[11];

    std::cout &lt;&lt; &quot;Spieler 2 gib deinen Namen ein: &quot;;
    std::cin &gt;&gt; player2[11];
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2017469</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017469</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Sun, 06 Feb 2011 18:56:11 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Sun, 06 Feb 2011 20:08:34 GMT]]></title><description><![CDATA[<p>Da ich keine Lust habe, dir das jetzt mit den C-Strings zu erklären (dafür gibt es genug andere Posts) empfehle ich dir nur, std::string zu verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017505</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017505</guid><dc:creator><![CDATA[Incocnito]]></dc:creator><pubDate>Sun, 06 Feb 2011 20:08:34 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Tue, 08 Feb 2011 17:19:07 GMT]]></title><description><![CDATA[<p>Hallo, wiedereinmal!</p>
<p>Ich möchte jetzt einen Timer in mein Spiel einbauen (Wenn ein Spieler dran ist zählt er von 10 runter --&gt; Beendet den Spielzug). Ich habe mal mit for schleifen und sleep(); probiert allerdings ohne Erfolg. Gibt es eine Möglichkeit das zu machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2018360</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018360</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Tue, 08 Feb 2011 17:19:07 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Tue, 08 Feb 2011 17:58:41 GMT]]></title><description><![CDATA[<p>Guck mal in #include &lt;ctime&gt; rein ...</p>
<pre><code>Zeit zeit = Zeit::jetzt()
Gib 10 aus 
while (Zeit::jetzt() &lt; zeit + 10 Sekunden)
{
   // guck ob spieler was eingegeben hat (cronio.h) oder so ... 
   // wenn nicht: 
    /// wenn Zeit::jetzt() &gt; zeit+9 Sekunde n gib 1 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+8 Sekunde n gib 2 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+7 Sekunde n gib 3 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+6 Sekunde n gib 4 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+5 Sekunde n gib 5 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+4 Sekunde n gib 6 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+3 Sekunde n gib 7 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+2 Sekunde n gib 8 aus,
    /// sonst wenn Zeit::jetzt() &gt; zeit+1 Sekunde n gib 9 aus,
    ///  - du musst dir auch merken welche Zeit du schon ausgegeben hast ... damit der nicht die Konsole vollspammt

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018371</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Tue, 08 Feb 2011 17:58:41 GMT</pubDate></item><item><title><![CDATA[Reply to TicTacToe -- Hilfe? on Tue, 08 Feb 2011 20:31:35 GMT]]></title><description><![CDATA[<p>Und wie kann ich mit conio.h prüfen ob der Spieler etwas eingegeben hat ohne dass das Programm stehen bleibt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2018452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018452</guid><dc:creator><![CDATA[Kuwiano]]></dc:creator><pubDate>Tue, 08 Feb 2011 20:31:35 GMT</pubDate></item></channel></rss>