我正在尝试创建拉米纸牌游戏的简化版本。我需要解析卡片缩写,例如 SA 是 Spades Ace。 DT 是 Diamond 10 等。我知道有一种更简单的方法可以做到这一点,但这就是我的任务想要完成的方式。
示例执行看起来像
rummy 3 S2 H9 C4...等包括全部52张牌
argv[1]中的数字是游戏中的玩家。我应该如何将卡片从数字后面开始并将它们放入数组中?
到目前为止我的代码
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int players = *argv[1];
char deck[52];
int i, j, pid, cid;
if (players > '5' || players < '3')
{
printf("%c is not the allowed number of players, min is 3 and max is 5\n",*argv[1]);
exit(0);
}
}
最佳答案
快速而肮脏的演示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int players = atoi(argv[1]);
char deck[52][3];
int i, j, pid, cid;
if (players > 5 || players < 3)
{
printf("%d is not the allowed number of players, min is 3 and max is 5\n", players);
exit(0);
}
for (i = 0; i < argc - 2; i++)
{
strcpy(deck[i], argv[i+2]);
}
for (i = 0; i < argc - 2; i++)
{
printf("%s\n", deck[i]);
}
}
绝对不会对输入进行健全性检查。这只是为了演示。
关于c - 将参数解析为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35596448/