井字游戏板验证板

时间:2019-07-14 03:32:09

标签: c multidimensional-array

我正在验证井字游戏板,看是否有正确数量的x和o。 x必须比o小2,反之亦然,这是因为没有“多余的转弯”,我们不能假设o首先。

我正在使用z来查找数组中x和o的#的差,由于某种原因z不能保持值。

void IsValidBoard(char board[][3]){
int x=0;
int o=0;
int z=0;
char a;
char b;
char c;
    for (int i=0;i<3;i++){
        a=board[0][i];
        b=board[1][i];
        c=board[2][i];
        if(a=='o'){
            o++;
        }
        else if(a=='x'){
            x++;
        }
        if(b=='o'){
            o++;
        }
        else if(b=='x'){
            x++;    
        }
        if(c=='o'){
            o++;
        }
        else if(c=='x'){
            x++;
        }


}
        z==abs(o-x);
        printf("z=%d\n", z);
            printf("%d x('s), %d o('s) ->", x,o);
        if( z>2){
            printf("Invalid Board\n");
        }
        else {
            printf("Board is Valid\n");
        }

return ;



   Please enter x or o: o

     |     |
  1  |  2  |  o
_____|_____|_____
     |     |
  4  |  o  |  o
_____|_____|_____
     |     |
  7  |  8  |  9
     |     |
z=0
0 x('s), 3 o('s) ->Board is Valid
Enter the number of the cell where you want to insert X or 0 or enter -1 to exit
8
Please enter x or o: x

     |     |
  1  |  2  |  o
_____|_____|_____
     |     |
  4  |  o  |  o
_____|_____|_____
     |     |
  7  |  x  |  9
     |     |
 z=0
 1 x('s), 3 o('s) ->Board is Valid
 Enter the number of the cell where you want to insert X or 0 or enter -1 
 to exit

0 个答案:

没有答案
相关问题