-1

我正在制作一个简单的井字游戏。我在存储用户的选择时遇到了问题。我基本上有一个名为 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]);
}
4

2 回答 2

2

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

这种错误是“复制和粘贴编程”通常会出现的错误。使用循环。

我想你应该打印 12 个单元格,而不是 16 个。

于 2013-06-08T09:22:17.417 回答
0

Change the ending of the updateUserBoard function to something like this:

Some notes

  • For-loop makes the code shorter. Its contents are run 12 (=board width * board height) times
  • The "if" block in the loop is executed when all the cells in one line have been drawn (iterator i is greater than zero and its and row width's modulus are zero (i.e. it's time to draw the next line)). This is where the row changes.

The code itself:

for(int i = 0; i < 12; i++)
{
    printf("%c  |", userBoard.user[i]);
    if(i%4 == 0 && i > 0)
    {
        printf("\n------------------------\n");
    }
}
于 2013-06-08T10:30:51.680 回答