fresh made tic tac toe !

hey guyzzz, just made a tic tac toe...
i wanted your opinions on how can i restart it, for the convenience of the user.
i want your opinion for the logic and the program i made.
i know all about the system(..)things... but my compiler wont let me use clrscr(); ..so i used system("CLS").. i managed to comy the box from a third party site[was too lazy to make it, sorrry]..
and please dont tell me about the no. of lines, i was way too ignorant for them..
if you think anything else could be improved..please tell...thank you ...{ sorry , if you think that the code is stupid, i am a novice and started learning c++ since a month.. i am not learning it "legally".. i am just 15 , i am reading tutorials of the internet and youtube things and all that stuff... okay, sorry for the blabbing ..here is the code
____________________________________________________________________________

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
void box();
void player1_won();
void player2_won();
void fu();
int main()
{
    again:
    string x[3], z="sayTanmayRoxx", p;
	char f, g;
	int a;
	int d;
	cout<<"enter password\n";
	cin>>p;
	if(p==z)
	{
	cout<<"\aenter player 1 name\n";
	cin>>x[0];
	system("CLS");
	cout<<"\aenter player 2 name\n";
	cin>>x[1];
	system("CLS");  
	for(a=1;(a<=9);a++)
	{
		int b=1, c=1;
		system("CLS");	
		box();
if(a%2==1)
{
         
	cout<<x[0]<<"  as player 1 , enter your number\n";
	cin>>b;
	if(b==1&&square[1]=='1')
	square[1]='x';
	else if(b==2&&square[2]=='2')
	square[2]='x';
	else if(b==3&&square[3]=='3')
	square[3]='x';
	else if(b==4&&square[4]=='4')
	square[4]='x';
	else if(b==5&&square[5]=='5')
	square[5]='x';
	else if(b==6&&square[6]=='6')
	square[6]='x';
	else if(b==7&&square[7]=='7')
	square[7]='x';
	else if(b==8&&square[8]=='8')
	square[8]='x';
	else if(b==9&&square[9]=='9')
	square[9]='x';
	else if(b<1||b>9)
	cout<<"please , dont troll\a";
}

else 
{
     cout<<x[1]<<" as player 2 , enter your number\n";
cin>>b;
	if(b==1&&square[1]=='1')
	square[1]='o';
	else if(b==2&&square[2]=='2')
	square[2]='o';
	else if(b==3&&square[3]=='3')
	square[3]='o';
	else if(b==4&&square[4]=='4')
	square[4]='o';
	else if(b==5&&square[5]=='5')
	square[5]='o';
	else if(b==6&&square[6]=='6')
	square[6]='o';
	else if(b==7&&square[7]=='7')
	square[7]='o';
	else if(b==8&&square[8]=='8')
	square[8]='o';
	else if(b==9&&square[9]=='9')
	square[9]='o';
	else if(b<1||b>9)
	cout<<"please , dont troll\a";
}
 if(square[1]=='x'&&square[2]=='x'&&square[3]=='x')
 {
 player1_won();
  return 0;
}
  else if(square[1]=='x'&&square[4]=='x'&&square[7]=='x')
  {
player1_won();
  return 0;
}
else if(square[3]=='x'&&square[6]=='x'&&square[9]=='x')
 {
 player1_won();
  return 0;
}
  else if(square[7]=='x'&&square[8]=='x'&&square[9]=='x')
  {
 player1_won();
  return 0;
}
else if(square[4]=='x'&&square[5]=='x'&&square[6]=='x')
{
    player1_won();
     return 0;
     }
  else if(square[1]=='o'&&square[2]=='o'&&square[3]=='o')
  {
 player2_won();
  return 0;
}
  else if(square[1]=='o'&&square[4]=='o'&&square[7]=='o')
  {
player2_won();
  return 0;
}
else if(square[4]=='x'&&square[5]=='x'&&square[6]=='x')
{
     player2_won();
     return 0;
     }
else if(square[3]=='o'&&square[6]=='o'&&square[9]=='o')
 {
 player2_won();
  return 0;
}
}
system("CLS");
cout<<"THE GAME IS DRAW"<<endl;
cout<<"press another , key for exit :d\n";
getch();
return 0;
}
}
void box()
{
	system("CLS");
	cout << "\n\n\tTic Tac Toe\n\n";
	cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
	cout << endl;
	cout << "     |     |     " << endl;
	cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;
	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;
	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;
	cout << "     |     |     " << endl << endl;
}
void player1_won()
{
     system("cls");
  cout<<"player 1 , has won!!!!!!!!";
  getch();
     }
     void player2_won()
     {
          system("cls");
  cout<<"player 2 , has won!!!!!!!!";
  getch();
          }



Instead of doing this:
1
2
if(b==1&&square[1]=='1')
	square[1]='x';


You could do this instead:
1
2
if(square[b] != 'x' || square[b] != 'o')
        square[b] = 'x';


Then you won't have to go through all those if statements.
Last edited on
Topic archived. No new replies allowed.