当我从另一个链表中删除节点时,为什么另一个链表中的链表值也会被删除?

时间:2019-04-28 23:55:52

标签: c visual-studio-2015

我有一个链表,代表一副纸牌,当我将一张纸牌提供给玩家和计算机时(一次一张),我复制了最上面一张纸牌的值,并将其值提供给手中的卡,但是当我从卡组中删除该节点时,其删除会与手中的卡的值混淆。删除时其值与手中卡的值完全相同。

此外,我包括了我所有的代码,以防万一它有所帮助,但现在还没有全部完成。在继续前进之前,请尝试着动手工作。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<time.h>
#include <stdlib.h>
#include <wchar.h>
#include <conio.h>

int rand_gen(int count) {
double frac;
frac = (double)rand() / ((double)RAND_MAX + 1);
return floor(count * frac);
}
typedef struct card_s {
char suit;
int suitnum;
int face;
struct card_s *next, *previous;
}card;

void deletecard(card *p, card **hl) {
if (p == *hl) {
    *hl = p->next;
}
else {
    p->previous->next = p->next;
}
if (p->next == NULL) {
    p->previous->next = NULL;
}
else {
    p->next->previous = p->previous;
}
free(p);
}
void createdeck(card **hl) {
int i = 1;
int j;
int y = 0;
card* temp;
temp = (card *)malloc(sizeof(card));
card* temp2;
temp2 = (card *)malloc(sizeof(card));
while (i <= 13) {
    j = 1;
    while (j <= 4) {
        temp2->face = i;
        temp2->suitnum = j;
        if (*hl == NULL) {
            temp->next = NULL;
            temp->previous = NULL;
            temp->face = 1;
            temp->suitnum = 1;
            (*hl) = temp;
        }
        else {
            temp->next = temp2;
            temp2->previous = temp;
            temp2->next = NULL;
            temp = temp->next;
            temp2 = (card *)malloc(sizeof(card));
        }
        j++;
    }
    i++;
}
}
void printdeck(card *currentNode) {
printf("\n\n\n");
if (currentNode->suitnum == 1) {
    currentNode->suit = '♣';
}
else if (currentNode->suitnum == 2) {
    currentNode->suit = '♦';
}
else if (currentNode->suitnum == 3) {
    currentNode->suit = '♥';
}
else {
    currentNode->suit = '♠';
}
while (currentNode != NULL) {
    printf("Face: %d, Suit: %c\n", currentNode->face, currentNode- 
>suit);
    currentNode = currentNode->next;
}
}

void shuffle(card *current) {
int x = 1;
while (x < 1000) {
    int i = 0;
    int c = 0;
    int y = 0;
    card* temp;
    card *temp2;
    card* temp3;
    temp3 = (card *)malloc(sizeof(card));
    temp2 = (card *)malloc(sizeof(card));
    temp = (card *)malloc(sizeof(card));
    while (i < 52) {
        int c = 0;
        int j = rand_gen(51);
        temp = current;
        while (c < j) {
            temp = temp->next;
            c++;
        }
        int y = 0;
        temp2 = current;
        while (y < i) {
            temp2 = temp2->next;
            y++;
        }
        temp3->face = temp2->face;
        temp3->suitnum = temp2->suitnum;
        temp2->face = temp->face;
        temp2->suitnum = temp->suitnum;
        temp->face = temp3->face;
        temp->suitnum = temp3->suitnum;
        i++;
    }
    x++;
}
}

