### single player, tic tac toe problem

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182`` ``````#include #include #include #include #include /* 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<>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<>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"<

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?
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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183`` ``````#include #include #include #include #include /* 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<>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<>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"<

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

Check out that order of operations. 1%10 happens first.
i modified that with
 ``1234`` `````` 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?
 ``12`` ``````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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187`` ``````[code]#include #include #include #include #include /* 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<>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<>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"<
}[/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.