user-0 提问: 就是用数据结构做一个金钩钓鱼.c

回复:
user: user-0
content: 引用 7 楼 lgbxyz 的回复:写了个简单的demo，没怎么测试，大小王也没做处理，仅供参考   #include <time.h> #include <stdio.h> #include <string.h> #include <stdlib.h>   #define MAX_PLAYER (3)  typedef enum _card_type { 	Spade = 0, 	//黑桃 	Heart,		//红桃 	Diamond,	//方块 	Club, 		//梅花 	Joker, 		//大王/小王 }card_type_t;  const static char* card_type_str[] = { 	"黑桃", 	//黑桃 	"红桃",	//红桃 	"方块",	//方块 	"梅花", 	//梅花 	"王", 	//大王/小王 };  typedef enum _card_value { 	CV_A = 1,  	CV_2, CV_3, CV_4, CV_5,  	CV_6, CV_7, CV_8, CV_9,  	CV_10, CV_J, CV_Q, CV_K,  	CV_RJ, //RedJokeker 	CV_BJ //BlackJoker }card_value_t;  const static char* card_value_str[] = { 	NULL, //dummy data for 0 	"A", "2", "3", "4", "5", 	"6", "7", "8", "9", "10", 	"J", "Q", "K", "大王", 	"小王" };  typedef struct _card_element { 	int card_index; // 1 - 54 	card_type_t  card_type; 	card_value_t card_value; }card_element_t;  typedef struct _card_list { 	card_element_t* elm; 	struct _card_list* next; }card_list_t;   //card num to element #define CRD_NUM_TO_ELM(num, elm)  { 	\ 	int val1 = num % CV_K;				\ 	int val2 = num / CV_K;				\ 	(elm)->card_index = num;			\ 	if (0 == val1) {					\ 		(elm)->card_type = 				\ 		((val2 == 0) ? 0 : (val2-1));	\ 		(elm)->card_value = CV_K;		\ 	} else {							\ 		(elm)->card_type = val2;		\ 		(elm)->card_value = val1;		\ 	}									\ } 						 //card element to str #define CRD_ELM_TO_STR(elm, str) { 							\ 	if ((elm)->card_type != Joker) {						\ 		strcpy(str, card_type_str[(elm)->card_type]);		\ 		strcat(str, card_value_str[(elm)->card_value]);		\ 	}else {													\ 		strcpy(str, card_value_str[(elm)->card_value+13]);	\ 	}														\ }  //card element to str #define CRD_NUM_TO_STR(num, str) { 							\ 	card_element_t elm;										\ 	CRD_NUM_TO_ELM(num, &elm);								\ 	CRD_ELM_TO_STR(&elm, str);								\ }   typedef struct _card_sequence { 	card_element_t elm[54]; 	card_element_t first_hand; //先手牌  }card_sequence_t;  typedef struct _card_player { 	char name[64]; //player name 	int card_num; 	card_list_t* head; //for pop 	card_list_t* tail; // for append }card_player_t;   char* get_card_str(card_element_t* elm) { 	static char str[64]; 	CRD_ELM_TO_STR(elm, str); 	return str; }  //洗牌  void poker_shuffle(card_sequence_t* sequence) {//FisherYates shuffle 	int arr[54] = {0};     int i, j;     char temp;          for (i = 1; i < 55; i++)     {     	arr[i-1] = i; 	}       if (i == 0) return; 	     srand(time(NULL)); 	     i = 54;     while (--i) {         j = rand() % (i+1);         temp = arr[i];         arr[i] = arr[j];         arr[j] = temp;     } 	 	for (i = 0; i < 54; i++) 	{ 		CRD_NUM_TO_ELM(arr[i], &sequence->elm[i]); 	} 	i = rand() % 53 + 1; 	CRD_NUM_TO_ELM(i, &sequence->first_hand); }  //抓牌 void poker_dispatch(card_sequence_t* sequence, card_player_t player[MAX_PLAYER]) { 	int i = 0, j = 0, k = 0; 	card_list_t *list = NULL; 	  	k = 54 / MAX_PLAYER; 	while (k--) { 		for (i = 0; i < MAX_PLAYER; i++) { 			list = (card_list_t *)calloc(sizeof(card_list_t), 1); 			list->elm = &(sequence->elm[j++]); 			list->next = NULL; 			if (NULL == player[i].tail) { 				player[i].head = list; 				player[i].tail = list; 			} else { 				player[i].tail->next = list; 				player[i].tail = list; 			} 			player[i].card_num++; 		} 	} }  //出牌 int poker_push_card(card_player_t *player) { 	int i, j, k = 0; 	static card_element_t* cards[54]; 	static int idx; 	static int losers = 0; 	card_list_t* list = NULL; 	card_list_t* temp = NULL;  	if (player->card_num <= 0) { 		return 0; 	} START: 	list = player->head; 	player->head = list->next; 	player->card_num--; 	cards[idx++] = list->elm; 	printf("%s[还有%2d张牌]%s出牌:%s ", player->name, player->card_num, \ 		((k == 0)?"":"继续"), get_card_str(list->elm));   	for (i = idx - 2; i >= 0 ; i--) { 		if (Joker == list->elm->card_type) { 			break; 		} 		else if (list->elm->card_value == CV_J) { 			k++; 			printf("赢得所有牌(%d张牌):", idx); 			for (j = idx - 1; j >= 0; j--) { 				temp = (card_list_t *)calloc(sizeof(card_list_t), 1); 				temp->elm = cards[j]; 				temp->next = NULL; 				player->tail->next = temp; 				if (NULL == player->head) { 					player->head = temp; 				} 				player->tail = temp; 				player->card_num++; 				idx--; 				printf("%s ", get_card_str(cards[j])); 			} 			free(list); 			list = NULL; 			printf("\n"); 			goto START; 		} 		else if (list->elm->card_value == cards[i]->card_value) { 			k++; 			printf("赢得%d张牌:", idx-i); 			for (j = idx - 1; j >= i; j--) { 				temp = (card_list_t *)calloc(sizeof(card_list_t), 1); 				temp->elm = cards[j]; 				temp->next = NULL; 				player->tail->next = temp; 				player->tail = temp; 				if (NULL == player->head) { 					player->head = temp; 				} 				player->card_num++; 				idx--; 				printf("%s ", card_value_str[cards[j]->card_value]); 			} 			free(list); 			list = NULL; 			printf("\n"); 			goto START; 		} 	}  	if (list) { 		free(list); 	} 	printf("\n");  	if (player->card_num <= 0) { 		losers++; 		printf("[%s]出局!\n", player->name); 		if (losers == (MAX_PLAYER - 1)) { 			return 1; 		} 	} 	return 0; }  //游戏 void poker_run_fish(card_player_t player[MAX_PLAYER]) { 	int i = 0; 	 	while (1) { 		i = 0; 		for (i = 0; i < MAX_PLAYER; i++) { 			if (1 == poker_push_card(&player[i])) { 				return; 			} 		} 	} }  void free_player_info(card_player_t player[MAX_PLAYER]) { 	int i = 0; 	card_list_t* list = NULL; 	 	for (i = 0; i < MAX_PLAYER; i++) { 		list = player[i].head; 		while (list != NULL) { 			player[i].head = list->next; 			free(list); 			list = player[i].head; 		} 	} }   int main(void) { 	int i = 0; 	card_sequence_t seq; 	card_player_t player[MAX_PLAYER]; 	 	poker_shuffle(&seq); 	 	memset(player, 0x00, sizeof(player)); 	 	//name the player 	for (i = 0; i < MAX_PLAYER; i++) 	{ 		sprintf(player[i].name, "player_%d", i + 1); 	} 	 	poker_dispatch(&seq, player); 	 	poker_run_fish(player); 	 	for (i = 0; i < MAX_PLAYER; i++) { 		if (player[i].card_num > 0) { 			printf("%s 获胜(%2d)!!!!!!!!!\n", player[i].name, player[i].card_num); 			break; 		} 	}  	free_player_info(player); 	 	return 0; }    没事的，不用管大小王。我们的要求其实只是数字就可以了。不用想太多

