输入搜索关键词并按下Enter键开始搜索
德州扑克 (Texas hold'em,有时也简称为Hold'em或Holdem),简称德扑,是世界上最流行的公牌扑克衍生游戏,也是国际扑克比赛的正式竞赛项目之一。世界扑克大赛(World Series of Poker,WSOP)和世界扑克巡回赛(World Poker Tour,WPT)的主赛事(Main Event)项目即是“无限注德州扑克”。德州扑克是位置顺序影响最大的扑克衍生游戏之一,每一轮的下注顺序维持不变,它也是美国多数赌场内最受欢迎的扑克牌类游戏,在美国以外的地区也十分流行,与桥牌的流行程度相当。理论上一桌同时最多可容纳22位(若不销牌则为23位)牌手,但一般是二至十人一桌。
——Wikipedia 德州扑克
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> #include <string.h> const char ranks[] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'}; const char suits[] = {'C', 'D', 'H', 'S'}; int num[7] = {0, 1, 2, 3, 4, 5, 6}; int in_hand[4][13] = {0}; /*防止发到重复的牌*/ int repeat[13] = {0}; /*存数字重复的数量*/ int cnt = 0, cnt2 = 1; int max = 0; /*存最佳牌型的值*/ typedef struct Sort_ /*分开存每张牌的花色及数字*/ { char suit; int rank; } Sort; Sort rec_cards[52]; /*出现过的牌*/ Sort p_cards[2]; /*玩家手牌*/ Sort r_cards[2]; /*电脑手牌*/ Sort s_cards[5]; /*公牌*/ Sort c_cards[7]; /*手牌+公牌*/ Sort m_cards[5]; /*c_cards 7取5 出来的阵列*/ Sort n_cards[5]; /*存最佳牌型*/ Sort t_cards[5]; /*暂存牌型*/ int compare(const Sort *a, const Sort *b) /*qsort比较函式*/ { ... } void combine_sort(Sort a[], Sort b[], int len_a, int len_b) /*合并手牌与公牌*/ { int i; for (i = 0; i < len_a; i++) c_cards[i] = a[i]; for (i = 0; i < len_b; i++) c_cards[i + len_a] = b[i]; qsort(c_cards, len_a + len_b, sizeof(Sort), compare); } bool straight(Sort data[]) /*判断顺子*/ { int i, j, flag = 0; for (i = 0; i < 4; i++) { if (data[i].rank + 1 == data[i + 1].rank) { flag++; if (flag == 3 && data[flag + 1].rank == 0 || flag == 4) return true; } else return false; } } int find_type(Sort data[]) /*判断牌型*/ { int i, j, flag = 0; int repeat[13] = {0}; for (i = 0; i < 5; i++) /*纪录每个数字出现了几次*/ repeat[data[i].rank]++; for (i = 0; i < 5; i++) /*同花顺*/ { if (data[0].suit == data[i].suit) { if (i == 4) if (straight(data) == true) return 9; } else break; } for (i = 0; i < 13; i++) /*四条*/ { if (repeat[i] == 4) return 8; } for (i = 0; i < 13; i++) /*葫芦*/ { if (repeat[i] == 3) for (j = 0; j < 13; j++) { if (repeat[j] == 2) return 7; } } for (i = 0; i < 5; i++) /*同花*/ { if (data[0].suit == data[i].suit) { if (i == 4) return 6; } else break; } if (straight(data) == true) /*顺子*/ return 5; for (i = 0; i < 13; i++) /*三条*/ { if (repeat[i] == 3) for (j = 0; j < 5; j++) { if (repeat[j] == 1) return 4; } } flag = 0; for (i = 0; i < 13; i++) /*两对*/ if (repeat[i] == 2) { flag++; if (flag == 2) return 3; } flag = 0; for (i = 0; i < 13; i++) /*一对*/ if (repeat[i] == 2) { flag++; if (flag == 1) return 2; } return 1; /*高牌*/ } int combination(int data[], int after, int got) /* 找出手牌+公牌取5张的所有组合 */ { int i; if (5 == got) { for (i = 0; i < 5; i++) m_cards[i] = c_cards[data[i]]; int tmp = find_type(m_cards); /*每种组合丢入find_type找出是哪种牌型*/ if (tmp == max) { if (m_cards[4].rank > t_cards[4].rank) { if (m_cards[4].suit > t_cards[4].suit) for (i = 0; i < 5; i++) n_cards[i] = m_cards[i]; for (i = 0; i < 5; i++) n_cards[i] = m_cards[i]; } else for (i = 0; i < 5; i++) n_cards[i] = t_cards[i]; } if (tmp > max) { max = tmp; for (i = 0; i < 5; i++) n_cards[i] = m_cards[i]; } return; } for (i = after; i < 7; i++) { int tmp = data[got]; data[got] = data[i]; data[i] = tmp; combination(data, i + 1, got + 1); tmp = data[got]; data[got] = data[i]; data[i] = tmp; } } int reset() /*重置max*/ { max = 0; combination(num, 0, 0); return max; } void num_to_type(int n) /*印出牌型名称*/ { ... } void save_cards(Sort a[], int len) /*把发的牌分别存起来*/ { int i; if (len == 2 && cnt2 % 3 == 1) for (i = 0; i < 2; i++) p_cards[i] = a[i]; if (len == 2 && cnt2 % 3 == 2) for (i = 0; i < 2; i++) r_cards[i] = a[i]; if (len == 5) for (i = 0; i < 5; i++) s_cards[i] = a[i]; cnt2++; } void deal_cards(int n) /*发牌*/ { int i, rank, suit; Sort data[n]; for (i = 0; i < n;) { suit = rand() % 4; rank = rand() % 13; if (in_hand[suit][rank] == 0) { in_hand[suit][rank] = 1; printf("%c%c ", ranks[rank], suits[suit]); data[i].suit = suits[suit]; data[i].rank = rank; rec_cards[cnt].suit = suits[suit]; rec_cards[cnt].rank = rank; i++; cnt++; } } save_cards(data, n); qsort(rec_cards, cnt, sizeof(Sort), compare); printf("\n\n"); } void welcome() /*欢迎介面*/ { ... } int main() { srand((unsigned)time(NULL)); int i, round = 0, p_tmp, r_tmp, p_win = 0, r_win = 0; printf("是否开始游戏?(Y/N):"); welcome(); while (round++ < 5) { printf("\n"); printf("你的牌:\n"); deal_cards(2); printf("电脑的牌:\n"); deal_cards(2); printf("公牌:\n"); deal_cards(5); printf("按任意键显示胜负\n\n"); getch(); combine_sort(p_cards, s_cards, 2, 5); printf("你的牌与公牌:"); for (i = 0; i < 7; i++) printf("%c%c ", ranks[c_cards[i].rank], c_cards[i].suit); printf("\n"); p_tmp = reset(); printf("最佳组合:"); for (i = 0; i < 5; i++) printf("%c%c ", ranks[n_cards[i].rank], n_cards[i].suit); num_to_type(p_tmp); combine_sort(r_cards, s_cards, 2, 5); printf("\n\n电脑的牌与公牌:"); for (i = 0; i < 7; i++) printf("%c%c ", ranks[c_cards[i].rank], c_cards[i].suit); printf("\n"); r_tmp = reset(); printf("最佳组合:"); for (i = 0; i < 5; i++) printf("%c%c ", ranks[n_cards[i].rank], n_cards[i].suit); num_to_type(r_tmp); if (p_tmp > r_tmp) { printf("\n你赢了!\n"); p_win++; } else if (p_tmp < r_tmp) { printf("\n你输了!\n"); r_win++; } else printf("\n平手!\n"); printf("\n目前比数(你:电脑) %d:%d\n", p_win, r_win); printf("\n已出现的纸牌:"); for (i = 0; i < cnt; i++) printf("%c%c ", ranks[rec_cards[i].rank], rec_cards[i].suit); printf("张数:%d\n", cnt); printf("\n按任意键进行下一局\n"); getch(); } printf("\n游戏结束!\n\n最终比数(你:电脑) %d:%d", p_win, r_win); }
0 条评论