War is the name of a popular card game where two players turn up a card...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
War is the name of a popular card game where two players turn up a card at the same time and the player with the higher card takes both cards and puts them, face down, on the bottom of his stack. The goal is to be the first player to win all 52 cards. In this question, you will implement the logic for the war card game. You have been provided with a template file that does the drawing for you. The drawing is not hard, but you can save time this way (but of course, you draw it if you want!). The template call for functions that were not written yet and it is your job to write those functions and the other ones listed in this question. The game works as follows. You take a full deck of cards (52 cards) with no extra cards. Shuffle the deck. Divide the whole deck out into two players, so that each player has 26 cards. Now we can start the game. To play, both players turn up their top card and show it. The player with the highest card wins (aces are low). If the cards are the same number, then the suit decides (the stronger suit is the winner). The order (strongest to weakest) is: 1st: Hearts (H) 2nd: Diamonds (D) 3rd: Spades (S) 4th: Clubs (C) There is never a tie. The winner takes both cards and puts them on the bottom of their deck, and another round is played. This is played until one of the players runs out of cards, which means that player is the loser. This assignment is about using arrays and of course, you will use everything else that you have learned in the course. 1 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 How to start? This question provides the required functions and descriptions, and an overview of some of the global variables (not all) you will need. Initially, type in all your function stubs (empty code, returning anything) and get the program to run. If you are successful, you will see each player with 52 cards on the canvas. SampleSolutionA5Q1 Round: 0 52 A1 A1 A1 A1 52 ☐ × Then work on one piece at a time. The grading is different for this assignment than the others. To maximize your marks, implement the easy functions first (the lower marks) and get them working, then work on the other functions. Provided code in the template: The draw loop has been provided, which continually plays the game as fast as possible until a winner is found. It also accesses the mouse using the function mousePressed. If you click the right mouse button, the simulation pauses. If you click the left mouse button, it executes just one round. If you click the right mouse button again, the fast simulation is continued. Playing Cards: Each card have a number from 1-13 (A, 2..10, J, Q, K), and a suit (Hearts, Diamonds, Spades, or Clubs). You can store cards nicely inside computers with a few tricks: • • • Each card has a unique number, from 0..51 (52 cards. Remember the first index is always 0). You can get the card suit by taking card/13 with integer division. Cards 0..12 will be suit 0, 13..25 will be suit 1, 26..38 will be suit 2, and the rest suit 3. (4 suits). Suit is card/13. You can get the actual card number by taking card%13. The first 13 will be 0..12, 13..25 will also be 0..12, and so forth. Number is card%13. 2 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 Array of Cards: The use of arrays is the challenging part of this question. You will use arrays to store collections of cards. For example, at the beginning, you generate the card deck into one array. Each player's own deck (their hand of cards) will have its own array. These arrays store integers, and each bin(user) can store an integer to represent the unique card (e.g., 27). Remember you can convert this card to a suit and number as per above. When you deal the cards to the players, you will add each player's cards into their respective arrays. Partially-filled arrays: your arrays will often be partially filled. For example, if you create an array to store the players deck with 52 bins (they may collect all the cards!!) but they only have 10 cards, what goes in the other bins? You need to set these bins to impossible values. Give this a name as a final constant, e.g., NO_CARD, and set it to an impossible value (e.g., outside the range 0..51, maybe -1?). Maintaining the arrays: As the game plays, players constantly lose and gain cards. They take cards off the top of their decks. They place cards on the bottom. It is important to keep the card order. This is the core tough part of the question. In programming, when you come across a tough part like this, a good strategy is to wrap the logic into safe functions. If you implement those functions, then everything else becomes easy. In this case, you will build addCardToBottom, getTopCard, removeTopCard. If you can solve those three problems, the job is done! The idea behind this is that, for each player, you will have: an array to store their deck of cards, an integer pointing at the current top card, and an integer pointing at the first empty slot at the bottom of the deck. Look at the following diagram. * means empty (impossible value). Thisarray has 5 cards stored and a range of empty bins. * * * * 43 8 21 51 1 * * * * top bottom The arrows represent integers you keep track of that tell you which bin is the top or bottom. You will be putting cards from left to right, and you add cards to the bottom, so the top of the deck is on the left. When you add a new card, you put it in the bottom bin, and move along that pointer to the next empty bin. When you take a card off the top, you get the number (43 in this case), replace it with impossible value (*), and move the pointer along. When the arrows hit the end of the array you just wrap around using mod. This is detailed in the functions below. Global variables: In addition to the drawing-related global variables already provided, you will need (at least) the following: a final integer representing NO_CARD, final String arrays representing the card numbers (“A”, “K”,”Q”, “J”, “10”, “9”, “8”, “7”, “6”, “5”, “4”, “3”, “2”) and ("C","S","D","H") for suits. You will need the following non-final variables - player 1 hand, 3 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 player 2 hand (make them capable of holding the whole deck), integers for the tops and bottoms of the decks, and an integer counting how many rounds have passed. (2 marks) globals, flow and play - if your program doesn't run, this is 0. You will lose marks here for small logic issues like off by 1, etc., that make the overall game work incorrectly. Functions: You must implement the following functions: (1 marks) generateDeck - takes no parameters but returns a new array, size 52, representing a clean freshly-opened deck of cards. The array will have the numbers 0..51 stored in bins 0..51 (use a for loop). Try printing out the array for debugging purposes. (1.5 marks) shuffleDeck - takes a card deck as the parameter and shuffles it. A good way to do this is to go through each card in the deck (use a for loop) and swap it with a random card in the deck. For debugging, try printing out the array and checking that you do not have duplicates. (1 marks) resetHand 1 – takes a hand (integer array), returns nothing. Sets every bin in the array to have no card in it (use your final constant and loop set the hand to no card). (1 marks) countCards - takes a hand (integer array) and returns an integer representing how many cards are in the array. Use a for loop to go over each one and count how many are not set to no card. (3 marks) addCardToBottom - takes a hand (integer array), a bottom pointer (int), and a card (int). Returns the new bottom location. Since the current bottom pointer should point to an empty bin (i.e. empty array), put the card there. Move the bottom counter right by one, and then mod the array length so that it wraps around if needed. Return the new bottom pointer. You do not need to use a loop in this function. For debugging, before doing this, ensure that the bottom pointer points to a bin (array) with no card in it. If there is a card there- this means either your array is full, or, your bottom pointer is wrong. If you try to add a card using this function and print out the return from this function, it should return the next bottom (index) since you have added a card on the previous index; - (2 marks) resetDecks takes no parameters and returns nothing, works on global variables. Calls generateDeck to make a fresh deck, and shuffleDeck to shuffle it. It calls resetHand twice to reset the two player hands. Sets the top and bottom pointers to 0 (the start of the hand in the array). Finally, distributes the cards from the deck into the hands. There are many ways to do this- since the deck is already shuffled, as long as you get 26 into each hand you are fine. 4 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 (3 marks) getTopCard - takes a hand (integer array) and a top pointer (int), and returns the card. It looks in the array at the top bin and gets the card out, and returns this card. For debugging, check to make sure there is a card there. This should only happen if the array is empty, or you have a bug. (3 marks) removeTopCard - takes a hand (integer array), and a top pointer (int). After the top card is removed, it returns the new top pointer. You should check that there is actually a card to remove, and print out an error message if not (helps with debugging). Set the current top card bin in the array to have no card. Move the top pointer right by one: add one. Mod the array length to make it wrap around if you go off the edge. Return the new top pointer. (.5 marks) getSuit - takes a card, and returns a number 0..3 representing the suit. Basically card/13. (.5 marks) getNumber - takes a card, and returns a number 0..12 representing the card number. Basically card%13. (2 marks) getText – takes a card and returns a string with two characters: the first character is the card number ("A","1",...,"K"), and the second is the suit letter. DO NOT USE AN IF STATEMENT IN THIS METHOD. The way to do it is to get the card number and suit and use these as indexes into your global string arrays for the card numbers and suits. (2 marks) isBetter - takes two cards (as integers) and returns a boolean telling you if the first card is better than the second. Use getNumber and getSuit on the cards first to get their suits and numbers. If the first card is larger, then it's better, if the second is larger, then it's better. In the case of a tie, compare the suits as explained earlier (hearts beats diamonds beats spades beats clubs). (2.5 marks) doRound - takes no parameters and returns nothing. Works on global variables and own local variables. This is the core logic of the game. First, get the top cards from the players' decks and remove them from those decks. (use getTopCard and removeTopCard for this). Call isBetter to find out who won. You will place the two cards on the bottom of the winner's deck using addCardToBottom, however, IMPORTANT: put the two cards back on the deck in random order. Flip a coin using random, and use this to determine the order. At the end of this function increase the number of rounds. 5 War is the name of a popular card game where two players turn up a card at the same time and the player with the higher card takes both cards and puts them, face down, on the bottom of his stack. The goal is to be the first player to win all 52 cards. In this question, you will implement the logic for the war card game. You have been provided with a template file that does the drawing for you. The drawing is not hard, but you can save time this way (but of course, you draw it if you want!). The template call for functions that were not written yet and it is your job to write those functions and the other ones listed in this question. The game works as follows. You take a full deck of cards (52 cards) with no extra cards. Shuffle the deck. Divide the whole deck out into two players, so that each player has 26 cards. Now we can start the game. To play, both players turn up their top card and show it. The player with the highest card wins (aces are low). If the cards are the same number, then the suit decides (the stronger suit is the winner). The order (strongest to weakest) is: 1st: Hearts (H) 2nd: Diamonds (D) 3rd: Spades (S) 4th: Clubs (C) There is never a tie. The winner takes both cards and puts them on the bottom of their deck, and another round is played. This is played until one of the players runs out of cards, which means that player is the loser. This assignment is about using arrays and of course, you will use everything else that you have learned in the course. 1 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 How to start? This question provides the required functions and descriptions, and an overview of some of the global variables (not all) you will need. Initially, type in all your function stubs (empty code, returning anything) and get the program to run. If you are successful, you will see each player with 52 cards on the canvas. SampleSolutionA5Q1 Round: 0 52 A1 A1 A1 A1 52 ☐ × Then work on one piece at a time. The grading is different for this assignment than the others. To maximize your marks, implement the easy functions first (the lower marks) and get them working, then work on the other functions. Provided code in the template: The draw loop has been provided, which continually plays the game as fast as possible until a winner is found. It also accesses the mouse using the function mousePressed. If you click the right mouse button, the simulation pauses. If you click the left mouse button, it executes just one round. If you click the right mouse button again, the fast simulation is continued. Playing Cards: Each card have a number from 1-13 (A, 2..10, J, Q, K), and a suit (Hearts, Diamonds, Spades, or Clubs). You can store cards nicely inside computers with a few tricks: • • • Each card has a unique number, from 0..51 (52 cards. Remember the first index is always 0). You can get the card suit by taking card/13 with integer division. Cards 0..12 will be suit 0, 13..25 will be suit 1, 26..38 will be suit 2, and the rest suit 3. (4 suits). Suit is card/13. You can get the actual card number by taking card%13. The first 13 will be 0..12, 13..25 will also be 0..12, and so forth. Number is card%13. 2 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 Array of Cards: The use of arrays is the challenging part of this question. You will use arrays to store collections of cards. For example, at the beginning, you generate the card deck into one array. Each player's own deck (their hand of cards) will have its own array. These arrays store integers, and each bin(user) can store an integer to represent the unique card (e.g., 27). Remember you can convert this card to a suit and number as per above. When you deal the cards to the players, you will add each player's cards into their respective arrays. Partially-filled arrays: your arrays will often be partially filled. For example, if you create an array to store the players deck with 52 bins (they may collect all the cards!!) but they only have 10 cards, what goes in the other bins? You need to set these bins to impossible values. Give this a name as a final constant, e.g., NO_CARD, and set it to an impossible value (e.g., outside the range 0..51, maybe -1?). Maintaining the arrays: As the game plays, players constantly lose and gain cards. They take cards off the top of their decks. They place cards on the bottom. It is important to keep the card order. This is the core tough part of the question. In programming, when you come across a tough part like this, a good strategy is to wrap the logic into safe functions. If you implement those functions, then everything else becomes easy. In this case, you will build addCardToBottom, getTopCard, removeTopCard. If you can solve those three problems, the job is done! The idea behind this is that, for each player, you will have: an array to store their deck of cards, an integer pointing at the current top card, and an integer pointing at the first empty slot at the bottom of the deck. Look at the following diagram. * means empty (impossible value). Thisarray has 5 cards stored and a range of empty bins. * * * * 43 8 21 51 1 * * * * top bottom The arrows represent integers you keep track of that tell you which bin is the top or bottom. You will be putting cards from left to right, and you add cards to the bottom, so the top of the deck is on the left. When you add a new card, you put it in the bottom bin, and move along that pointer to the next empty bin. When you take a card off the top, you get the number (43 in this case), replace it with impossible value (*), and move the pointer along. When the arrows hit the end of the array you just wrap around using mod. This is detailed in the functions below. Global variables: In addition to the drawing-related global variables already provided, you will need (at least) the following: a final integer representing NO_CARD, final String arrays representing the card numbers (“A”, “K”,”Q”, “J”, “10”, “9”, “8”, “7”, “6”, “5”, “4”, “3”, “2”) and ("C","S","D","H") for suits. You will need the following non-final variables - player 1 hand, 3 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 player 2 hand (make them capable of holding the whole deck), integers for the tops and bottoms of the decks, and an integer counting how many rounds have passed. (2 marks) globals, flow and play - if your program doesn't run, this is 0. You will lose marks here for small logic issues like off by 1, etc., that make the overall game work incorrectly. Functions: You must implement the following functions: (1 marks) generateDeck - takes no parameters but returns a new array, size 52, representing a clean freshly-opened deck of cards. The array will have the numbers 0..51 stored in bins 0..51 (use a for loop). Try printing out the array for debugging purposes. (1.5 marks) shuffleDeck - takes a card deck as the parameter and shuffles it. A good way to do this is to go through each card in the deck (use a for loop) and swap it with a random card in the deck. For debugging, try printing out the array and checking that you do not have duplicates. (1 marks) resetHand 1 – takes a hand (integer array), returns nothing. Sets every bin in the array to have no card in it (use your final constant and loop set the hand to no card). (1 marks) countCards - takes a hand (integer array) and returns an integer representing how many cards are in the array. Use a for loop to go over each one and count how many are not set to no card. (3 marks) addCardToBottom - takes a hand (integer array), a bottom pointer (int), and a card (int). Returns the new bottom location. Since the current bottom pointer should point to an empty bin (i.e. empty array), put the card there. Move the bottom counter right by one, and then mod the array length so that it wraps around if needed. Return the new bottom pointer. You do not need to use a loop in this function. For debugging, before doing this, ensure that the bottom pointer points to a bin (array) with no card in it. If there is a card there- this means either your array is full, or, your bottom pointer is wrong. If you try to add a card using this function and print out the return from this function, it should return the next bottom (index) since you have added a card on the previous index; - (2 marks) resetDecks takes no parameters and returns nothing, works on global variables. Calls generateDeck to make a fresh deck, and shuffleDeck to shuffle it. It calls resetHand twice to reset the two player hands. Sets the top and bottom pointers to 0 (the start of the hand in the array). Finally, distributes the cards from the deck into the hands. There are many ways to do this- since the deck is already shuffled, as long as you get 26 into each hand you are fine. 4 ASSIGNMENT 5 DEPARTMENT AND COURSE NUMBER: COMP 1010 (3 marks) getTopCard - takes a hand (integer array) and a top pointer (int), and returns the card. It looks in the array at the top bin and gets the card out, and returns this card. For debugging, check to make sure there is a card there. This should only happen if the array is empty, or you have a bug. (3 marks) removeTopCard - takes a hand (integer array), and a top pointer (int). After the top card is removed, it returns the new top pointer. You should check that there is actually a card to remove, and print out an error message if not (helps with debugging). Set the current top card bin in the array to have no card. Move the top pointer right by one: add one. Mod the array length to make it wrap around if you go off the edge. Return the new top pointer. (.5 marks) getSuit - takes a card, and returns a number 0..3 representing the suit. Basically card/13. (.5 marks) getNumber - takes a card, and returns a number 0..12 representing the card number. Basically card%13. (2 marks) getText – takes a card and returns a string with two characters: the first character is the card number ("A","1",...,"K"), and the second is the suit letter. DO NOT USE AN IF STATEMENT IN THIS METHOD. The way to do it is to get the card number and suit and use these as indexes into your global string arrays for the card numbers and suits. (2 marks) isBetter - takes two cards (as integers) and returns a boolean telling you if the first card is better than the second. Use getNumber and getSuit on the cards first to get their suits and numbers. If the first card is larger, then it's better, if the second is larger, then it's better. In the case of a tie, compare the suits as explained earlier (hearts beats diamonds beats spades beats clubs). (2.5 marks) doRound - takes no parameters and returns nothing. Works on global variables and own local variables. This is the core logic of the game. First, get the top cards from the players' decks and remove them from those decks. (use getTopCard and removeTopCard for this). Call isBetter to find out who won. You will place the two cards on the bottom of the winner's deck using addCardToBottom, however, IMPORTANT: put the two cards back on the deck in random order. Flip a coin using random, and use this to determine the order. At the end of this function increase the number of rounds. 5
Expert Answer:
Answer rating: 100% (QA)
Solutions Heres the best way to solve it import javautilRandom impor... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Googles ease of use and superior search results have propelled the search engine to its num- ber one status, ousting the early dominance of competitors such as WebCrawler and Infos- eek. Even later...
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
A mutual fund has 20 stocks in its portfolio. On a given day 9 stocks move up, 3 stay the same, and 8 move down. In how many ways could this happen?
-
On May 8, 2016, Holly purchased a residential apartment building. The cost basis assigned to the building is $800,000. Holly also owns another residential apartment building that she purchased on...
-
Yuri manages 150 apartments in Irvine, California. All the apartments can be rented at a price of $1,200 per month each, but for each $100 increase in the monthly rent, there will be five additional...
-
Match the measures of worth in the first column with the appropriate unit of measure that results from the analysis. Measure of Worth (a) Annual Worth (b) External Rate of Return (c) Future Worth (d)...
-
Mark Corporation estimates its manufacturing overhead to be $90,000 and its direct labor costs to be $200,000 for year 1. The actual direct labor costs were $50,000 for Job 301, $75,000 for Job 302,...
-
3. (5 Marks) An engineering project requires $25,000 as first cost and has a planning horizon of six years. Operating costs are $2,300 per year, and expected annual revenue is $9,200 (both real...
-
John, a Newyork man, works just across the border in New Jersey. John rear-ends Sheila, a new jersey woman right as John is approaching his new jersey office. Sheila sues John. Is it likely that a...
-
Assessment: Describe how the transition team plans to monitor, assess, and evaluate the established goals. What processes and strategies will be put into place to implement these assessments?...
-
(CO 3) The average area of a bedroom in a home is 219 square feet with a standard deviation of 32.5 square feet. What is the largest a bedroom can be and be among the smallest 15% of bedrooms in a...
-
A bowling lane is 60 feet long. If a bowling ball isreleased at t = 0 and reaches thepins 2.5 seconds later, what is its average speed?
-
What happens when you ask friends, family or classmates about what you could do differently to be a better listener? What communications challenges do you tend to have with everyone? What will you...
-
Explain the central conundrum in "What's a Fair Share in the Age of Google?" Is there an ethical problem with this, or should everyone just adjust to the new paradigm for distributing information?
-
Evrnts of Mitosis Instructions: Copy and paste each of these events onto the slide with the phase in which it occurs. Chromatin condenses into chromosomes. Nucleus reforms around chromosomes. DNA is...
-
What is beacon marketing? What are digital wallets?
-
Create an application that contains an enumeration that represents the days of the week. Display a list of the days, and then prompt the user for a day. Display business hours for the chosen day....
-
The arguments required by the font constructor include all of the following except ____________. A. Typeface b. Mode c. Style d. Point size
-
Write a program that calculates and displays the conversion of an entered number of dollars into currency denominations20s, 10s, 5s, and 1s. Save the program as Dollars.java.
-
Many long-term loans are payable over a period of time. For example, when a business takes out a mortgage to finance a building, it pays off a fraction of that mortgage every month. Required: What...
-
In this chapter, we said that the quick ratio is a better measure of liquidity than the current ratio because the quick ratio includes only those current assets that may be easily converted to cash....
-
Fredski Coaching School had accounts receivable of $30 000 at the beginning of the year and $60 000 at the year end. The revenue for the year was $120 000. How much cash did Fredski collect from his...
Study smarter with the SolutionInn App