我正在尝试为井字游戏编写代码

时间:2019-07-13 16:54:40

标签: c

我正在尝试创建一个在板上输入x和o的函数,该函数已经具有1-9的计数器。有人可以向我解释算法和逻辑,以便我尝试进行相应的编码 如果有人可以指导我完成操作,那么我将通过不同的示例,但没有专门针对较短功能的示例

//function to initialize board and my main and last is the function with printf commands


     #include <stdio.h>
#define MAX 3
void InitializeBoard(int m, int n , char board[][n]){
    int c =1;
    for(int i =0; i<m; i++){
        for(int j=0; j< n; j++){
            board[i][j] = c+'0';
            c++;

        }

    }
    return;
}

void PrintBoard(int m, int n, char board[][n]){


    printf("\n %c | %c  | %c  \n", board[0][0],
           board[0][1], board[0][2]);
    printf("\n--------------\n");
    printf("\n  %c | %c  | %c  \n", board[1][0],
           board[1][1], board[1][2]);
    printf("\n--------------\n");
    printf("\n %c | %c  | %c  \n\n", board[2][0],
           board[2][1], board[2][2]);
    return;
    }
int main() {
    printf("Let's begin the game ! \n " );

    char choice;
    int m , n ;
    char board [MAX][MAX] ;



    do {
        printf("Press 'p' to print the tic-tac-toe board.\n");
        printf("Press 'c' to create the tic-tac-toe board with some X and O celss.\n");
        printf("Press 't' to test is the tic-tac-toe board is valid or invalid.\n");
        printf("Press 'w' to predict winning cell for the player X and O.\n\n");
        printf("Press 'e' to exit\n");

        scanf(" %c", &choice);

        switch(choice){
            case 'p' :

                InitializeBoard(m,n,board);
                PrintBoard(m,n, board);
                break;
            case 'c' :


            default:
                choice =-1;
        }
    }while(choice!=-1);

    return 0;
}

我希望输出

        1|2|3
        -----
        4|5|6
        -----
        7|8|9

1 个答案:

答案 0 :(得分:1)

问题是您要用来填充面板的未初始化变量。

int m , n ;
......

InitializeBoard(m,n,board);
PrintBoard(m,n, board);

您需要将它们初始化为行数和列数。

int m = MAX  , n = MAX;