#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();
}
每当启动函数 game() 时,get(c) 第一次在循环中不起作用(第 90 行)。之后它在下一次循环迭代中工作正常。但每当重赛开始时,游戏功能都会发生。重新启动并且 get(c) 将无法再次正常工作。 关于游戏—— 这个游戏是“dot box”。玩家将相邻的点连接起来形成一条线。他们轮流轮流。如果玩家划出一条线完全完成一个方框,他将得分并在方框中写下他名字的第一个字母以进行标记。然后他会再转一次。如果他再次得分,他就会继续轮流。这是一场有趣的比赛。请帮我解决第 90 行的问题。
最佳答案
早期的 scanf("%d", ...);
指令将在输入缓冲区中留下一个换行符
,该换行符由
scanf("%c", &y);
而不是您期望的输入。
要消耗输入缓冲区中任何先前的空白字符,需要在 %c
之前有一个 ' '
(空格)
scanf(" %c", &y);
关于c++ - 每当启动函数 game() 时,get(c) 在循环中第一次不起作用(第 90 行)。之后它在下一个循环迭代中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30580304/