Help Me!!!!!!!!!!

Dear All

I am making a 2-player tic-tac-toe game in c++ (compiler: turbo c++ 3.0) but my code just refuses to work. The program bombs every tiume i try it.

Also i am limited to using the aforementioned compiler.

PLEASE HELP ME !

DPA

The program code :

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>

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();
}

void FrontEnd() //to be renamed later as FrontEnd
{
char lt=201,e=205,t=203,rt=187,pl=186,l=204,j=206,r=185,lb=200,b=202,rb=188;
gotoxy(30,13);
cout<<"TIC TAC TOE : 2 Player";
gotoxy(35,16);
cout<<"A"<<" "<<"B"<<" "<<"C";
gotoxy(32,18);
cout<<lt<<e<<e<<e<<e<<e<<t<<e<<e<<e<<e<<e<<t<<e<<e<<e<<e<<e<<rt<<endl;
gotoxy(32,19);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(29,20);
cout<<1<<" "<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,21);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,22);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,23);
cout<<l<<e<<e<<e<<e<<e<<j<<e<<e<<e<<e<<e<<j<<e<<e<<e<<e<<e<<r<<endl;
gotoxy(32,24);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(29,25);
cout<<2<<" "<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,26);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,27);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,28);
cout<<l<<e<<e<<e<<e<<e<<j<<e<<e<<e<<e<<e<<j<<e<<e<<e<<e<<e<<r<<endl;
gotoxy(32,29);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(29,30);
cout<<3<<" "<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,31);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,32);
cout<<pl<<" "<<pl<<" "<<pl<<" "<<pl<<endl;
gotoxy(32,33);
cout<<lb<<e<<e<<e<<e<<e<<b<<e<<e<<e<<e<<e<<b<<e<<e<<e<<e<<e<<rb<<endl;
cout<<endl;
}

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);
}
}


Try beginning with substituting
void End(const char msg[15])
with
void End(const char * msg)

Also:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
}


You can change it to:

1
2
3
4
5
6
if(win == 1)
    End("Player 1 WINS!");
else if(win == 2)
    End("Player 2 WINS!");
else if(draw == 1)
    End("Game Draw.");


EDIT:
These are wrong:

1
2
3
4
5
6
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h> 


Use these instead:

1
2
3
4
5
6
7
#include <iostream>
#include <conio.h>
#include <dos.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;


1
2
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.
Last edited on
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.
Last edited on
Are you using the std namespace?

Aceix.
Last edited on
Wanted to help but it'll take longer cause of the "outdated" non-standard header you're using(<conio.h>) with its associated functions.

Aceix.
no turbo c++ 3.0 does not support namespaces.

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.
Topic archived. No new replies allowed.