用C语言编写的TicTacToe游戏的奇怪输出

2q5ifsrm  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(117)

我是一个初学C语言的程序员,在用C语言写一个tictactoe游戏时,我遇到了一个奇怪的输出。我主要使用if语句来检查“square”是“X”还是“O”

#include <stdio.h>

int main(){
    int p1,p2;
    char arr[] = {'1','2','3','4','5','6','7','8','9'};
    void drawboard(){
        printf("%c|%c|%c\n",arr[0],arr[1],arr[2]);
        printf("-----\n");
        printf("%c|%c|%c\n",arr[3],arr[4],arr[5]);
        printf("-----\n");
        printf("%c|%c|%c\n",arr[6],arr[7],arr[8]);
    }
    while(1>0){
        drawboard();
        printf("Player 1, enter your choice:\n");
        scanf("%d",&p1);
        printf("Player 2, enter your choice:\n");
        scanf("%d",&p2);
        arr[p1-1] = 'X';
        arr[p2-1] = 'O';
        if(arr[0]&&arr[1]&&arr[2]=='X'){
            printf("Player 1 won.\n");
            return 1;

        }
        if(arr[3]&&arr[4]&&arr[5]=='X'){
            printf("Player 1 won.\n");
            return 1;
        }
        if(arr[6]&&arr[7]&&arr[8]=='X'){
            printf("Player 1 won.\n");
            return 1;
        }
        if(arr[0]&&arr[3]&&arr[6]=='X'){
            printf("Player 1 won.\n");
            return 1;
        }
        if(arr[1]&&arr[4]&&arr[7]=='X'){
            printf("Player 1 won.\n");
            return 1;
        }
        if(arr[2]&&arr[5]&&arr[8]=='X'){
            printf("Player 1 won.\n");
            return 1;
        }
        if(arr[0]&&arr[4]&&arr[8]=='X'){
            printf("Player 1 won.\n");
            return 1;
        }
        if(arr[2]&&arr[4]&&arr[6]=='X'){
            printf("Player 1 won.\n");
            return 1;
        }
        else if(arr[0]&&arr[1]&&arr[2]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else if(arr[3]&&arr[4]&&arr[5]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else if(arr[6]&&arr[7]&&arr[8]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else if(arr[0]&&arr[3]&&arr[6]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else if(arr[1]&&arr[4]&&arr[7]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else if(arr[2]&&arr[5]&&arr[8]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else if(arr[0]&&arr[4]&&arr[8]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else if(arr[2]&&arr[4]&&arr[6]=='O'){
            printf("Player 2 won.\n");
            return 1;
        }
        else{
            continue;
        }

    }
    return 0;
}

在第一个和第二个玩家的输入之后,如果输入是“8”或“6”,则它说他赢了,即使只有一个空格被占用。

2jcobegt

2jcobegt1#

您写道:

arr[6]&&arr[7]&&arr[8]=='X'

这意味着:
如果arr[6]为真,且arr[7]为真,且arr[8]=='X'为真。
True表示除'\0'之外的任何字符,因此所有方块都为True,并且只要arr[8]为X,此条件就为True。
您的意思是:

arr[6]=='X' && arr[7]=='X' && arr[8]=='X'

(空格是可选的),它检查所有3个都是X。您必须以这种方式修复所有if语句。

s3fp2yjn

s3fp2yjn2#

前面有我的评论。一些问题...
1.不要使用 * 嵌套 * 函数
1.将arr置于全局范围。

  1. if语法不正确。(例如)if(arr[0]&&arr[1]&&arr[2]=='X')应该是if ((arr[0] == 'X') && (arr[1] == 'X') && (arr[2] == 'X'))
    1.不检查非法移动或超出范围的移动。
    1.未检查所有可能的获胜位置(例如,缺少对角线)。
    1.不需要对每个玩家进行单独的获胜检查。[对 * 任一 * 玩家]的获胜是在给定的方向比赛中的所有三个。
    1.该程序可以通过分解成更小的函数来大大简化。
    下面是正确的代码。它是注解。
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

char arr[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

void
drawboard(void)
{
    printf("%c|%c|%c\n", arr[0], arr[1], arr[2]);
    printf("-----\n");
    printf("%c|%c|%c\n", arr[3], arr[4], arr[5]);
    printf("-----\n");
    printf("%c|%c|%c\n", arr[6], arr[7], arr[8]);
}

// match3 -- decide if win
// RETURNS: winning char or zero
int
match3(int a,int b,int c)
{
    int x;

    --a;
    --b;
    --c;

    x = arr[a];

    return ((arr[b] == x) && (arr[c] == x)) ? x : 0;
}

// check -- check for win
int
check(void)
{
    int win;

    do {
        // top row
        win = match3(1,2,3);
        if (win)
            break;

        // middle row
        win = match3(4,5,6);
        if (win)
            break;

        // bottom row
        win = match3(7,8,9);
        if (win)
            break;

        // left column
        win = match3(1,4,7);
        if (win)
            break;

        // middle column
        win = match3(2,5,8);
        if (win)
            break;

        // right column
        win = match3(3,6,9);
        if (win)
            break;

        // left to right diagonal
        win = match3(1,5,9);
        if (win)
            break;

        // right to left diagonal
        win = match3(3,5,7);
        if (win)
            break;
    } while (0);

    switch (win) {
    case 'X':
        printf("Player 1 won.\n");
        break;
    case 'O':
        printf("Player 2 won.\n");
        break;
    }

    if (win)
        drawboard();

    return win;
}

// legal -- check for remaining legal moves
void
legal(void)
{
    int more;

    // look for remaining legal moves
    more = 0;
    for (int idx = 0;  idx < 9;  ++idx) {
        more = (arr[idx] != 'X') && (arr[idx] != 'O');
        if (more)
            break;
    }

    if (! more) {
        printf("No more legal moves -- no winner\n");
        drawboard();
        exit(0);
    }
}

// ask -- ask player for moves
int
ask(int playno,int x)
{
    char buf[100];
    char *cp;
    int move;

    printf("\n");
    legal();

    while (1) {
        drawboard();

        printf("Player %d, enter your choice:\n",playno);
        if (fgets(buf,sizeof(buf),stdin) == NULL) {
            printf("EOF\n");
            exit(0);
        }

        // echo for debug input from file
        do {
            struct termios tio;
            if (tcgetattr(fileno(stdin),&tio) < 0)
                fputs(buf,stdout);
        } while (0);

        // get the move
        move = strtol(buf,&cp,10);

        if (*cp != '\n') {
            printf("syntax error\n");
            continue;
        }

        // move is within bounds
        if ((move >= 1) && (move <= 9)) {
            --move;

            // check for move to an already occupied space
            if ((arr[move] == 'X') || (arr[move] == 'O')) {
                printf("already has an '%c'\n",arr[move]);
                continue;
            }

            // make the move
            arr[move] = x;
            break;
        }

        printf("move out of range\n");
    }

    return check();
}

int
main(void)
{
    int more;

    while (1) {
        if (ask(1,'X'))
            break;
        if (ask(2,'O'))
            break;
    }

    return 0;
}

下面是我用来测试程序的一些示例游戏...
第一个

相关问题