目前正试图找到一种方法来为Yahtzee游戏定义C中的一组胜利条件。我也想尝试使用结构来做到这一点。值数组是传递给此函数的指针。我计划检查name.howget
是否与实际值匹配,然后允许用户选择他/她可用的那些。但我不知道我编码它的方式是否有效。如何在一个结构中获得Full House的所有组合而不必全部输入?
int win_or_lose(int *array_ptr, int sum_of_dice)
{
struct Win_Conditions{
char name[7];
int used;
int score;
int howget;
};
//THREE OF A KIND STRUCT
struct Win_Conditions ThreeKind;
strcpy(ThreeKind.name, "Three of a Kind");
ThreeKind.used, false;
ThreeKind.score, sum_of_dice;
//second set of values is set to four to that is can accept a four of a kind as a three of a kind.
ThreeKind.howget, 1,1,1 || 2,2,2 || 3,3,3 || 4,4,4 || 5,5,5 || 6,6,6 || 1,1,1,1 || 2,2,2,2 || 3,3,3,3 || 4,4,4,4 || 5,5,5,5 || 6,6,6,6;
//FOUR OF A KIND STRUCT
struct Win_Conditions FourKind;
strcpy(FourKind.name, "Four of a Kind");
FourKind.used, false;
FourKind.score, sum_of_dice;
FourKind.howget, (1,1,1,1) || (2,2,2,2) || (3,3,3,3) || (4,4,4,4) || (5,5,5,5) || (6,6,6,6);
struct Win_Conditions FullHouse;
strcpy(FullHouse.name, "Full House");
FullHouse.used, false;
FullHouse.score, 25;
FullHouse.howget, (1,1 && 1,1,1 )|| (1,1 && 2,2,2) || (1,1 && 3,3,3) || (1,1 && 4,4,4) || (1,1 && 5,5,5) || (1,1 && 6,6,6) ||
(2,2 && 1,1,1 )|| (2,2 && 2,2,2) || (2,2 && 3,3,3) || (2,2 && 4,4,4) || (2,2 && 5,5,5) || (2,2 && 6,6,6) ..... didn't want to input every combo
}
答案 0 :(得分:0)
尝试:
//Assumes diceArray is sorted. Seriously, learn qsort from <stdlib>
int isThreeKind(int diceArray[])
{
int i;
for(i=0; i<NUM_DICE-2; i++)
{
if(diceArray[i] == diceArray[i+1] && diceArray[i] == diceArray[i+2])
{
return TRUE;
}
}
return FALSE;
}
然后在win_or_lose
中你打电话
if(isThreeKind(diceArray) == TRUE)
{
score += sum_of_dice; //or however yatzhee is played
}
如果你真的想将这类东西组合成模块,你会看到面向对象的编程,你会想要尝试C ++或Java或C#。
使用虚拟函数howget可以创建一个类,子类可以全部定义自己的howget版本。哪种看起来像你计划如何做到这一点。但对于有人出发来说,这可能会有点沉重。
但我认为你应该从关于C
语法的教程开始。每个人都从某处开始。那些逗号......