Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP 1010 -A01

Introduction to Computer Science 1

Summer 2022

Assignment 5: Arrays

Q1: War Card game

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 ofcards, 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.

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.

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.

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,

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 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.

(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.