我一直在用C语言开发一个项目,用来实现非洲棋类游戏Oware,我正在努力实现捕获功能,因为种子所在的坑没有像应该的那样被清空.我的实现如下所示
#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 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 capture(struct gameState *game, int player, int pitToSow){
int currentPit = (pitToSow + 1) % num_pits_player; //variable for the current pit
int seedsInCurrentPit = game->pits[(player + currentPit / num_pits_player) % num_players][currentPit % num_pits_player]; //getting the seeds in the current pit
int opponent = (player + 1) % num_players;
if(seedsInCurrentPit == 2 || seedsInCurrentPit == 3) { //checking for conditions to sow
game->deposit[player] += seedsInCurrentPit; //adding to deposit
game->pits[opponent][seedsInCurrentPit] = 0; //dont work
printf("player %d sowed %d seeds from \nplayer %d's pit number %d", player, seedsInCurrentPit, opponent, game->pits[opponent][seedsInCurrentPit]);
//check suplementary rules
}
}
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) { //validating if the pit is valid
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];
int lastPitSown = -1; // Initialize the variable to track the last pit sown
int lastPlayer = player; // Initialize the variable to track the player of the last pit sown
//zero out the pit
game->pits[player][pitToSow] = 0;
for(int currentPit = pitToSow + 1; seedsToSow > 0; currentPit++, seedsToSow--) {
// Distribute one seed to the current pit - moving mechanic
game->pits[(player + currentPit / num_pits_player) % num_players][currentPit % num_pits_player]++;
lastPitSown = currentPit % num_pits_player; // Update the last pit sown
lastPlayer = (player + currentPit / num_pits_player) % num_players; // Update the player of the last pit sown
}
if(lastPitSown >= 0 && lastPitSown < num_pits_player){ //if the last pit sown is in the scope of the array
capture(game, player, pitToSow); //call capture function
}
//update turns
game->playerTurn = (game->playerTurn + 1) % num_players;
}
void startGame(struct gameState *game){
for(int player=0; player < num_players; player++){ //rotating the players
for(int pit=0; pit < num_pits_player; pit++){ //populating each player's pits
game->pits[player][pit] = num_seeds;
}
game->deposit[player] = 0; //init of deposits/score
}
game->playerTurn=0;
}
int main() {
struct gameState game;
//picking the game mode to play
//game start - populate each pit w/ 4, deposit w/ 0
startGame(&game);
//display game
displayGame(&game);
while(game.deposit[0] <= 25 || game.deposit[1] <= 25){
makeMove(&game);
displayGame(&game);
}
return 0;
}
我try 使用game->pits[opponent][seedsInCurrentPit] = 0;
,但即使我相信索引是正确的,代码也不能像预期的那样运行.我还添加了printf("player %d sowed %d seeds from \nplayer %d's pit number %d", player, seedsInCurrentPit, opponent, game->pits[opponent][seedsInCurrentPit]);
作为调试语句,它似乎返回得很好,但是我需要清零的值(存放SeedsInCurrentPit的PIT)却不是这样.
最后,我试过使用game->pits[(player + currentPit / num_pits_player) % num_players][currentPit % num_pits_player] = 0;
,但没有用.
An example for the return I get when I run my current code implementation is
As can be seen, the debug statement returns just fine, as the pit to be emptied should be the first of player 1
(pit 0), and the correct number of seeds gets added to player 0's deposit
, however the pit where the seeds are taken from isn't emptied as it should, and behaves like a normal pit.
For this scenario, I've fed the following input:
player 0: 3
player 1: 1
player 0: 4
player 1: 3
player 0: 2
Right here, capture happens
Furthermore, I've noticed that player 1 does not capture any seeds from player 0, even if conditions are met, as shown in the image below
这本应促使玩家1捕获2个种子,但它没有.