void drawhand(card **player, card **computer, card **headl) {
//not done yet, need to fix the cards' values from going deleted
int i = 0;
card* temp = (card *)malloc(sizeof(card));
temp = *headl;
*player = temp;
card* temp2 = (card *)malloc(sizeof(card));
temp2 = temp->next;
deletecard(temp, headl);
*computer = temp2;
card* temp3 = (card *)malloc(sizeof(card));
temp3 = temp2->next;
deletecard(temp2, headl);
(*player)->next = temp3;
card* temp4 = (card *)malloc(sizeof(card));
temp4 = temp3->next;
deletecard(temp3, headl);
(*computer)->next = temp4;
card* temp5 = (card *)malloc(sizeof(card));
temp5 = temp4->next;
deletecard(temp4, headl);
(*player)->next->next = temp5;
card* temp6 = (card *)malloc(sizeof(card));
temp6 = temp5->next;
deletecard(temp5, headl);
(*computer)->next->next = temp6;
card* temp7 = (card *)malloc(sizeof(card));
temp7 = temp6->next;
deletecard(temp6, headl);
(*player)->next->next->next = temp7;
card* temp8 = (card *)malloc(sizeof(card));
temp8 = temp7->next;
deletecard(temp7, headl);
(*computer)->next->next->next = temp8;
card* temp9 = (card *)malloc(sizeof(card));
temp9 = temp8->next;
deletecard(temp8, headl);
(*player)->next->next->next->next = temp9;
card* temp10 = (card *)malloc(sizeof(card));
temp10 = temp9->next;
deletecard(temp9, headl);
deletecard(temp10, headl);
(*player)->next->previous = *player;
(*player)->next->next->previous = (*player)->next;
(*player)->next->next->next->previous = (*player)->next->next;
(*player)->next->next->next->next->previous = (*player)->next->next- 
>next;
(*player)->next->next->next->next->next = NULL;
(*computer)->next->previous = *computer;
(*computer)->next->next->previous = (*computer)->next;
(*computer)->next->next->next->previous = (*computer)->next->next;
(*computer)->next->next->next->next->previous = (*computer)->next- 
>next->next;
(*computer)->next->next->next->next->next = NULL;
}
void valueprint(int x) {
printf("Royal Flush          10♠ J♠ Q♠ K♠ A♠\n\n");
printf("Straight Flush       2♣ 3♣ 4♣ 5♣ 6♣\n\n"); 
printf("Four of a Kind       9♣ 9♠ 9♥ 9♦ ■\n\n");
printf("Full House           9♣ 9♠ 9♥ 3♥ 3♠\n\n");
printf("Flush                ■♥ ■♥ ■♥ ■♥ ■♥\n\n");
printf("Straight             4■ 5■ 6■ 7■ 8■\n\n");
printf("Three of a Kind      9♣ 9♠ 9♥ ■ ■\n\n");
printf("Two Pair             K♠ K♣ 6♠ 6♥ ■\n\n");
printf("Jacks or Better      J♣ J♠ ■ ■ ■\n\n");
}
void computerhand(card *currentnode, int i) {
printf("\n\n\n");
//not done yet, need to print the computer's three cards and 2 blank 
spaces
}
void bank(int balance) {
printf("Your balance is: %d coins.\n", balance);
}
int bet(int *balance) {
int bet;
printf("Place your bet (1-%d) coins: \n", balance);
scanf("%d", &bet);
while (bet > balance || bet < 0) {
    printf("Place your bet (1-%d) coins: \n", balance);
    scanf("%d", &bet);
}
balance = balance - bet;
printf("You bet %d coins.  Now you have %d coins.\n", bet, balance);
return bet;
}
void swap(card **player) {
int i = 0;
int array[5];
int value;
int y = 0;
while (y != -1) {
    printf("Pick cards (between 1-5) to swap (-1 to stop):");
    scanf("%d", &value);
    if (value == -1) {
        y = -1;
    }
    else if (value <= 5 || value >= 0){
        array[i] = value;
        i++;
    }
}
i = 0;
/*while(array[i]!=NULL){
card* temp = (card *)malloc(sizeof(card));
i++;*/
//not done yet, still need to swap cards
}
void computerswap(card **computer) { 
//not done yet, need to do it all
}
int winner(card *player, card *computer) {
//not done yet, need to do it all
}

int main(void) {
char answer = 'y';
while (answer == 'y' || answer == 'Y') {
    int x = 1;
    valueprint(x);
    int *balance=100;
    while (balance > 0) {
        srand((int)time(NULL));
        card *headl = NULL;
        createdeck(&headl);
        printdeck(headl);
        shuffle(headl);
        int betvalue;
        printdeck(headl);
        card* player = NULL;
        card* computer = NULL;
        drawhand(&player, &computer, &headl);
        printf("Computer/Dealer's hand:\n");
        computerhand(computer, 1);
        printf("Your hand:\n");
        printdeck(player);
bank(balance);
        betvalue = bet(balance);
        swap(player);
        computerswap(computer);
        printf("Your hand: \n");
        printdeck(player);
        printf("Computer/Dealer's hand:\n");
        computerhand(computer, 2);
        if (winner(player, computer) == 1) { //player
            balance = balance + betvalue;
            printf("You won!!! Your balance is now %d", 
balance);
        }
        else if (winner(player, computer) == 2) { //computer
            balance = balance - betvalue;
            printf("you lost.  Your balance is now %d", 
balance);
        }
        printf("Hit Enter key to continue: ");
        _getch();
}
}
}

0 个答案:

没有答案