#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int p=0;
int g=1;int p1s=0;int p2s=0;
char pl,pt1,pt2;
char p1[20];
char p2[20];
char a[17][35];
int N;
int M;
void game();
void boxsize(){
printf("Enter the no. of dots you want in a horizontal row (should be less than 18) :\n");
scanf("%d",&M);
while(M>17){
printf("should be less than 18\n");
printf("Enter the no. of dots you want in a horizontal row (should be less than 18) :\n");
scanf("%d",&M);
}
printf("Enter the no. of such rows you want (should be less than 10) :\n");
scanf("%d",&N);
while(N>9){
printf("should be less than 10\n");
printf("Enter the no. of such rows you want (should be less than 10) :\n");
scanf("%d",&N);
}
}
void result(){
printf("\n FINAL SCORE >----->> \n %s = %d\n%s =%d\n",p1,p1s,p2,p2s);
if(p1s>p2s)printf("=========> %s WINS <=========\n",p1);
if(p1s<p2s)printf("=========> %s WINS <=========\n",p2);
if(p1s==p2s)printf("=========> MATCH DRAW <=========\n");
}
void getname(){
printf("Enter first player's name:\n");
gets(p1);
printf("Enter second player's name:\n");
gets(p2);
pt1=p1[0];
pt2=p2[0];
if(p1[0]==p2[0])pt2=p2[1];
}
void printer(){
int i,j;
system("cls");
printf("score ----> %s => %d %s => %d\n\n",p1,p1s,p2,p2s);
printf(" 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 \n");
for(i=0;i<(2*N-1);i++){if(i%2==0)printf("%c ",'a'+(i/2));else printf(" ");for(j=0;j<(2*M+1);j++)printf("%c ",a[i][j]);printf("\n");}
printf("\n");
}
void check(char c,char d,int i1,int i2,int *f){int i,j,k,l;
if(c==d){
i=c-'a';i=2*i;j=2*i1+1;
if(a[i-2][j]=='_' && a[i-1][j-1]=='|' && a[i-1][j+1]== '|'){*f=1;if(p%2==0){p1s++;a[i-1][j]=pt1;};if(p%2==1){p2s++;a[i-1][j]=pt2;}};
if(a[i+2][j]=='_' && a[i+1][j-1]=='|'&& a[i+1][j+1]== '|'){*f=1;p--;if(p%2==0){p1s++;a[i+1][j]=pt1;};if(p%2==1){p2s++;a[i+1][j]=pt2;}};
;
}
if(i1==i2){
i=c-'a';i=2*i;j=2*i1;
if(a[i][j-1]=='_'&&a[i+2][j-1]=='_'&&a[i+1][j-2]=='|'){*f=1;if(p%2==0){p1s++;a[i+1][j-1]=pt1;};if(p%2==1){p2s++;a[i+1][j-1]=pt2;}};
if(a[i][j+1]=='_'&&a[i+2][j+1]=='_'&&a[i+1][j+2]=='|'){*f=1;if(p%2==0){p1s++;a[i+1][j+1]=pt1;};if(p%2==1){p2s++;a[i+1][j+1]=pt2;}};
}
}
void rematch (){
char y,z;
printf("Do you want to play again (y/n)\n");
scanf("%c",&y);
if(y=='y'){
game();
}
}
void game(){ int i,j,k,i1,i2,i3,is,f=0;
p1s=0;p2s=0;
for(i=0;i<(2*N-1);i++){for(j=0;j<(2*M+1);j++){if(i%2==0){if(j%2==0)a[i][j]='.';else a[i][j]=' ';}else a[i][j]=' ';}}
printer();g=1;
char c[4];char d[4];char e[4];
while(g==1){ f=0;if(p%2==0){printf("%s's turn\n",p1);}
if(p%2==1){printf("%s's turn\n",p2);}
printf("enter first coordinate\n");
gets(c);
if(c[0]=='q'){g=0;continue;
}
printf("enter second coordinate\n");
gets(d);
if(d[0]=='q'){g=0;continue;
}
i1=10*(c[1]-'0')+(c[2]-'0');
i2=10*(d[1]-'0')+(d[2]-'0');
if(c[2]=='\0')i1=c[1]-'0'; /* correction if single digit is entered */
if(d[2]=='\0')i2=d[1]-'0';
if(c[0]>d[0]||i1>i2){strcpy(e,c);strcpy(c,d);strcpy(d,e);is=i1;i1=i2;i2=is;} /*correction if entered in reverse order*/
i3=d[0]-c[0]+i2-i1;
i3=d[0]-c[0]+i2-i1;
printf("%d %d %d\n",i1,i2,is);
if(i3!=1){printf(">> invalid move\n");continue;}
if(!((c[0]-'a')<N)&&!((d[0]-'a')<N)){printf(">>invalid move\n");continue;}
if(!(i1<(M+1))&&!(i2<(M+1))){printf(">>invalid move\n");continue;}
if(c[0]==d[0]){j=c[0]-'a';j=2*j;k=i1*2+1;if(a[j][k]=='_'){printf(">>repeated line\n");continue;}else a[j][k]='_';}
if(i2==i1){j=c[0]-'a';j=2*j+1;k=i1*2;if( a[j][k]=='|'){printf(">>repeated line\n");continue;}else a[j][k]='|';}
check(c[0],d[0],i1,i2,&f);
p++;
if (f==1)p--;
printf("%d\n",p);
printer();
}
result();
rematch();
}
int main (){
getname();
printf("Coordinate of point has to be entered as first alphabet and then followed by number. e.g. - a11,b4,e05,c10.\nenter coordinate as q at any time to exit\n");
boxsize();
game();
printf("game over\n");
return 0;
getch();
}
Whenever function game() is intiated, get(c) doesnt work for first time in loop (in line no. 90).after that it woks fine for next loop iteration. but whenever rematch is initiated game func. is initiated all over again and get(c) will not work properly again. About the game- this game is 'dot box'. Players join adjacent points to form a line. They take turn alternately. If a player makes a line that finishes a box completly he will score and write his name's first letter in box to mark it. He will again take another turn then. If he scores again he continues taking turn.Its an interesting game. please help me out with the problem in line 90.