我只需要重新开始吗?

时间:2015-12-02 14:31:58

标签: data-structures module linked-list

请把它简单明了地告诉我。我试图用一个哨兵节点来实现一个循环的双向链表,然后想出了一些向前或向后的游戏,并且通过玩家循环。它工作正常....除了我意识到我必须有两个单独的文件用于环模块(.h和.c),然后是一个单独的主文件。代码很长,在我意识到我的错误之后我已经整理了它,因为它毫无意义。所以我不要求你阅读它或检查错误或任何东西。但是如果你能够以1-10的比例告诉我,只要略过它就会有多糟糕,我将非常感激。只是这样我才能理解我将要做的事情...谢谢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define PLAYERLIMIT 5

//Doubly linked list implementation
struct Node {
    char data[20];
    struct Node* next;
    struct Node* prev;
};
struct Node* sentinel; //global pointer to the sentinel node
struct Node* head;
struct Node* tail;

// Create sentinel node, originally it just points to itself

struct Node* MakeSentinel () {
struct Node* SentinelNode =
(struct Node*)malloc(sizeof(struct Node));
SentinelNode->next = SentinelNode->next;
SentinelNode->prev = SentinelNode->prev;
return SentinelNode;
}

//Creation of a node takes an int and returns a node
struct Node* GetNewNode (char *x) {
    struct Node* newNode =
    (struct Node*)malloc(sizeof(struct Node)); //created node in the dynamic memory
strcpy (newNode->data, x); //temp->data is same as (*temp).data
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

//Returning a pointer to newly created node, inserts next to sentinel
void InsertAtHead (char *x){
    struct Node* newNode = GetNewNode(x);
if (sentinel == NULL) {
    sentinel = MakeSentinel();
    head = newNode;
    sentinel->next = head;
    sentinel->prev = head;
    head->next = sentinel;
    head->prev = sentinel;
    return;
}
head->prev = newNode;
newNode->next = head;
newNode->prev = sentinel;
sentinel->prev = newNode;
head = newNode;
}

void PrintHead() {
//    printf("Sentinel prev is %s\n", sentinel->prev->data);
struct Node* temp = sentinel->prev;
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("The players in the game are\n\n");
while (temp != sentinel) {
    printf ("%s ", temp->data);
    temp = temp->next;

}
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}

//Removing a name from the linked list
void DeleteEntry (struct Node* del) {
    if ((del->next != sentinel) && (del->prev != sentinel)) {
del->next->prev = del->prev;
del->prev->next = del->next;
}
else if (del->next == sentinel) {
    del->next->next = del->prev;
    del->prev->next = del->next;
}
else if(del->prev == sentinel) {
    del->next->prev = del->prev;
    del->prev->prev = del->next;
}
//    printf("Sentinel prev is now%s\n", sentinel->prev->data);

return;
}



int main(int argc, char *argv[]) {

//Entering all player names into the doubly linked list
printf ("Enter the names of the players. Press Enter after each new player\n");
printf ("Must have 5 Players'.'\n\n");

int i = 1;
char name[20];


while ((i <= PLAYERLIMIT)) {

    printf("Player: ");
    scanf ("%s", name);
    InsertAtHead(name);
    i++;

}
PrintHead();

//Starting the Game

//Initialising Variables for the game
int nextCard;
int currentCard;
int score;
char oppositeGuess[20];
int userChoice;
int playGame = 1;
struct Node* CurrentPlayer = head;
struct Node* PlayerBefore;

//Setting up the random cards
int range;
srand(time(NULL));
range = (13 - 1) + 1;

nextCard = rand() % range + 2;
currentCard = rand() % range + 2;

while (playGame == 1) {

    //Change current card to past card before creating a new current card
    currentCard = nextCard;

    //generate a random int for card

    nextCard = rand() % range + 2;

    if (currentCard < 11) {
        printf("\nThe current card is a %d.\n", currentCard);
    }
    else if (currentCard == 11) {
        printf("\nThe current card is a jack.\n");
    }
    else if (currentCard == 12) {
        printf("\nThe current card is a queen.\n");
    }
    else if (currentCard == 13) {
        printf("\nThe current card is a king.\n");
    }
    else if (currentCard == 14) {
        printf("\nThe current card is an ace.\n");
    }

    printf ("***%s it is your go!***\n", CurrentPlayer->data);
    if (CurrentPlayer->prev != sentinel) {
        PlayerBefore = CurrentPlayer->prev;

    }
    else {
        PlayerBefore = sentinel->next;

    }
//        printf("\nThe CurrentPlayer is %s\n", CurrentPlayer->data);
//        printf("The PlayerBefore is %s\n\n", PlayerBefore->data);
    printf("Will the next card be higher(1) or lower(2)?\n");
    scanf("%d", &userChoice);
    printf("\n");
    printf ("***%s would you like to guess the opposite?***\n", PlayerBefore->data);
    scanf("%s", oppositeGuess);
    if (strncmp(oppositeGuess, "Yes", 4) == 0) {

        if (userChoice == 1) {
            if (currentCard < nextCard) {
                printf("\nSorry, %s was correct. You are out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", PlayerBefore->data);
                DeleteEntry(PlayerBefore);

            }
            else if (currentCard > nextCard) {
                printf ("Congratulations! player %s was wrong and is now out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);

            }
            else if (currentCard == nextCard){
                printf("\nCards were equal. Next players turn.\n");
            }
        }
        else if (userChoice == 2) {
            if (currentCard < nextCard) {
                printf("Congratulations! player %s was wrong and is now out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);

            }
            else if (currentCard > nextCard) {
                printf ("\nSorry, %s was correct. You are out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", PlayerBefore->data);
                DeleteEntry(PlayerBefore);

            }
            else if (currentCard == nextCard){
                printf("\nCards were equal. Next players turn.\n");
            }
        }
    }
    if (strncmp(oppositeGuess, "No", 4) == 0) {

        if (userChoice == 1) {
            if (currentCard > nextCard) {
                printf ("\nSorry you have guessed incorrectly, you are out!\n");
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);

            }

            else if (currentCard < nextCard) {
                printf("\nCongratualtions you were correct, next players turn.\n");
            }
            else if (currentCard == nextCard) {
                printf("\nThe cards are the same. Next players turn.\n");
            }
        }
        else if (userChoice == 2) {
            if (currentCard > nextCard) {
                printf ("\nCongratualtions you were correct, next players turn.\n");

            }
            else if (currentCard < nextCard) {
                printf("\nSorry you have guessed incorrectly, you are out!\n");
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);


            }
            else if (currentCard == nextCard) {
                printf("\nThe cards are the same. Next players turn.\n");
            }
        }
        else {
            printf("\nPlease enter a valid choice.\n");
        }
    }

        PrintHead();
    if (CurrentPlayer->next != sentinel) {
        CurrentPlayer = CurrentPlayer->next;
    }
    else {
        CurrentPlayer = sentinel->prev;
    }

    if ((CurrentPlayer->next == sentinel) && (CurrentPlayer->prev == sentinel)) {
        playGame = 0;
    }
}
printf("%s you are the Winner!\n", CurrentPlayer->data);
}

1 个答案:

答案 0 :(得分:0)

在1-10的范围内,我认为这段代码值得保存:您已经进行了相当繁琐的练习(制作循环列表)并将其转化为有趣的东西。

对于不同的模块使用单独的文件没有问题,您可以使用#include命令包含它们。例如,如果您将所有链接列表内容放在mydllist.c

//Doubly linked list implementation
struct Node {
  char data[20];
  struct Node* next;
  struct Node* prev;
};

struct Node* sentinel; //global pointer to the sentinel node
struct Node* head;
struct Node* tail;

// Create sentinel node, originally it just points to itself

struct Node* MakeSentinel () {
  struct Node* SentinelNode = (struct Node*)malloc(sizeof(struct Node));
  SentinelNode->next = SentinelNode->next;
  SentinelNode->prev = SentinelNode->prev;
  return SentinelNode;
}

//Creation of a node takes an int and returns a node
struct Node* GetNewNode (char *x) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //created node in the dynamic memory
    strcpy (newNode->data, x); //temp->data is same as (*temp).data
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

//Returning a pointer to newly created node, inserts next to sentinel
void InsertAtHead (char *x){
    struct Node* newNode = GetNewNode(x);
    if (sentinel == NULL) {
      sentinel = MakeSentinel();
      head = newNode;
      sentinel->next = head;
      sentinel->prev = head;
      head->next = sentinel;
      head->prev = sentinel;
      return;
    }

 head->prev = newNode;
 newNode->next = head;
 newNode->prev = sentinel;
 sentinel->prev = newNode;
 head = newNode;
}

void PrintHead() {
//    printf("Sentinel prev is %s\n", sentinel->prev->data);
  struct Node* temp = sentinel->prev;
  printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  printf("The players in the game are\n\n");
  while (temp != sentinel) {
    printf ("%s ", temp->data);
    temp = temp->next;

  }
  printf("\n");
  printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}

//Removing a name from the linked list
void DeleteEntry (struct Node* del) {
    if ((del->next != sentinel) && (del->prev != sentinel)) {
      del->next->prev = del->prev;
      del->prev->next = del->next;
    }
    else if (del->next == sentinel) {
      del->next->next = del->prev;
      del->prev->next = del->next;
    }
    else if(del->prev == sentinel) {
      del->next->prev = del->prev;
      del->prev->prev = del->next;
    }
    //    printf("Sentinel prev is now%s\n", sentinel->prev->data);

    return;
}

您可以将它包含在dllist_game.c文件的顶部,如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "mydllist.c"

然后我开始做一些简单的重构,特别是main()函数,以使代码更容易理解。

与您的#define PLAYERLIMIT 5一起,我添加#define NUMBER_OF_CARDS 13(或无论最大卡数是多少)以及布尔值的枚举类型

typedef enum {true, false} bool;

当你定义范围而不是写作时这样:

int range;
srand(time(NULL));
range = (13 - 1) + 1 //n.b. (13 - 1) + 1 = 13 is there some reason you wrote it like this?

你可以写

int range = NUMBER_OF_CARDS;
srand(time(NULL));

将变量初始化为默认值始终是一个好主意:您可能会忘记它们未初始化并尝试使用它们(这可能很糟糕):

int nextCard = 0;
int currentCard = 0;
int score = 0;
char oppositeGuess[20] = "";
int userChoice = 0 ;
bool playGame = true;
struct Node* CurrentPlayer = head;
struct Node* PreviousPlayer = head->prev;

我将循环计数器的名称从i更改为num_entered_players,以便在使用该变量时更容易查看该变量的用途:

int num_entered_players = 1;
char name[20];

while ((entered_players <= PLAYERLIMIT)) {
    printf("Player: ");
    scanf ("%s", name);
    InsertAtHead(name);
    num_entered_players++;
}

最后,我开始将大while循环的一部分提取到较小的函数中,这些函数描述了正在发生的事情。例如:

if (currentCard < 11) {
    printf("\nThe current card is a %d.\n", currentCard);
}
else if (currentCard == 11) {
    printf("\nThe current card is a jack.\n");
}
else if (currentCard == 12) {
    printf("\nThe current card is a queen.\n");
}
else if (currentCard == 13) {
    printf("\nThe current card is a king.\n");
}
else if (currentCard == 14) {
    printf("\nThe current card is an ace.\n");
}

会变成:

void printCurrentCard(int currentCard){

  if (currentCard < 11) {
        printf("\nThe current card is a %d.\n", currentCard);
    }
    else if (currentCard == 11) {
        printf("\nThe current card is a jack.\n");
    }
    else if (currentCard == 12) {
        printf("\nThe current card is a queen.\n");
    }
    else if (currentCard == 13) {
        printf("\nThe current card is a king.\n");
    }
    else if (currentCard == 14) {
        printf("\nThe current card is an ace.\n");
    }   
}

并会像这样调用:

while (playGame == true) {

    //Change current card to past card before creating a new current card
    currentCard = nextCard;

    //generate a random int for card
    nextCard = rand() % range + 2;

    printCurrentCard(currentCard);

....
}

等。因此,如果您在特定步骤中更改了游戏规则,那么您只需要一个地方来更改代码 - 它不应该影响游戏的其余部分。

一旦你完成了这些改变 - 游戏的逻辑可以进入另一对.c和.h文件,你可以直接从一个单独的main()调用它!