c - 打印数组时如何显示不同的字符? (C语言)

标签 c game-engine

现在我的数组打印如下:

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

我想为空,但不确定如何将其从我的代码中提取出来。我希望董事会看起来像这样:

    |   |  
 -----------
    |   | 
 -----------
    |   | 

不太确定为什么我无法弄清楚如何让它发挥作用。有任何快速帮助吗?

最佳答案

在最初的情况下,您正在打印索引,您不必这样做并且您的 sizeof 不起作用...所以它变成:-

void displayBoard(char board[]){
for(int i=0;i<9;i++){
    printf(" %c ",board[i]);
    if(i != 2 && i != 5 && i != 8) printf("|");
    if(i == 2 || i == 5) printf("\n------------\n");
}
printf("\n");
}

你原来的sizeof(board)等于4

因为它是函数的一个参数,而且是一个指针。

奖励答案:提供键盘映射

int keyboard_mapping[9] = {6,7,8,3,4,5,0,1,2};
int from_entry(char* s)
{
    int v = atoi(s);
    if(v < 1 || v > 9) return 0; // we have a problem...not handled
    return keyboard_mapping[v-1];
}

然后是这样的:-

board[atoi(move)] = 'X';

成为

board[from_entry(move)] = 'X';

更多奖励:

将 first 设置为 1 或 2,具体取决于您希望玩家先走还是后走。

char move[] = "";
    int turn;
    int first;

    //TODO ask the user whether to do go first or second
    printf("Tic-Tac-Toe\nCreated by \nYou are first! What's your move going to be?\n");
    while(checkForWin(board) == ' ' && boardFull(board) == 0){
        printf("\n");
        displayBoard(board);
        for(turn=1; turn <= 2; turn++;)
        {
            if(turn == first)
            {
                printf("\nSelection a position to place your piece: ");
                scanf("%s",move);
                if(board[atoi(move)] == ' ')
                {
                    board[atoi(move)] = 'X';
                }               
            }
            else
            {
                int compmove = chooseMove(board, 1).move;
                board[compmove] = 'O';
            }        
        }
        else {
            printf("\n----------------------------------------\n\nPlease choose another location.\nThis one has already been selected.\n\n----------------------------------------\n\n");
        }
    }

关于c - 打印数组时如何显示不同的字符? (C语言),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20508408/

相关文章:

C - 编译器允许的语法无效?

c - 如何获取我的代码的运行时间?

c# - unity GetComponent<Type> 返回值

c++ - DirectX 字体不绘制?

c - 关于 glib c setmntent 函数的查询

C将未知大小的数组传递给单个变量中的函数

ios - iOS 中基于 map 的游戏的最佳 2D/3D 游戏引擎

java - 如何处理多点触控?

c - 在c中打开并写入文本

c++ - C++中的简单事件系统