学习C - 需要帮助完成我的tic-tac-toe计划

时间:2012-05-07 01:45:47

标签: c tic-tac-toe

我一直在尝试学习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;
}

感谢阅读。

1 个答案:

答案 0 :(得分: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;

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