如何将字符放在字符数组中的特定点?

时间:2013-06-08 09:18:25

标签: c

我正在做一个简单的tic tac toe游戏。我遇到了存储用户选择的问题。 我基本上有一个名为userBoard的结构,它有一个大小为12的字符数组。将向用户显示一个每个位置编号的板。然后,用户必须选择一个位置来放置他们的角色。用户的角色(X或O)将被随机分配给他们。当用户键入数字时,它将传递给名为updateUserBoard的函数。然后updateUserBoard将用户的角色放到用户选择的任何位置。我现在遇到的问题是,它不是仅将用户字符放在数组的一个位置,而是用用户字符填充整个数组。代码如下。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printBoard(void);
void updateUserBoard(int location, char userCharacter, char computerCharacter);

struct UserTicTacToeBoard {
    char user[12]; // A user character array to store the users character.
};

struct ComputerTicTacToeBoard {
    char comp[12];
};

typedef struct UserTicTacToeBoard UserTicTacToeBoard;
typedef struct ComputerTicTacToeBoard ComputerTicTacToeBoard;

UserTicTacToeBoard userBoard;
ComputerTicTacToeBoard compBoard;

int main(int argc, char const *argv[])
{
    printf("Welcome to a game of tic tac toe.\n");
    printf("Let's see if you're smarter than the computer.\n");
    char computerCharacter, userCharacter;
    /* Getting a random number needs some fixing! */
    int assignRandom = (rand() % 10000) / 10000.0;
    int userDecision;
    switch(assignRandom) {
        case 1:
        strcpy(&userCharacter, "O");
        strcpy(&computerCharacter, "X");
        break;

        default:
        strcpy(&userCharacter, "X");
        strcpy(&computerCharacter, "O");
        break;
    }

    printf("Computer gets %c\n", computerCharacter);
    printf("User gets %c\n", userCharacter);
    printBoard();

    for(int i = 0; i <= 12; i++) {
        userBoard.user[i] = computerCharacter;
        compBoard.comp[i] = userCharacter;
    }

    while(1) {
    printf("\nLadies first.\n");
    printf("Please enter a number from the table above which you would like to replace with\nNumber: ");
    // Use some other function here instead of scanf. If the user types anything other than an int,
    // scanf goes into this crazy loop.
    scanf("%d", &userDecision);
    updateUserBoard(userDecision, userCharacter, computerCharacter);
    }
    return 0;
}

void printBoard(void)
{
    printf(" 0  | 1  | 2  | 3  |\n");
    printf("--------------------\n");
    printf(" 4  | 5  | 6  | 7  |\n");
    printf("--------------------\n");
    printf(" 8  | 9  | 10 | 11 |\n");
}

void updateUserBoard(int location, char userCharacter, char computerCharacter)
{
    if (location > 11) {
        printf("ERROR: PUSSY DETECTED. GROW A PAIR.\n");
        exit(1);
    }

    userBoard.user[location] = userCharacter; // Here instead of putting the user's character to userBoard.user[location], it fills the whole array with the users location 

    printf(" %c  | %c  | %c  | %c  |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]);
    printf("------------------------\n");
    printf(" %c  | %c  | %c  | %c  |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]);
    printf("------------------------\n");
    printf(" %c  | %c  | %c  | %c  |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]);
    printf("------------------------\n");
    printf(" %c  | %c  | %c  | %c  |\n", userBoard.user[location], userBoard.user[location], userBoard.user[location], userBoard.user[location]);
}

2 个答案:

答案 0 :(得分:2)

输出代码中存在问题:printf对所有16个单元格使用userBoard.user[location](即您打印相同的字符16次)。

这种错误是人们通常用于“复制和粘贴编程”的错误。使用循环。

我想你应该打印12个细胞,而不是16个。

答案 1 :(得分:0)

将updateUserBoard函数的结尾更改为以下内容:

一些注释

  • For-loop使代码更短。其内容为12(=板宽*板高)次
  • 当绘制了一行中的所有单元格时,执行循环中的“if”块(迭代器i大于零,并且其行宽和模数为零(即,是时候绘制下一个)线))。这是行更改的地方。

代码本身:

for(int i = 0; i < 12; i++)
{
    printf("%c  |", userBoard.user[i]);
    if(i%4 == 0 && i > 0)
    {
        printf("\n------------------------\n");
    }
}