作者enthos (影斯)
看板C_and_CPP
標題Re: [問題] 撲克牌牌組判斷
時間Thu May 20 22:59:37 2010
※ 引述《jason06184 (講座)》之銘言:
: 簡單來說我跑出來要是像這樣的東西
: 4 of Hearts
: 7 of Diamonds
: 4 of Diamonds
: 7 of Clubs
: 7 of Spades
: 這副牌為7葫蘆
// 32-bit, linux
#include <stdio.h>
#include <stdlib.h>
/* initialize suit array */
const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
/* initialize face array */
const char *face[ 13 ] =
{ "ACE", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King" };
// card is 0 ... 51
void show_card(int card)
{
if (card < 0 || card > 51)
perror("Error.");
printf("%s of %s.\n", face[card % 13], suit[card / 13]);
}
void check_fullhouse(int a, int b, int c, int d, int e)
{
int *tmp;
int i;
int flag_2 = 0; int flag_3 = 0;
int fullhouse_card = 0;
tmp = calloc(14, sizeof(int));
tmp[a % 13]++;
tmp[b % 13]++;
tmp[c % 13]++;
tmp[d % 13]++;
tmp[e % 13]++;
for(i = 0; i <= 12; i++) {
if (tmp[i] == 2)
flag_2++;
if (tmp[i] == 3) {
flag_3++;
fullhouse_card = i;
}
}
if (flag_2 == 1 && flag_3 == 1)
printf("這副牌為%s葫蘆\n", face[fullhouse_card]);
else
printf("Bad cards.\n");
free(tmp);
}
int main()
{
show_card(3); show_card(6 + 13); show_card(3 + 13);
show_card(6 + 26); show_card(6 + 39);
check_fullhouse(3, 19, 16, 32, 45);
check_fullhouse(1, 8, 9, 11, 22);
return 0;
}
/*
user@linux$ gcc -o card.bin card.c
user@linux$ ./card.bin
4 of Hearts.
7 of Diamonds.
4 of Diamonds.
7 of Clubs.
7 of Spades.
這副牌為7葫蘆
Bad cards.
*/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.22.98
→ jason06184:似乎有瑕疵 05/20 23:10
推 loveflames:你不是要葫蘆的判定方法嗎?還是說沒照原本程式格式寫就 05/20 23:16
→ loveflames:叫有瑕疵? 05/20 23:16
推 jason06184:不是 我的意思不是這樣 我的意思是放進去跑有錯誤 05/20 23:17
→ game0416:這不是你自己的問題嗎...? 05/20 23:18
推 jason06184:原來如此 我稍稍改一下下 05/20 23:23
→ siuol:tmp = (int *)calloc(size_t, size_t); 05/21 08:49
→ siuol:原PO應該不會強制轉型態吧.... 05/21 08:51
推 jason06184:樓上的ID..=口=!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 05/21 09:55
→ james732:用 gcc 沒寫 (int *) 其實沒關係...XD 05/21 09:56