在C中遍历两个数组

ijxebb2r  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(75)

我正在用C开发一个游戏项目-棋盘游戏Oware的重建,我需要两个数组相互 Package 。
我有一个游戏的结构体和一些全局变量,每个玩家有一个数组,用结构体中的全局变量定义,这使得每个玩家有六个坑。

#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--|--|--------|

zdwk9cvp

zdwk9cvp1#

1.总是验证scanf()的返回值,在这种情况下,该值在预期范围内。我想知道6是否是num_pits_player?如果是,请使用该变量生成提示符。

  1. currentPit = (currentPit + 1);应该是数组的模大小,否则你将有未定义的行为。
  2. opponentPlayerIndex不用于任何东西,因此将其删除。
    1.(不固定)分数不更新。
#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--|--|--------|

相关问题