hi all,im new here and im a beginner at c++, im making a simple tic tac toe program, and its not working properly, it cant determine the winner,could someone please help.
heres the code
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<graphics.h>
#include<ctype.h>
#include<time.h>
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
cleardevice();
new_game: //label to start new game;
int i,j;
char choice;
int count1=0,count2=0,count3=0;
char square[3][3];
struct game_var
{
int row_user,column_user,row_comp,column_comp;
char plyr,comp;
}game_variables; //declaring game variables
settextstyle(3,0,20); //setting font
for(i=0;i<3;i++) //for1 loop to initialize all elements to space
{
for(j=0;j<3;j++)
{
square[i][j]=' ';
}
} //end of for1loop
cout<<endl;
cout<<"Welcome To Tic Tac Toe"<<endl;
cout<<"Choose x or o"<<endl;
cin>>game_variables.plyr;
switch(game_variables.plyr) //checking players choice
{
case 'x':game_variables.comp='o';
break;
case 'o':game_variables.comp='x';
break;
default :cout<<"Wrong Choice!!!! Starting Over..............";
goto new_game;
}//end of switch
cout<<"Generating board"<<endl;
for(i=0;i<3;i++) //for2 loop to display empty board
{
cout<<endl;
cout<<"|";
for(j=0;j<3;j++)
{
cout<<square[i][j]<<"|";
}
} //end of for2
cout<<endl;
user_input:
cout<<"Enter Row and Column(x and y Coordinates)"
<<" That Are Greater Than 0 And Less Than 3"<<endl;
cout<<"Row = "<<endl;
cin>>game_variables.row_user;
cout<<"Column = "<<endl;
cin>>game_variables.column_user;
if(game_variables.row_user>3||game_variables.column_user>3||game_variables.row_user<0||game_variables.column_user<0)
{
cout<<"Board Cannot Be Created, Starting Over............";
goto user_input;
}
cout<<"Computers Turn............."<<endl;
if(square[game_variables.row_user][game_variables.column_user]==game_variables.plyr||square[game_variables.row_user][game_variables.column_user]==game_variables.comp)
{
cout<<"Place Already Taken, Enter New Coordinates";
goto user_input;
}
comp_start:
randomize();
game_variables.row_comp=random(1-0+1)+0;
game_variables.column_comp=random(1-0+1)+0;
if(square[game_variables.row_comp][game_variables.column_comp]==game_variables.plyr)
{
goto comp_start;
}
square[game_variables.row_comp][game_variables.column_comp]=game_variables.comp;
square[game_variables.row_user][game_variables.column_user]=game_variables.plyr;
for(i=0;i<3;i++) //for3 loop to display current board
{
cout<<endl;
cout<<"|";
for(j=0;j<3;j++)
{
cout<<square[i][j]<<"|";
}
} //end of for3
cout<<endl;
for(i=0;i<3;i++) //for4 to check the numbers of squares used
{
for(j=0;j<3;j++)
{
if(square[i][j]==game_variables.plyr||square[i][j]==game_variables.comp)
count1++;
}
} //end of for4
for(i=0;i<3;i++) //for5 checking winning conditions for diagonals
{
for(j=0;j<3;j++)
{
if(i==j||i+j==2)
{
if(square[i][j]==game_variables.plyr)
{
count2++;
if(count2<3)
count2=0;
}
else if(square[i][j]==game_variables.comp)
{
count3++;
if(count3<3)
count3=0;
}
} //end of if
} //end of j
} //end of for5
for(j=0;j<3;j++)
{
if(square[0][j]==game_variables.plyr) //for user row
count2++;
else if(count2<3)
count2=0;
if(square[1][j]==game_variables.plyr)
count2++;
if(count2<3)
count2=0;
if(square[2][j]==game_variables.plyr)
count2++;
else if(count2<3)
count2=0;
}
for(i=0;i<3;i++)
{
if(square[i][0]==game_variables.plyr) //for user column
count2++;
else if(count2<3)
count2=0;
if(square[i][1]==game_variables.plyr)
count2++;
if(count3<=3)
count2=0;
if(square[i][2]==game_variables.plyr)
count2++;
else if(count2<3)
count2=0;
}
for(j=0;j<3;j++)
{
if(square[0][j]==game_variables.comp) //for comp row
count3++;
else if(count3<3)
count3=0;
if(square[1][j]==game_variables.comp)
count3++;
if(count2<3)
count3=0;
if(square[2][j]==game_variables.comp)
count3++;
else if(count3<3)
count3=0;
}
for(i=0;i<3;i++)
{
if(square[i][0]==game_variables.comp) //for comp comlumn
count3++;
else if(count3<3)
count3=0;
if(square[i][1]==game_variables.comp)
count3++;
if(count3<3)
count3=0;
if(square[i][2]==game_variables.comp)
count3++;
else if(count3<3)
count3=0;
}
check:
if(count3==3)
{
cout<<"Computer Has Won"<<endl;
goto game;
}
else if(count2==3)
{
cout<<"You have Won!! Congratulations"<<endl;getch();
game:
cout<<"Do You Want To Start A New Game y\n"<<endl;
cin>>choice;
switch(choice)
{
case 'y':goto new_game;
case 'Y':goto new_game;
case 'n':exit(0);
case 'N':exit(0);
} //end of switch
}
else if(count1<9&&count2<3&&count2<3)
goto user_input;
else if(count1==9&&count2<3&&count2<3 )
{
cout<<"Draw Game, Do You Want Start New Game y\n"<<endl;
cin>>choice;
switch(choice)
{
case 'y':goto new_game;
case 'Y':goto new_game;
case 'n':exit(0);
case 'N':exit(0);
}
}
getch();
}
also i wasnt able to use graphics to draw a grid, could someone please tell how to do that.im using turbo C++,also if someone could give a simpler code for determing the winner or draw match,it would be very helpful.
thank you