#include #include #include #define CARDS 52 /* 難しいのでジョーカー抜き */ typedef struct { /* 1枚のカードの構造体 */ char *suit; /* スート */ int number; /* 数字 */ char *name; /* 読み名 */ } aCard; typedef struct { /* 山、手札などカードの集合体 */ aCard card[CARDS]; /* 単一カードが52枚 */ int n; /* 今何枚あるか */ } Cards; char *hand[] = { /* 役の名前の配列 */ "ハイカード", "ワンペア", "ツーペア", "スリーカード", "ストレート", "フラッシュ", "フルハウス", "フォーカード", "ストレートフラッシュ", "ロイヤルストレートフラッシュ", }; void initialize(Cards *d) /* Cards構造体のポインタ */ { static char *suits[] = { "ハート", "スペード", "ダイヤ", "クラブ", "ジョーカー" }; static char *numbers[] = { "JOKER", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; /* ここまでは initcard2 と同じ*/ int s, n, i; for (i=0; icard がaCard構造体の配列メンバになる。 d->cardは配列なのでd->card[x] でアクセスする。 */ d->card[i].suit=suits[s]; d->card[i].number = n; d->card[i].name = numbers[n]; } d->n = 52; /* この山が持っている枚数をセット */ } void shuffle(Cards *d) { int i, pair; int length=d->n; aCard work; srandom(time(NULL)); for (i=0; icard[i], sizeof work); memcpy(&d->card[i], &d->card[pair], sizeof work); memcpy(&d->card[pair], &work, sizeof work); } } void dispcard(aCard c) { printf("%sの%s\n", c.suit, c.name); } int is_royalStraightFlush(Cards *c) { return 0; } int is_straightFlush(Cards *c) { return 0; } int is_fourCards(Cards *c) { return 0; } int is_fullHouse(Cards *c) { return 0; } int is_flush(Cards *c) { return 0; } int is_straight(Cards *c) { return 0; } int is_threeCards(Cards *c) { return 0; } int is_twoPairs(Cards *c) { return 0; } int is_onePair(Cards *c) { int box[14]; /* 該当番号が何個あるかの配列 */ int i, j; int sum=0; /* 1枚以上のカードが何種類? */ for (i=1; i<=13; i++) {/* 全て0に初期化 box[0] は使わない */ box[i] = 0; } for (j=0; jn; j++) { /* Count each number */ /* c->card[j] が手持の5枚目のカードの構造体 */ /* c->card[j].number は、その数字 */ box[c->card[j].number]++; } for (i=1; i<=13; i++) { if (box[i]>0) sum++; } printf("sum=%d\n", sum); if (sum == 4) return 1; return 0; } int judge(Cards *c) { if (is_royalStraightFlush(c)) { printf("ロイヤルストレートフラッシュです\n"); } else if (is_straightFlush(c)) { printf("ストレートフラッシュです\n"); } else if (is_fourCards(c)) { printf("フォーカードです\n"); } else if (is_fullHouse(c)) { printf("フルハウスです\n"); } else if (is_flush(c)) { printf("フラッシュです\n"); } else if (is_straight(c)) { printf("ストレートです\n"); } else if (is_threeCards(c)) { printf("スリーカードです\n"); } else if (is_twoPairs(c)) { printf("ツーペアです\n"); } else if (is_onePair(c)) { printf("ワンペアです\n"); } else { printf("ハイカードです\n"); } } int main(int argc, char *argv[]) { Cards deck, human, com; int i; initialize(&deck); shuffle(&deck); /* 人間に5枚配る */ for (human.n=0; human.n<5; human.n++) { memcpy(&human.card[human.n], &deck.card[--deck.n], sizeof (aCard)); } puts("あなたの手は"); for (i=0; i