我正在开发一个棋类游戏Oware的C-rereation游戏项目,我需要两个数组来相互包装.

我有一个用于游戏的 struct 和几个全局变量,以及每个玩家的一个数组,该数组由 struct 中的全局变量定义,这使得每个玩家有六个坑可以玩.

#include <stdio.h>
#define num_players 2
#define num_pits_player 6
#define num_seeds 4

struct gameState{
    int pits[num_players][num_pits_player]; //pits for the seeds
    int deposit[num_players]; //deposit for the pits
    int playerTurn; //keeping track of whose turn it is
};

void displayGame(struct gameState *game){
    printf("\n");
    printf("|--------|--|--player1--|--|--------|\n"
           "|score_p1|%2d|%2d|%2d|%2d|%2d|%2d|score_p0|\n"
           "|  %2d    |-----------------|   %2d   |\n"
           "|        |%2d|%2d|%2d|%2d|%2d|%2d|        |\n"
           "|--------|--|--player0--|--|--------|\n",
           game->pits[1][0], game->pits[1][1], game->pits[1][2], game->pits[1][3], game->pits[1][4], game->pits[1][5],
           game->deposit[1], game->deposit[0],
           game->pits[0][0], game->pits[0][1], game->pits[0][2], game->pits[0][3], game->pits[0][4], game->pits[0][5]);
}

void makeMove(struct gameState *game){
    int player = game->playerTurn;
    int pitToSow;

    printf("Player %d's turn.\nPick a pit to sow(1-6): ", player);
    scanf("%d", &pitToSow);
    pitToSow--; // adjusting for zero-based indexing

    // Get the seeds from the selected pit
    int seedsToSow = game->pits[player][pitToSow];

    // Clear the selected pit
    game->pits[player][pitToSow] = 0;

    // Distribute seeds in a counterclockwise direction
    int currentPit = (pitToSow + 1) % num_pits_player;

    while (seedsToSow > 0) {
        // Distribute one seed to the current pit
        game->pits[player][currentPit]++;
        seedsToSow--;

        // Move to the next pit
        currentPit = (currentPit + 1);

    }
}

我面临的错误是,每当代码运行时,我都会收到

|--------|--|--player1--|--|--------| /*initial print*/
|score_p1| 4| 4| 4| 4| 4| 4|score_p0| 
|   0    |-----------------|    0   |
|        | 4| 4| 4| 4| 4| 4|        |
|--------|--|--player0--|--|--------|

Player 0's turn.
Pick a pit to sow(1-6):4
|--------|--|--player1--|--|--------| /*player 0's play*/
|score_p1| 5| 5| 4| 4| 4| 4|score_p0| /*works as expected*/
|   0    |-----------------|    0   |
|        | 4| 4| 4| 0| 5| 5|        |
|--------|--|--player0--|--|--------|
Player 1's turn.
Pick a pit to sow(1-6):5

|--------|--|--player1--|--|--------| /*player 1's play, the error seems to be*/
|score_p1| 5| 5| 4| 4| 0| 5|score_p0| /*that it doesnt wrap through */
|   1    |-----------------|    1   | /*to player 0's pits when finished with its own*/
|        | 4| 4| 4| 0| 5| 5|        | 
|--------|--|--player0--|--|--------| 

而不是理想

|--------|--|--player1--|--|--------| /*initial print*/
|score_p1| 4| 4| 4| 4| 4| 4|score_p0| 
|   0    |-----------------|    0   |
|        | 4| 4| 4| 4| 4| 4|        |
|--------|--|--player0--|--|--------|

Player 0's turn.
Pick a pit to sow(1-6):4
|--------|--|--player1--|--|--------| /*player 0's play*/
|score_p1| 5| 5| 4| 4| 4| 4|score_p0| /*works as expected*/
|   0    |-----------------|    0   |
|        | 4| 4| 4| 0| 5| 5|        |
|--------|--|--player0--|--|--------|
Player 1's turn.
Pick a pit to sow(1-6):5

|--------|--|--player1--|--|--------| /*optimal player 1 play,wrapping to player 0's pits when finished with its own*/
|score_p1| 5| 5| 4| 4| 0| 5|score_p0| 
|   1    |-----------------|    1   | 
|        | 5| 5| 5| 0| 5| 5|        | 
|--------|--|--player0--|--|--------| 

