COP3502C Computer Science 1 Dr. Andrew Steinberg Spring 2022 Programming Assignment 3 The Card Game of War Max Points 100 Due: 3/14/2022 at 11:59pm Background Story of this Assignment You have just...

1 answer below »
The Main is already filled in. All that has to be done is finish around 9 functions. Oh, and I would love if Vaibhav worked on this assignment. Thank You!


COP3502C Computer Science 1 Dr. Andrew Steinberg Spring 2022 Programming Assignment 3 The Card Game of War Max Points 100 Due: 3/14/2022 at 11:59pm Background Story of this Assignment You have just learned your first elementary data structure (woohoo!). Something that I stressed in lecture that you will see throughout your CS classes is that many data structures can be derived from the linked list. Therefore, I believe the first data structure programming assignment should revolve around the linked list for many reasons. One reason is that you will get the chance to apply the linked list in an application scenario. Another reason and perhaps one of the most important ones is that you will get a fundamental understanding of implementing data structures in C by coding up linked lists. Have fun and start early (seriously start early)! Make sure to see the TAs and ULAs for help! Assignment Details For this programming assignment, you will get to simulate the classic card game war using a singly linked list. Rules for the game can be found at this website: https://gamerules.com/rules/war-card-game/ In this program we will always assume only two players will play. This means each player will represent its own linked list (that’s right two separate linked lists). As the rules state there are a total of 52 cards. The following number list shows each set of cards in the deck. Each type contains 4 (♠, ♣, ♥, and ♦) cards (hence 4 ∗ 13 = 52 cards total). 1. Ace 2. King 3. Queen 4. Jack 5. 10 6. 9 7. 8 8. 7 9. 6 10. 5 11. 4 12. 3 13. 2 Also, the order presented shows the order of card dominance (top being the highest and bottom being the lowest). This will determine which player wins after cards are drawn. For example, if Player A draws the card king of hearts and Player B draws the card 9 of clubs, then Player A wins that round. As a result, Player A takes its own card drawn and Player B’s card and puts it in the back of the pile (sounds like inserting the back of a linked list ). Now as the rules state, the objective of the game is to get the player to lose all their cards (this sounds like until the respective player’s linked list is empty ). https://gamerules.com/rules/war-card-game/ COP3502C Computer Science 1 Dr. Andrew Steinberg Spring 2022 The Provided Skeleton File You were provided with a skeleton C file that has the main function and function prototypes. This section will discuss the lines of code provided for you in the main function to assist you with understanding how the code will execute. • Lines 6 – 8 shows the preprocessor directives. • Lines 10 – 14 defines the typedef struct called card_t you will be utilizing in the assignment. The struct contains the following member components. o An integer called rank. This will keep track of the priority of the card. This is utilized in determining the winning round when cards are drawn. o A char pointer called type. This is a dynamic string. It will store the card name. Examples include “8 of Spades”, “6 of Hearts”, “Ace of Hearts”, etc… o A struct pointer called nextptr that points to a cart_t struct. This is simply the node in the linked list. • Line 33-37 sets the seed. At this point in the course you should know what that does. The test script uses the value 0 as the test seed. • Line 38 is a call to a user-defined function called rules. This function has already been implemented for you since it is just printf statements . • Lines 40 and 41 are just variable declarations. Variable player represents the player you get to choose. You can either be player 1 or 2. This is done in lines 43 – 45. Variable result, will determine which player won in displaying the respective message. You will be playing against the computer. • Lines 47 – 58 shows a for loop that simulates a game. It will keep track which game number the simulation is playing. It should only play up to 5 games total. • Line 52 calls the function playRound which simulates an entire game. The function returns an integer that is stored in result. This value is used to determine to proper message to display to the terminal window based on the player you pick at the beginning of the simulation run. The Function Prototypes You were provided with a skeleton C file that has the main function and function prototypes. This section will discuss the lines of code provided for you in the main function to assist you with understanding how the code will execute. DO NOT CHANGE THE PROTOTYPES! Points will be deducted. You can add additional helper functions as long as they are not called in main and doesn’t affect the overall output. void rules(); The rules function will display the rules of the game. It is already implemented for you so you do not need to change anything for this function. COP3502C Computer Science 1 Dr. Andrew Steinberg Spring 2022 int playRound(); The playRound function will simulate an ENTIRE round of the card game. This function will call the other functions (mentioned below) that are going to be mentioned in order to simulate the game. It takes no parameters and returns an integer value (1 or 2) that determines which player won the round. The part that determines which player won is already implemented for you in the main function. IMPORTANT! Something important you will need to consider is the setup of the round. You will have a deck that is already setup as a linked list from the openCardDeck function, however it is not randomize (it is in top-down order of rankings). You will need to randomize the cards distributed to each player. Hint: Think about the search, deckSize, insertBackDeck, and removeDeck functions can properly pick a random card from the deck along with removing from the original setup deck. ANOTHER SUPER IMPORTANT ITEM! When cards are drawn, and a winner is determined. Take the cards in the following order always when inserting back into a respective player’s linked list. Player 1’s card(s) go first and then Player 2’s card(s) go last. The following figure shows the order to insert back into the winner’s deck for the W-A-R scenario. card_t * openCardDeck(); The openCardDeck function will setup the round by “opening the card deck pack”. The function will open a text file (which is provided for you in the text file on Webcourses) called deck.txt and read the information to store in a linked list. You should see a user defined function called insertBackSetup being invoked. This function was provided for you already. There are no parameters however the function returns an address to a typedef struct card_t. This address is the head of a linked list. This linked list represents the whole deck you open from the container. It should contain 52 nodes. This function has been implemented for you. You do not need to make any changes or additions to this implementation. It is here to assist you with the game setup. card_t * insertBackSetup(card_t *node, char *name, int cardrank); The insertBackSetup function will insert a card to the back of the original deck for the setup purposes. The function takes three arguments. The first argument is an address to a typedef struct card_t which represents the head of the linked list that represents the original deck before being split between the players. The second argument is the name of the card (examples include “Ace of Hearts”, “2 of Clubs”, etc…). The last argument is the rank of card that will be used for the game to determine who wins each card draw. The function returns an address to a typedef struct card_t that represents the head of a linked list. You do not need to make any changes or additions to this implementation. It is here to assist you with the game setup. Player 2 card 2 faced down. Player 2 card 3 faced down. Player 1 card 2 faced down. Player 1 card 3 faced down. Player 2 card 1 faced up. Player 2 card 1 faced down. Player 1 card 2 faced up. Player 2 card 2 faced up. Player 1 card 1 faced up. Player 1 card 1 faced down. COP3502C Computer Science 1 Dr. Andrew Steinberg Spring 2022 int empty(card_t * node); The empty function will determine if the linked list is empty. It takes a reference to a typedef struct card_t and returns an integer that determines if the linked list is empty or not. You do not need to make any changes or additions to this implementation. The function has already been implemented for you. It is here to assist you with the game implementation. void cleanUp(card_t * head); The cleanUp function will free the allocated memory used. This is important to implement to prevent memory leaks since you will be utilizing dynamic memory. It takes a reference to a head of a linked list and will free all the memory allocated in the program run contained in the linked list. The function does not return anything. Make sure you are properly freeing memory by utilizing valgrind. int deckSize(card_t * head); The deckSize function will count the number of cards in the linked list. The functions takes a reference to the head of the linked list and returns the number of cards in the linked list. card_t * search(card_t * node, int spot); The search function will traverse the linked list for a specific card in the pile. The function takes two arguments. The first argument is the reference to the linked list and an integer which is the location of the list called spot. The location starts with 0 (like indexing with an array) and goes up to the total number
Answered 9 days AfterFeb 23, 2022

Answer To: COP3502C Computer Science 1 Dr. Andrew Steinberg Spring 2022 Programming Assignment 3 The Card Game...

Sathishkumar answered on Mar 04 2022
122 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here