1

我一直在尝试学习 C。来自具有 HTML、CSS 和在 Wordpress 中创建自定义主题经验的网页设计背景,我想尝试一下编程。我有一个朋友帮助我,他对我的最新任务是井字游戏。以下是我到目前为止的代码。我目前无法在玩家 1 和 2 之间进行游戏循环,并且无法让游戏识别游戏是 0 不完整、1 p1 获胜、2 p2 获胜或 3 只猫游戏。

#include <stdio.h>
#define O 2
#define X 1

void printItem(int item)
{
    char items[] = { ' ', 'X', 'O' };
    printf(" %c ", items[item]);
 /*   
    if (item == 0)
        printf(" ");
    else if (item == 1)
        printf("X");
    else
        printf("O");
  */
}

void printRowDivider()
{
    printf("  +---+---+---+\n");
}

void printBoard(int board[9]) 
{
    printf("    A   B   C\n");
    printRowDivider();
    printf("1 ");
    for (int i = 0; i < 9; i++)
    {
        printf("|");
        printItem(board[i]);
        if (i == 2 || i == 5)
        {
            printf("|\n");
            printRowDivider();
            printf("%d ", (i+1)/3 + 1);
        }
    }
    printf("|\n");
    printRowDivider();
}

int gameStatus(int board[9]) {

    // check rows
    if (board[0] == board[1] && board[1] == board[2] && board[0] != 0)
        return board[0];
    else if (board[3] == board[4] && board[4] == board[5] && board[3] != 0)
        return board[3];
    else if (board[6] == board[7] && board[7] == board[8] && board[6] != 0)
        return board[6];

    // check columns
    else if (board[0] == board[3] && board[3] == board[6] && board[0] != 0)
        return board[0];
    else if (board[1] == board[4] && board[4] == board[7] && board[1] != 0)
        return board[1];
    else if (board[2] == board[5] && board[5] == board[9] && board[2] != 0)
        return board[2];

    // check diagnols 
    else if (board[0] == board[4] && board[4] == board[8] && board[0] != 0)
        return board[0];
    else if (board[2] == board[4] && board[4] == board[6] && board[2] != 0)
        return board[2];

    else {
        return 3;
    }

}

int main()
{
    int gameBoard[9] = { 0, 0, 0, 
                         0, 0, 0, 
                         0, 0, 0 };

    printBoard(gameBoard);          

        printf("p1 (Column + Row): ");
        int row;
        char column;                

        scanf(" %c%d", &column, &row);
        printf("You said %d, %d.\n", row, column);

        row -= 1;
        column -= 'A';

        printf("You said %d, %d.\n", row, column);
        gameBoard[row*3 + column] = X;

        printBoard(gameBoard);
        printf("p2 (Column + Row): ");
        scanf(" %c%d", &column, &row);
        row -= 1;
        column -= 'A';

        printf("You said %d, %d.\n", row, column);
        gameBoard[row*3 + column] = O;

        printBoard(gameBoard);

        int player = 0; 

        while (1) { 
            printBoard(gameBoard);

            printf("Player %d: ", player + 1); 
            scanf(" %c%d", &column, &row ); 
            player -= player;

        }

    printBoard(gameBoard);

    return 0;
}

谢谢阅读。

4

1 回答 1

1

在您的主程序中,您的循环是:

int player = 0; 

while (1)
{ 
    printBoard(gameBoard);
    printf("Player %d: ", player + 1); 
    scanf(" %c%d", &column, &row ); 
    player -= player;
}

这会将播放器设置为 0,然后再次设置为 0(因为 0 - 0 为 0),等等。如果您需要在 0 和 1 之间波动,有几种方法可以做到这一点,至少包括:

player = !player;
player = 1 - player;

您还需要将此循环上方的大部分移动代码放在循环内调用的函数中。

于 2012-05-07T02:17:58.677 回答