int movenum1=0,movenum2=0;
const int Xpos[]={35,41,47,35,41,47,35,41,47};
const int Ypos[]={20,20,20,25,25,25,30,30,30};
int Player1Moves[11]={0};
int Player2Moves[11]={0};
void StoreValue(int store,int playernum)
{
if (playernum==1) Player1Moves[movenum1++]=store;
else if (playernum==2) Player2Moves[movenum2++]=store;
}
int WhatMove(char x[],char y,int playernum)
/* 1 for valid move, 0 for invalid and control returns to Player(1/2)Move
for another input */
{
if ((strcmp(x,"A1")==0)||(strcmp(x,"a1")==0))
{
gotoxy(Xpos[0],Ypos[0]);
cout<<y;
StoreValue(1,playernum);
return 1;
}
else if ((strcmp(x,"B1")==0)||(strcmp(x,"b1")==0))
{
gotoxy(Xpos[1],Ypos[1]);
cout<<y;
StoreValue(2,playernum);
return 1;
}
else if ((strcmp(x,"C1")==0)||(strcmp(x,"c1")==0))
{
gotoxy(Xpos[2],Ypos[2]);
cout<<y;
StoreValue(3,playernum);
return 1;
}
else if ((strcmp(x,"A2")==0)||(strcmp(x,"a2")==0))
{
gotoxy(Xpos[3],Ypos[3]);
cout<<y;
StoreValue(4,playernum);
return 1;
}
else if ((strcmp(x,"B2")==0)||(strcmp(x,"b2")==0))
{
gotoxy(Xpos[4],Ypos[4]);
cout<<y;
StoreValue(5,playernum);
return 1;
}
else if ((strcmp(x,"C2")==0)||(strcmp(x,"c2")==0))
{
gotoxy(Xpos[5],Ypos[5]);
cout<<y;
StoreValue(6,playernum);
return 1;
}
else if ((strcmp(x,"A3")==0)||(strcmp(x,"a3")==0))
{
gotoxy(Xpos[6],Ypos[6]);
cout<<y;
StoreValue(7,playernum);
return 1;
}
else if ((strcmp(x,"B3")==0)||(strcmp(x,"b3")==0))
{
gotoxy(Xpos[7],Ypos[7]);
cout<<y;
StoreValue(8,playernum);
return 1;
}
else if ((strcmp(x,"C3")==0)||(strcmp(x,"c3")==0))
{
gotoxy(Xpos[8],Ypos[8]);
cout<<y;
StoreValue(9,playernum);
return 1;
}
else
{
cout<<"Error";
return 0;
}
}
void Player1Move()
{
char name[2];
gotoxy(1,4);
clreol();
cout<<"Player 1 : Enter position : ";
gets(name);
int ret=WhatMove(name,88,1); //88 is ASCII for X
if (ret==0) Player1Move();
}
void Player2Move()
{
char name[2];
gotoxy(1,5);
clreol();
cout<<"Player 2 : Enter position : ";
gets(name);
int ret=WhatMove(name,79,2); //79 is ASCII for O
if (ret==0) Player2Move();
}
int Player1Win(int x)
{
int ret=0;
for (int i=0;Player1Moves[i]!=0;i++) if (Player1Moves[i]==x) ret=1;
return ret;
}
int Player2Win(int x)
{
int ret=0;
for (int i=0;Player2Moves[i]!=0;i++) if (Player2Moves[i]==x) ret=1;
return ret;
}
int IsGameWin()
{
int win=0;
if (Player1Win(1)==1&&Player1Win(2)==1&&Player1Win(3)==1) win=1;
else if (Player2Win(1)==1&&Player2Win(2)==1&&Player2Win(3)==1) win=2;
else if (Player1Win(4)==1&&Player1Win(5)==1&&Player1Win(6)==1) win=1;
else if (Player2Win(4)==1&&Player2Win(5)==1&&Player2Win(6)==1) win=2;
else if (Player1Win(7)==1&&Player1Win(8)==1&&Player1Win(9)==1) win=1;
else if (Player2Win(7)==1&&Player2Win(8)==1&&Player2Win(9)==1) win=2;
else if (Player1Win(1)==1&&Player1Win(4)==1&&Player1Win(7)==1) win=1;
else if (Player2Win(1)==1&&Player2Win(4)==1&&Player2Win(7)==1) win=2;
else if (Player1Win(2)==1&&Player1Win(5)==1&&Player1Win(8)==1) win=1;
else if (Player2Win(2)==1&&Player2Win(5)==1&&Player2Win(8)==1) win=2;
else if (Player1Win(3)==1&&Player1Win(6)==1&&Player1Win(9)==1) win=1;
else if (Player2Win(3)==1&&Player2Win(6)==1&&Player2Win(9)==1) win=2;
else if (Player1Win(1)==1&&Player1Win(5)==1&&Player1Win(9)==1) win=1;
else if (Player2Win(1)==1&&Player2Win(5)==1&&Player2Win(9)==1) win=2;
else if (Player1Win(3)==1&&Player1Win(5)==1&&Player1Win(7)==1) win=1;
else if (Player2Win(3)==1&&Player2Win(5)==1&&Player2Win(7)==1) win=2;
return win;
}
int IsGameDraw() //1 for draw, otherwise 0
{
int x=0,y=0,win=0,ret=0;
for (int i=0;Player1Moves[i]!=0;i++,x++);
for (i=0;Player2Moves[i]!=0;i++,y++);
win=IsGameWin();
if ((x+y==9)&&(win==0)) ret=1;
return ret;
}
void End(const char msg[15])
{
int posx,posy;
int n=strlen(msg);
randomize();
while(!kbhit())
{
clrscr();
for (int i=0;i<n;i++)
{
posx=random(80)+1;
posy=random(50)+1;
gotoxy(posx,posy);
cout<<msg[i];
}
delay(1000);
}
getch();
}
void main()
{
clrscr();
gotoxy(30,1);
cout<<"TIC TAC TOE"<<endl;
cout<<"Player 1 has crosses(X) and Player 2 has noughts(O)"<<endl;
int n=random(2)+1;
cout<<"Let's see who starts : Player "<<n<<endl;
int win=0,draw=0;
FrontEnd();
if (n==1)
{
for (int i=0;i<6;i++)
{
Player1Move();
Player2Move();
int x=IsGameWin();
int y=IsGameDraw();
if ((x==1)||(x==2)) break;
if (y==1) break;
}
}
else if (n==2)
{
for (int i=0;i<6;i++)
{
Player2Move();
Player1Move();
int x=IsGameWin();
int y=IsGameDraw();
if ((x==1)||(x==2)) break;
if (y==1) break;
}
}
win=IsGameWin();
draw=IsGameDraw();
if (win==1)
{
const char msg[15]={'P','l','a','y','e','r',' ','1',' ','W','I','N','S','!','\0'};
End(msg);
}
if (win==2)
{
const char msg[15]={'P','l','a','y','e','r',' ','2',' ','W','I','N','S','!','\0'};
End(msg);
}
if (draw==1)
{
const char msg[10]={'G','a','m','e',' ','D','r','a','w','\0'};
End(msg);
}
}
for (int i=0;Player1Moves[i]!=0;i++,x++);
for (i=0;Player2Moves[i]!=0;i++,y++);
This is invalid C++. If your compiler allows this code, you should think about changing it.
The second 'i' is not declared.
You should redeclare as you did with the first.
Also I get many errors about gotoxy and clreol, they probably come from conio.h which is deprecated/not liked.
I am limited to using the turbo c++ 3.0 compiler, due to this being a school project. Turbo C++ 3.0 recognizes the header files which u can see here, but it does not support the ones u mentioned.
Also if i declare the second i, then it gives me an error saying "Multiple Declarations for i".
I see, it used to be like that in VS6 too, about the redeclaration of 'i'. About the others, I'm sad there is few people here that will be able to help you, and I'm not one of them :|
DPA wrote:
I am limited to using the turbo c++ 3.0 compiler, due to this being a school project.
I wonder, schools are supposed to TEACH GOOD THINGS to students.
Why are the students forced to use OLD and UNSAFE programs?
Please, after school, if you are going to keep programming in C++, start from 0 and begin with something like GCC/MingW.
I know that my compiler is outdated but i am as frustrated as any of u wishing to help me. Schools are supposed to teach one useful things and ours is teaching us outdated ones, which are of no real use any longer.