user: user-1
content: 写了个简单的demo，没怎么测试，大小王也没做处理，仅供参考   #include <time.h> #include <stdio.h> #include <string.h> #include <stdlib.h>   #define MAX_PLAYER (3)  typedef enum _card_type { 	Spade = 0, 	//黑桃 	Heart,		//红桃 	Diamond,	//方块 	Club, 		//梅花 	Joker, 		//大王/小王 }card_type_t;  const static char* card_type_str[] = { 	"黑桃", 	//黑桃 	"红桃",	//红桃 	"方块",	//方块 	"梅花", 	//梅花 	"王", 	//大王/小王 };  typedef enum _card_value { 	CV_A = 1,  	CV_2, CV_3, CV_4, CV_5,  	CV_6, CV_7, CV_8, CV_9,  	CV_10, CV_J, CV_Q, CV_K,  	CV_RJ, //RedJokeker 	CV_BJ //BlackJoker }card_value_t;  const static char* card_value_str[] = { 	NULL, //dummy data for 0 	"A", "2", "3", "4", "5", 	"6", "7", "8", "9", "10", 	"J", "Q", "K", "大王", 	"小王" };  typedef struct _card_element { 	int card_index; // 1 - 54 	card_type_t  card_type; 	card_value_t card_value; }card_element_t;  typedef struct _card_list { 	card_element_t* elm; 	struct _card_list* next; }card_list_t;   //card num to element #define CRD_NUM_TO_ELM(num, elm)  { 	\ 	int val1 = num % CV_K;				\ 	int val2 = num / CV_K;				\ 	(elm)->card_index = num;			\ 	if (0 == val1) {					\ 		(elm)->card_type = 				\ 		((val2 == 0) ? 0 : (val2-1));	\ 		(elm)->card_value = CV_K;		\ 	} else {							\ 		(elm)->card_type = val2;		\ 		(elm)->card_value = val1;		\ 	}									\ } 						 //card element to str #define CRD_ELM_TO_STR(elm, str) { 							\ 	if ((elm)->card_type != Joker) {						\ 		strcpy(str, card_type_str[(elm)->card_type]);		\ 		strcat(str, card_value_str[(elm)->card_value]);		\ 	}else {													\ 		strcpy(str, card_value_str[(elm)->card_value+13]);	\ 	}														\ }  //card element to str #define CRD_NUM_TO_STR(num, str) { 							\ 	card_element_t elm;										\ 	CRD_NUM_TO_ELM(num, &elm);								\ 	CRD_ELM_TO_STR(&elm, str);								\ }   typedef struct _card_sequence { 	card_element_t elm[54]; 	card_element_t first_hand; //先手牌  }card_sequence_t;  typedef struct _card_player { 	char name[64]; //player name 	int card_num; 	card_list_t* head; //for pop 	card_list_t* tail; // for append }card_player_t;   char* get_card_str(card_element_t* elm) { 	static char str[64]; 	CRD_ELM_TO_STR(elm, str); 	return str; }  //洗牌  void poker_shuffle(card_sequence_t* sequence) {//FisherYates shuffle 	int arr[54] = {0};     int i, j;     char temp;          for (i = 1; i < 55; i++)     {     	arr[i-1] = i; 	}       if (i == 0) return; 	     srand(time(NULL)); 	     i = 54;     while (--i) {         j = rand() % (i+1);         temp = arr[i];         arr[i] = arr[j];         arr[j] = temp;     } 	 	for (i = 0; i < 54; i++) 	{ 		CRD_NUM_TO_ELM(arr[i], &sequence->elm[i]); 	} 	i = rand() % 53 + 1; 	CRD_NUM_TO_ELM(i, &sequence->first_hand); }  //抓牌 void poker_dispatch(card_sequence_t* sequence, card_player_t player[MAX_PLAYER]) { 	int i = 0, j = 0, k = 0; 	card_list_t *list = NULL; 	  	k = 54 / MAX_PLAYER; 	while (k--) { 		for (i = 0; i < MAX_PLAYER; i++) { 			list = (card_list_t *)calloc(sizeof(card_list_t), 1); 			list->elm = &(sequence->elm[j++]); 			list->next = NULL; 			if (NULL == player[i].tail) { 				player[i].head = list; 				player[i].tail = list; 			} else { 				player[i].tail->next = list; 				player[i].tail = list; 			} 			player[i].card_num++; 		} 	} }  //出牌 int poker_push_card(card_player_t *player) { 	int i, j, k = 0; 	static card_element_t* cards[54]; 	static int idx; 	static int losers = 0; 	card_list_t* list = NULL; 	card_list_t* temp = NULL;  	if (player->card_num <= 0) { 		return 0; 	} START: 	list = player->head; 	player->head = list->next; 	player->card_num--; 	cards[idx++] = list->elm; 	printf("%s[还有%2d张牌]%s出牌:%s ", player->name, player->card_num, \ 		((k == 0)?"":"继续"), get_card_str(list->elm));   	for (i = idx - 2; i >= 0 ; i--) { 		if (Joker == list->elm->card_type) { 			break; 		} 		else if (list->elm->card_value == CV_J) { 			k++; 			printf("赢得所有牌(%d张牌):", idx); 			for (j = idx - 1; j >= 0; j--) { 				temp = (card_list_t *)calloc(sizeof(card_list_t), 1); 				temp->elm = cards[j]; 				temp->next = NULL; 				player->tail->next = temp; 				if (NULL == player->head) { 					player->head = temp; 				} 				player->tail = temp; 				player->card_num++; 				idx--; 				printf("%s ", get_card_str(cards[j])); 			} 			free(list); 			list = NULL; 			printf("\n"); 			goto START; 		} 		else if (list->elm->card_value == cards[i]->card_value) { 			k++; 			printf("赢得%d张牌:", idx-i); 			for (j = idx - 1; j >= i; j--) { 				temp = (card_list_t *)calloc(sizeof(card_list_t), 1); 				temp->elm = cards[j]; 				temp->next = NULL; 				player->tail->next = temp; 				player->tail = temp; 				if (NULL == player->head) { 					player->head = temp; 				} 				player->card_num++; 				idx--; 				printf("%s ", card_value_str[cards[j]->card_value]); 			} 			free(list); 			list = NULL; 			printf("\n"); 			goto START; 		} 	}  	if (list) { 		free(list); 	} 	printf("\n");  	if (player->card_num <= 0) { 		losers++; 		printf("[%s]出局!\n", player->name); 		if (losers == (MAX_PLAYER - 1)) { 			return 1; 		} 	} 	return 0; }  //游戏 void poker_run_fish(card_player_t player[MAX_PLAYER]) { 	int i = 0; 	 	while (1) { 		i = 0; 		for (i = 0; i < MAX_PLAYER; i++) { 			if (1 == poker_push_card(&player[i])) { 				return; 			} 		} 	} }  void free_player_info(card_player_t player[MAX_PLAYER]) { 	int i = 0; 	card_list_t* list = NULL; 	 	for (i = 0; i < MAX_PLAYER; i++) { 		list = player[i].head; 		while (list != NULL) { 			player[i].head = list->next; 			free(list); 			list = player[i].head; 		} 	} }   int main(void) { 	int i = 0; 	card_sequence_t seq; 	card_player_t player[MAX_PLAYER]; 	 	poker_shuffle(&seq); 	 	memset(player, 0x00, sizeof(player)); 	 	//name the player 	for (i = 0; i < MAX_PLAYER; i++) 	{ 		sprintf(player[i].name, "player_%d", i + 1); 	} 	 	poker_dispatch(&seq, player); 	 	poker_run_fish(player); 	 	for (i = 0; i < MAX_PLAYER; i++) { 		if (player[i].card_num > 0) { 			printf("%s 获胜(%2d)!!!!!!!!!\n", player[i].name, player[i].card_num); 			break; 		} 	}  	free_player_info(player); 	 	return 0; }