推荐答案

  1. 始终验证从scanf()开始的返回值,在本例中,返回值在预期范围内.我想知道6是不是num_pits_player?如果是,则使用该变量生成提示.

  2. currentPit = (currentPit + 1);应该是数组的模大小,否则会有未定义的行为.

  3. opponentPlayerIndex不能用于任何go 掉它的东西.

  4. (非固定)分数不会更新.

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

#define num_players 2
#define num_pits_player 6
#define num_seeds 4

struct gameState{
    int pits[num_players][num_pits_player]; //pits for the seeds
    int deposit[num_players]; //deposit for the pits
    int playerTurn; //keeping track of whose turn it is
};

void makeMove(struct gameState *game){
    int player = game->playerTurn;
    printf("Player %d's turn.\nPick a pit to sow(1-6): ", player);
    int pitToSow;
    if(scanf("%d", &pitToSow) != 1 || pitToSow < 1 || pitToSow > 6) {
        fprintf(stderr, "scanf failed\n");
        exit(1);
    }
    pitToSow--; // adjusting for zero-based indexing

    // Get the seeds from the selected pit
    int seedsToSow = game->pits[player][pitToSow];
 
    // Clear the selected pit
    game->pits[player][pitToSow] = 0;

    for(int currentPit = pitToSow + 1; seedsToSow > 0; currentPit++, seedsToSow--)
        // Distribute one seed to the current pit
        game->pits[(player + currentPit / num_pits_player) % num_players][currentPit % num_pits_player]++;
}

void displayGame(struct gameState *game){
    printf("\n");
    printf("|--------|--|--player1--|--|--------|\n"
           "|score_p1|%2d|%2d|%2d|%2d|%2d|%2d|score_p0|\n"
           "|  %2d    |-----------------|   %2d   |\n"
           "|        |%2d|%2d|%2d|%2d|%2d|%2d|        |\n"
           "|--------|--|--player0--|--|--------|\n",
           game->pits[1][0], game->pits[1][1], game->pits[1][2], game->pits[1][3], game->pits[1][4], game->pits[1][5],
           game->deposit[1], game->deposit[0],
           game->pits[0][0], game->pits[0][1], game->pits[0][2], game->pits[0][3], game->pits[0][4], game->pits[0][5]);
}

int main(void) {
    struct gameState s = {
        .pits = {
            {4, 4, 4, 4, 4, 4},
            {4, 4, 4, 4, 4, 4}
        }
    };
    for(int p = 0; p < 2; p++) {
        s.playerTurn = p;
        makeMove(&s);
        printGameState(&s);
    }
}

和示例输出:

Player 0's turn.
Pick a pit to sow(1-6): 4

|--------|--|--player1--|--|--------|
|score_p1| 5| 5| 4| 4| 4| 4|score_p0|
|   0    |-----------------|    0   |
|        | 4| 4| 4| 0| 5| 5|        |
|--------|--|--player0--|--|--------|
Player 1's turn.
Pick a pit to sow(1-6): 5

|--------|--|--player1--|--|--------|
|score_p1| 5| 5| 4| 4| 0| 5|score_p0|
|   0    |-----------------|    0   |
|        | 5| 5| 5| 0| 5| 5|        |
|--------|--|--player0--|--|--------|

C++相关问答推荐

为什么海湾合作委员会在共享对象中的. init_data的虚拟内存地址之前留出一个空白

C strlen on char array

通过MQTT/蚊子发送大文件—限制在4MB

Mise()在虚拟内存中做什么?

无法用C++编译我的单元测试

fwrite无法写入满(非常大)缓冲区

在CLANG中调试预处理器宏

Vcpkg的配置文件

为什么我的Hello World EFI程序构建不正确?

FRIDA-服务器成为端口扫描的目标?

如何编写一个for循环来计算C中各项的总和?

理解bzip2的BZ2_解压缩函数中的状态重新分配

使用Open62541向OPCUA服务器发送读请求时内存泄漏

如何将大写/小写土耳其字母相互转换?

用C++高效解析HTTP请求的方法

问题:C#Define上的初始值设定项元素不是常量

与指针的原始C数组或C++向量<;向量<;双>>;

我正在使用 klib 库 我可以使用 (khash) KHASH_SET_INIT_INT64() 负值作为键.因为我在头文件中看到它使用 unsigned long int

'printf("%s", user_input)' 危险吗?

初始化动态分配的布尔二维数组的最佳方法是什么?