对于一个学校项目,我正在尝试使纸牌游戏变成鱼。到目前为止,我已经有了它,以便它可以从文件(用于测试特定组合)或从主循环中的某些循环在双链表中创建一个卡片组。然后,我可以拿走那副纸牌并对其进行随机打乱并打印卡片。我很难弄清楚如何每两位玩家发七张牌。我有一个想法,在其中创建hand1和hand2,然后从列表中取出前七张卡,将它们复制到hand1,然后从列表中删除它们。与hand2相同。该系统在从甲板上抓牌(钓鱼)或从第二位玩家偷牌方面也很有用。我将如何在代码中实现类似的东西?到目前为止,这是我的代码示例。任何帮助将不胜感激,并将帮助我了解发生了什么。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
typedef struct card_s{
char suit[7];
int value;
struct card_s *next, *previous;
}card;
int rand_gen(int count){
double frac;
frac = (double)rand()/((double)RAND_MAX+1);
return floor(count * frac); //random number in [0, count]
}
void deleteMember(card *p, card **hl, card **hr) {
if (p == *hl){ // if deleting the first element
*hl = p->next;
}
// update the left headp
else{
p->previous->next = p->next;
}
if (p == *hr){
*hr = p->previous; // if deleting the last element
}
else{
p->next->previous = p->previous;
}
free(p); // free memory
}
void addCard(card *p, card **hl, card **hr, FILE *inp){
card *temp; // pointer to a node_t
temp = (card *)malloc(sizeof (card)); // new node creation
fscanf(inp, "%d", &temp->value);
fscanf(inp, "%s", temp->suit);
if (*hl == NULL){ // if left head is null, i.e., list is empty
temp->previous = NULL;
temp->next = NULL;
*hl = temp;
*hr = temp;
}
else if (p->next == NULL){ // if adding at the end of list
temp->previous = p;
temp->next = NULL;
p->next = temp;
*hr = temp;
}
else{ // if adding to the middle of the list
temp->next = p-> next;
temp->previous = p;
p->next = temp;
temp->next->previous = temp;
}
}
void addCard1(card *p, card **hl, card **hr, int value, char suit[7]){
card *temp; // pointer to a node_t
temp = (card *)malloc(sizeof (card)); // new node creation
temp->value = value;
strcpy(temp->suit, suit);
if (*hl == NULL){ // if left head is null, i.e., list is empty
temp->previous = NULL;
temp->next = NULL;
*hl = temp;
*hr = temp;
}
else if (p->next == NULL){ // if adding at the end of list
temp->previous = p;
temp->next = NULL;
p->next = temp;
*hr = temp;
}
else{ // if adding to the middle of the list
temp->next = p-> next;
temp->previous = p;
p->next = temp;
temp->next->previous = temp;
}
}
void Print(card **headl) { //this function prints from left to right only
card *temp;
temp = (*headl);
while(temp != NULL) {
printf("|");
printf("%d", temp->value);
if(strcmp(temp->suit,"hea")==0) {
printf("♥");
}
if(strcmp(temp->suit,"clu")==0) {
printf("♣");
}
if(strcmp(temp->suit,"spa")==0) {
printf("♠");
}
if(strcmp(temp->suit,"dia")==0) {
printf("♦");
}
printf("|");
//printf("%s ", temp->suit);
temp = temp->next;
}
}
void Print1(card **headl) { //this function prints from left to right only
card *temp;
temp = (*headl);
while(temp != NULL) {
for(int i = 0; i < 13; i++) {
printf("|");
if(temp->value == 11){
printf("J");
}
else if(temp->value == 12) {
printf("Q");
}
else if(temp->value == 13) {
printf("K");
}
else if(temp->value == 14) {
printf("A");
}
else {
printf("%d", temp->value);
}
if(strcmp(temp->suit,"h")==0) {
printf("♥");
}
if(strcmp(temp->suit,"c")==0) {
printf("♣");
}
if(strcmp(temp->suit,"s")==0) {
printf("♠");
}
if(strcmp(temp->suit,"d")==0) {
printf("♦");
}
printf("|");
//printf("%s ", temp->suit);
temp = temp->next;
}
printf("\n");
}
}
void swap(card *pt, int i, int j) {
int i1 = i;
int j1 = j;
if(i1 == j1) {
j1 = j1 + 1;
}
card *temp;
temp = (card *)malloc(sizeof(card));
card *temp1 = pt, *temp2 = pt; //creates temperaries that start from the head left
for(int x = 0; x < i1; x++) { //counts the number of nodes until i
temp1 = temp1->next;
}
for(int x = 0; x < j1; x++) { //counts the number of nodes until j
temp2 = temp2->next;
}
temp->value = temp1->value; //swaps the information not the nodes
strcpy(temp->suit,temp1->suit);
temp1->value = temp2->value;
strcpy(temp1->suit, temp2->suit);
temp2->value = temp->value;
strcpy(temp2->suit, temp->suit);
}
int main(void) {
FILE *inp = NULL;
char player1[20];
char player2[20];
char y;
int i = 0;
char heart[]= "h";
char diamond[] = "d";
char clubs[] = "c";
char spades[] = "s";
inp = fopen("cards.txt", "r");
srand((int)time(NULL));
card *headl = NULL, *headr = NULL;
if(inp == NULL) {
printf("No file found");
return -1;
}
printIntro();
printf("\n");
printf("Player one enter your name: ");
fgets(player1, sizeof(player1)-2, stdin);
char *pos;
if ((pos=strchr(player1, '\n')) != NULL) //this it get rid of the enter in the string
*pos = '\0';
printf("Player two enter your name: ");
fgets(player2, sizeof(player2)-2, stdin);
i = 0;
char *pos1;
if ((pos1=strchr(player2, '\n')) != NULL)
*pos1 = '\0';
printf("Welcome %s and %s", player1, player2);
printf("\n");
printf("Would you like to load a preloaded deck (y/n): ");
scanf(" %c", &y);
if(y =='y' || y =='Y') {
for(int i = 0; i < 52; i++) {
addCard(headr, &headl, &headr, inp);
}
}
else {
for(int i = 2; i<15; i++) {
addCard1(headr, &headl, &headr, i, heart);
}
for(int i = 2; i<15; i++) {
addCard1(headr, &headl, &headr, i, diamond);
}
for(int i = 2; i<15; i++) {
addCard1(headr, &headl, &headr, i, clubs);
}
for(int i = 2; i<15; i++) {
addCard1(headr, &headl, &headr, i, spades);
}
}
Print1(&headl);
printf("\n");
printf("\n");
for(int i = 0; i <= 152; i++) { //does the swap function swap number times
swap(headl, rand_gen(52), rand_gen(52));
}
Print1(&headl);
return(0);
}
编辑:我已经尝试过了
for(int i = 0; i < 8; i++) {
addCard1(hand1r, &hand1l, &headr, headr->value, headr->suit);
deleteMember(headl, &headl, &headr);
}
但是,它在else if部分的addmember1函数上给我一个错误“线程1:EXC_BAD_ACCESS(code = 1,address = 0x10)”。
编辑2:我发现了问题所在。 这就是我必须要动手的东西
for(int i = 0; i < 7; i++) {
addCard1(hand1r, &hand1l, &hand1r, headl->value, headl->suit);
deleteMember(headl, &headl, &headr);
}
Print(&hand1l);
printf("\n");
for(int i = 0; i < 7; i++) {
addCard1(hand2r, &hand2l, &hand2r, headl->value, headl->suit);
deleteMember(headl, &headl, &headr);
}
Print(&hand2l);