user: user-2
content: 洗牌参考：#include <stdio.h> #include <stdlib.h> #include <time.h> int d[6]; int i,n,a,b,t; int c,j; void main() {     srand(time(NULL));     printf("shuffle 0..n-1 demo\n");     for (n=1;n<=5;n++) {/* 测试1～5个元素 */         printf("_____n=%d_____\n",n);         j=1;         for (c=1;c<=n;c++) j=j*c;/* j为n! */         j*=n*2;         for (c=1;c<=j;c++) {/* 测试n*2*n!次 */             for (i=0;i<n;i++) d[i]=i;/* 填写0～n-1 */             for (i=n;i>0;i--) {/* 打乱0～n-1 */                 a=i-1;b=rand()%i;                 if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}             }             printf("%04d:",c);             for (i=0;i<n;i++) printf("%d",d[i]);             printf("\n");         }     }     printf("shuffle 1..n demo\n");     for (n=1;n<=5;n++) {/* 测试1～5个元素 */         printf("_____n=%d_____\n",n);         j=1;         for (c=1;c<=n;c++) j=j*c;/* j为n! */         j*=n*2;         for (c=1;c<=j;c++) {/* 测试n*2*n!次 */             for (i=1;i<=n;i++) d[i]=i;/* 填写1～n */             for (i=n;i>1;i--) {/* 打乱1～n */                 a=i;b=rand()%i+1;                 if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}             }             printf("%04d:",c);             for (i=1;i<=n;i++) printf("%d",d[i]);             printf("\n");         }     } }

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:全部用数组即可。这个有特殊要求

