每当启动函数 game() 时,get(c) 第一次在循环中不起作用(在第 90 行中)。

Whenever function game() is initiated, get(c) doesn't work for first time in loop (in line no. 90).after that it works fine for next loop iteration

本文关键字:循环 不起作用 在第 第一次 行中 get 函数 启动 game      更新时间:2023-10-16
#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 18n");
    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 10n");
        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 = %dn%s =%dn",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 => %dnn",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 turnn",p1);}
    if(p%2==1){printf("%s's turnn",p2);}
    printf("enter first coordinaten");
    gets(c);
    if(c[0]=='q'){g=0;continue;
    }
    printf("enter second coordinaten");
    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]=='')i1=c[1]-'0';  /* correction if single digit is entered */
    if(d[2]=='')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 %dn",i1,i2,is);
    if(i3!=1){printf(">> invalid moven");continue;}
    if(!((c[0]-'a')<N)&&!((d[0]-'a')<N)){printf(">>invalid moven");continue;}
    if(!(i1<(M+1))&&!(i2<(M+1))){printf(">>invalid moven");continue;}
    if(c[0]==d[0]){j=c[0]-'a';j=2*j;k=i1*2+1;if(a[j][k]=='_'){printf(">>repeated linen");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 linen");continue;}else a[j][k]='|';}
    check(c[0],d[0],i1,i2,&f);
    p++;
    if (f==1)p--;
    printf("%dn",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 exitn");
    boxsize();
    game();
    printf("game overn");  
    return 0;
    getch();
}
每当函数 game(( 被初始化时,get(c( 第一次在循环中不起作用(在第

90 行中(,之后它就可以很好地进行下一次循环迭代。 但是每当重新启动游戏功能时,游戏功能都会重新启动,get(c( 将无法再次正常工作。关于游戏-这个游戏是"点盒"。玩家将相邻的点连接起来形成一条线。他们轮流。如果一名球员在盒子上画了一条线,他将得分并在盒子里写下他名字的第一个字母来标记它。到时候他会再转一轮。如果他再次进球,他会继续轮流得分。这是一个有趣的游戏。请帮助我解决第 90 行中的问题。

前面的scanf("%d", ...);指令将在输入缓冲区中留下一个newline,该缓冲区由

scanf("%c", &y);

而不是您期望的输入。

若要在输入缓冲区中使用任何以前的空格字符,它需要在%c之前有一个' '(空格(

scanf(" %c", &y);