single player, tic tac toe problem

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <cstdlib>
#include <ctime>
#include <iostream>
 #include<conio.h>
 #include<string.h>      /* time */
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()
{
    int abcd[45];
    char type[34];
    string data[56];
    again:
    string x[3], z="sayTanmayRoxx", p;
	char f, g;
	int a;
	int d;
	cout<<"enter password\n";
	cin>>p;
	if(p==z)
	{
            cout<<"player 1 or player 2?\n";
            cin>>abcd[0];
	cout<<"\aenter player 1 name\n";
	cin>>x[0];
	system("CLS");
	if(abcd[0]==1)
	goto first;
	cout<<"\aenter player 2 name\n";
	cin>>x[1];
	system("CLS");
    first:  
	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 
{
     if(abcd[0]==1)
     {
      srand((unsigned)time(NULL)); 
     abcd[1]=rand()+1%10;
     abcd[1]=b;
       goto second;         
                }
     cout<<x[1]<<" as player 2 , enter your number\n";
cin>>b;
second:
	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();
          }


hey guys, i was making a tic-tac toe. IN which the user has the provision to play 1 Player also. But i am having some problems with the rand() operator. It is not fully random. whenever you compiler it, its gonna choose ! only as its first preference. Then it wont change, its preference. It keeps on choosing 1. Can you tell me what i am doing wrong?
Thank you, in advance.
You should call srand ONCE at the very start of main, and never again. If you keep calling it, it will keep resetting to the same see value, and thus keep generating the same random sequence.
But it is still not working. As you said i changed the code to the following:
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <cstdlib>
#include <ctime>
#include <iostream>
 #include<conio.h>
 #include<string.h>      /* time */
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()
{
      srand((unsigned)time(NULL)); 
    int abcd[45];
    char type[34];
    string data[56];
    again:
    string x[3], z="sayTanmayRoxx", p;
	char f, g;
	int a;
	int d;
	cout<<"enter password\n";
	cin>>p;
	if(p==z)
	{
            cout<<"player 1 or player 2?\n";
            cin>>abcd[0];
	cout<<"\aenter player 1 name\n";
	cin>>x[0];
	system("CLS");
	if(abcd[0]==1)
	goto first;
	cout<<"\aenter player 2 name\n";
	cin>>x[1];
	system("CLS");
    first:  
	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 
{
     if(abcd[0]==1)
     {
    
     abcd[1]=rand()+1%10;
     abcd[1]=b;
       goto second;         
                }
     cout<<x[1]<<" as player 2 , enter your number\n";
cin>>b;
second:
	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();
          }

Am i still doing anything wrong?
rand()+1%10

Check out that order of operations. 1%10 happens first.
i modified that with
1
2
3
4
 abcd[1]=rand()%10;
     abcd[1]=b;
     if(b==0)
     b=b+1;

that did'nt work either.

It may sound silly, but is it my compiler (dev c++ 4.9.92)?
that has the glitch?
1
2
abcd[1]=rand()%10;
abcd[1]=b;
Look very closely.
Last edited on
it should be, b = abcd[1];, right?
Basically what you have to do is:

rand()%9+1

So then computer will generate a number between 1 and 9
nope, that is not the problem.
Problem was that the random no. generator was not truly random.
But now it is fixed.
thank you @LB.
so 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
[code]#include <cstdlib>
#include <ctime>
#include <iostream>
 #include<conio.h>
 #include<string.h>      /* time */
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()
{
    int abcd[45];    
    char type[34];
    string data[56];
    again:
    string x[3], z="sayTanmayRoxx", p;
	char f, g;
	int a;
	int d;
	cout<<"enter password\n";
	cin>>p;
	if(p==z)
	{
            cout<<"player 1 or player 2?\n";
            cin>>abcd[0];
	cout<<"\aenter player 1 name\n";
	cin>>x[0];
	system("CLS");
	if(abcd[0]==1)
	goto first;
	cout<<"\aenter player 2 name\n";
	cin>>x[1];
	system("CLS");
    first:  
	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 
{
     if(abcd[0]==1)
     {srand((unsigned)time(NULL)); 
    abcd[1]=rand()%10;
     b = abcd[1];
     if(b==0)
     b=b+1;
       goto second;         
                }
     cout<<x[1]<<" as player 2 , enter your number\n";
cin>>b;
second:
	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";
	else if(square[b]=='o')
	goto second;
	else if(square[b]=='x')
	goto second;
}
 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();
     
}[/code]

what do you think about it, huh?
According to me some cons of this program are:
1. use of goto
2. use of system("") // i knew about the alternative, but i was little ignorant for that, at that time
3. There are times, when the random no. generator chooses a no. which has been already choose earlier. This creates an infinite loop and the cpu fan gets really violent
4. the program is ugly.
I would be highly obliged, if you help me overcome them also....

Topic archived. No new replies allowed.