user: user-2
content: 全部用数组即可。

user: user-2
content: 我们叫“弥竹竿”

user: user-3
content: 原来是叫这个名字啊，小时候玩过

user: user-0
content: 引用 4 楼 zhao4zhong1 的回复:洗牌参考：#include <stdio.h> #include <stdlib.h> #include <time.h> int d[6]; int i,n,a,b,t; int c,j; void main() {     srand(time(NULL));     printf("shuffle 0..n-1 demo\n");     for (n=1;n<=5;n++) {/* 测试1～5个元素 */         printf("_____n=%d_____\n",n);         j=1;         for (c=1;c<=n;c++) j=j*c;/* j为n! */         j*=n*2;         for (c=1;c<=j;c++) {/* 测试n*2*n!次 */             for (i=0;i<n;i++) d[i]=i;/* 填写0～n-1 */             for (i=n;i>0;i--) {/* 打乱0～n-1 */                 a=i-1;b=rand()%i;                 if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}             }             printf("%04d:",c);             for (i=0;i<n;i++) printf("%d",d[i]);             printf("\n");         }     }     printf("shuffle 1..n demo\n");     for (n=1;n<=5;n++) {/* 测试1～5个元素 */         printf("_____n=%d_____\n",n);         j=1;         for (c=1;c<=n;c++) j=j*c;/* j为n! */         j*=n*2;         for (c=1;c<=j;c++) {/* 测试n*2*n!次 */             for (i=1;i<=n;i++) d[i]=i;/* 填写1～n */             for (i=n;i>1;i--) {/* 打乱1～n */                 a=i;b=rand()%i+1;                 if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}             }             printf("%04d:",c);             for (i=1;i<=n;i++) printf("%d",d[i]);             printf("\n");         }     } } 这个不需要洗牌，但还是感谢这位大哥