我目前正在尝试学习如何用 C 语言编写代码,因此我决定创建一个项目来跟踪扑克游戏期间的投注。 我声明了我的结构 Player 如下:
typedef struct Player Player;
struct Player
{
char Role[11];
char Name[20];
char State[7];
int Money;
int Bet;
};
我仅出于优化目的而特意使用静态表。
void Register_Player_Finalisation(char *Name, int Money, char *Role_g, char *State_g, Player *Player_created)
{
/**/
strcpy(Player_created->Name, Name);
strcpy(Player_created->Role, Role_g);
strcpy(Player_created->State, State_g);
Player_created->Money = Money;
Player_created->Bet = 0;
}
我使用副作用,因为这样做看起来更“干净”
我的问题来了:
int Register_Player(Player *Record_Of_Payers)
{
int Players_active = 1;
char Name[20];
scanf("%s\n", Name);
char Role[20] = "Not_Yet";
char State[8] ="Not_Yet";
int Money = 2000;
Player Player_created;
Register_PLayer_Finalisation(Name, Money, Role,State, &Player_created);
Record[0] = Player_created;
/* Boucle pour ajouter des joueurs */
while (Players_active < 6 )
{
scanf("%s\n", Name);
if (*Name != '7')
{
Players_active++;
/* Here is the issue */
Player New_Player;
/*------------------*/
Register_Player_Finalisation(Name, Money, Role,State, &New_Player);
Record[Players_active - 1] = New_Player;
}
else
{
printf("Exception \n");
break;
}
}
return Players_active;
}
所以我想在每次循环时创建一个新的 Player 实例。我可以执行 malloc 然后释放它,但我想避免使用动态表。 我还有一个小问题,当我们给出 7 作为参数时,它不会立即停止,而是需要另一个字符(无论是哪个)来打破。
它现在正在工作,但我觉得它不应该。