簡體   English   中英

我正在 c 中制作井字游戲,wincheck function 但在下面的代碼中不起作用

[英]i am making a tic tac toe game in c, the wincheck function but in the code below is not working

I am making a tic tac toe game in c, the wincheck function in the code below is not working, if there is a win, the game doesn't stop, it keeps running seems that the wincheck function doesn't make the wincheckvalue 1 ,但為什么?

#include <stdio.h>
#include <stdbool.h>
char list[10] = {'0',' ',' ',' ',' ',' ',' ',' ',' ',' '};

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

int wincheck(char c)
{
    int returnvalue = 0;
    if (list[1] == list[2] == c && list[2] == list[3] == c)
    {
        returnvalue = 1;
    }
    else if (list[4] == list[5] == c && list[5] == list[6] == c)
        returnvalue = 1;

    else if (list[7] == list[8] == c && list[8] == list[9] == c)
        returnvalue = 1;

    else if (list[1] == list[4] == c && list[4] == list[7] == c)
        returnvalue = 1;

    else if (list[2] == list[5] == c && list[5] == list[8] == c)
        returnvalue = 1;

    else if (list[3] == list[6] == c && list[6] == list[9] == c)
        returnvalue = 1;

    else if (list[1] == list[5] == c && list[5] == list[9] == c)
        returnvalue = 1;

    else if (list[3] == list[5] == c && list[5] == list[7] == c)
        returnvalue = 1;
    return returnvalue;

}


int main()
{
    int wincheckvalue;
    int v = 0;
    int a;
    int b;
    while (true)
    {       
        displayboard();
        printf("Player 1(X) ,Input num: ");
        scanf("%i",&a);
        while (list[a] == '0' || list[a] == 'X' )
        {
            displayboard();
            printf("Invalid move!\n");
            printf("Input num: ");
            scanf("%i",&a);
        }
        list[a] = 'X';
        wincheckvalue = wincheck('X');
        if (wincheckvalue == 1)
        {
            printf("Player 1 has won!!");
            break;
        }
        displayboard();
        printf("Player 2(0) ,Input num: ");
        scanf("%i",&b);
        while (list[b] == 'X' || list[b] == '0')
        {
            displayboard();
            printf("Invalid move!\n");
            printf("Input num: ");
            scanf("%i",&b);
        }
        list[b] = '0';
        displayboard();
        wincheckvalue = wincheck('0');
        if (wincheckvalue == 1)
        {
            printf("Player 2 has won!!");
            break;
        }

    }


}

你所有的比較像list[1] == list[2] == c都是錯誤的。

該比較等於(list[1] == list[2]) == c ,這意味着您將 list[1] == list[1] == list[2] [2] 的真/假(分別為整數10 )結果與c的值。 這很可能永遠不會是真的。

您必須像list[1] == c && list[2] == c等那樣拆分比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM