請把它給我簡單而簡單。我試圖用一個哨兵節點來實現一個循環雙向鏈表,並且提出了一些更高或更低的遊戲,它們往返前進,並循環播放。它工作正常....除了我意識到我必須有兩個單獨的文件的環模塊(一個.h和一個.c),然後單獨的主文件。代碼很長,在我意識到我的錯誤之後,我沒有整理它,因爲它是毫無意義的。所以即時通訊不要求你閱讀它或檢查錯誤或任何東西。但是如果你能以1-10的分數告訴我這種情況有多糟糕,那麼我會非常感激。只是這樣我就可以讓我的頭周圍的什麼IM將不得不做...謝謝我是否必須重新開始?
#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);
}
我不認爲你需要重新開始:你可以使用這個機會來重構和測試你的代碼。 –