TicTacToe game

I have few problems with my code.
I don't know why system('CLS') isn`t working
second checking the winner.. even if some wins game never stops ( later I will write a code to check who won, but for now I need to game stops as someone wins)
And the third i need I code to start a new game( example) I would love to use goto, but its forbidden for this project( and as i have read using goto is a bad practice( something like breaking the logic of code and making it hard to read? )
Sorry for my bad English and I hope someone can help me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <stdlib.h> 
using namespace std;

	char l[10] = {'0','1','2','3','4','5','6','7','8','9'}; // table
	int vic(); //victory
	void table(); // table
	void pc(); // playing with pc
	void cil(); // 2 players
	int N=0 ,i=1, d, player=1;
	
	
	int main()
	{
		int g;
		cout << "Who will play-chose 1 if player vs pc, chose 2 if 2 players:  ";
		cin >> g;
		if (g==1) // if pc
		{
		    pc(); // i havent writen this jet
		}
		else // players
		{
		    do 
		    {         
		        cil();
		        N++;
		        system("CLS");		          
		        vic();
		    }
		    while (N!=9 || i==d );
		 }                              
	}
	
		void pc()
		{
		    cout <<"still working on it ";
		}

	void cil()
{	
		int z;
		
	if (player==1)
		{
		cout << "player 1 move: ";
		cin >>z;
		l[z]= 'X';
		player++;
		table();	  
		}
		else
		{
		cout << "player 2 move: ";
		cin >>z;
		l[z]='O';
		player--;
		table();	  
		}	
}
	
	int vic()
	{
	    
	    int d;
		if ( l[1] == l[2] && l[2] == l[3])
		return d=1;
		else if ( l[4] == l[5] && l[5] == l[6])
		return d=1;
		else if (l[7] == l[8] && l[8] == l[9])
		return d=1;
		else if (l[1] == l[4] && l[4] == l[7])
		return d=1;
		else if (l[2] == l[5] && l[5] == l[8])
		return d=1;
		else if (l[3] == l[6] && l[6] == l[9])
		return d=1;
		else if (l[1] == l[5] && l[5] == l[9])
		return d=1;
		else if (l[3] == l[5] && l[5] == l[7])
		return d=1;
		else return d=0;
	}
	
	void table() 
{

	cout << "\n Tic Tac Toe\n\n";
	cout << "player 1 (X)  -  player 2 (O)" << endl << endl;
	cout << endl;
	cout << "     |     |     " << endl;
	cout << "  " << l[1] << "  |  " << l[2] << "  |  " << l[3] << endl;
	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << l[4] << "  |  " << l[5] << "  |  " << l[6] << endl;
	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << l[7] << "  | " <<  l[8] << "   |  " << l[9] << endl;
	cout << "     |     |     " << endl << endl;
}
Ok .. for now I did one more while loop to reset a game, but I want to know is that the right way?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
	int main()
	{
		char atb;
		do
		{
			int g;		
			char l[10] = {'0','1','2','3','4','5','6','7','8','9'}; // laukums
			cout << "Who will play-chose 1 if player vs pc, chose 2 if 2 players:  ";
			cin >> g;
			if (g==1) // ja pret pc
			{
				pc();
					cout << "will you play again Y y if yes N n if not: " <<endl;
				cin >> atb;
			}
			else 		
			{
				do 
				{   	        
					cil();
					skaits++;
					system("CLS");
					galds();	        
					uzvara();
				}
				while (skaits!=9 || i==d );
				{
				cout << "winner : " << " " << endl;// I will edit this
				cout << "will you play again Y y if yes N n if not: " <<endl;
				cin >> atb;
				}
			 } 
		}
		while ((atb == 'Y')||(atb == 'y'));
		


Last edited on
Topic archived. No new replies allowed.