#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()%10;
     abcd[1]=b;
     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";
}
 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.

Recommended Answers

All 3 Replies

line 76 should be

b = abcd[1];

not

abcd[1]=b

Also the use of goto is a big no no for c++. With the loop options availible you should never need to use one. You also need to learn how to indent properly.

commented: Indentation is very important. +2

:-) Good points Nathan! Tanmay, if you want to really understand random number generators, go visit http://www.phy.ornl.gov/csep/CSEP/RN/RN.html

It is the best tutorial I have seen regarding random number generators! I have been involved with this field for 30+ years, so I think I can say that without reservation!

FWIW, this tutorial is complements of the US Oak Ridge National Laboratory, where Frank Oppenheimer et al developed the first Uranium enrichment program that led to the atomic bomb in 1945. Also, Frank O. was my college physics professor at the University of Colorado in 1966.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.