processing代写: Assignment 4: Arrays and Functions

ASSIGNMENT 4: Arrays and Functions DEPARTMENT AND COURSE NUMBER: COMP 1010 COURSE TITLE: Introduction to Computer Science 1 TERM: Fall 2018

Assignment 4: Arrays and Functions

DUE DATE: APRIL 6, 2018 AT 11:59PM

Notes:

  • Name your sketches using your name, the assignment number, and the question number, exactly as in this example: LastnameFirstnameA4Q1.
  • Your programs must run upon download to receive any marks.
  • Submit one PDE file for each question.
  • Assignments must follow the programming standards document published on the course website on UMLearn.
  • After the due date and time assignments may be submitted but will lose 2% of marks per hour late or portion

    thereof.

  • You may submit a question multiple times, but only the most recent version will be marked.
  • These assignments are your chance to learn the material for the exams. Code your assignments independently.

    We use software to compare all submitted assignments to each other, and pursue academic dishonestly vigorously.

    Q1: War

    War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end. However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic for the war card game. You have been provided with a skeleton file that does the drawing for you. The drawing is not hard, but it is tedious, so you can save time this way (but you can spice it up if you want!).

    The game works as follows. You take a full deck of cards
    (52 cards) with no jokers. Shuffle the deck. Deal the whole
    deck out to two players, so that each player has 26 cards. Now you are ready to play.

    To play, both players turn up their top card and show it. The highest card wins (aces are low). If the cards are the same number, then the suit decides. The order (strongest to weakest) is: Hearts, Diamonds, Spades, Clubs. 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 player runs out of cards, which means they are the loser.

    This is a challenging assignment. The hardest part is the techniques you use on the arrays.

    Strategy: I have provided the required functions and descriptions, and an overview of some of the globals you will need. Initially, type in all your function stubs (empty code, returning

1

ASSIGNMENT 4
DEPARTMENT AND COURSE NUMBER: COMP 1010

garbage), and get the thing to run. 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, to maximize part marks.

Provided code: The draw loop has been provided (it’s quite simple), which continually plays game hands as fast as possible until a winner is found. I also use a new way to access the mouse, a function called mousePressed (details in the file). If you click the right mouse button, the simulation pauses. If you click the left mouse button, it executes just one round (very good for debugging!!). If you click the right mouse button again, the fast simulation is continued.

Playing Cards: Playing cards 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)
  • 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 assignment. 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) will have its own array. These arrays store integers, and each bin 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 assignment. 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, then you’re 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). This array has 5 cards stored and a range of empty bins.

*

*

*

*

43

8

21

51

1

*

*

*

*

top
2

bottom

ASSIGNMENT 4
DEPARTMENT AND COURSE NUMBER: COMP 1010

The arrows represent integers you keep track of that tell you which bin is the top or bottom. You will be putting cards in 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.

Globals: In addition to the drawing-related globals already provided, you will need (at least) the following: a final integer representing NO_CARD, final String arrays representing the card numbers (“A”,”1”,…”K” 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.

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 don’t 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).

(2 marks) resetDecks – takes no parameters and returns nothing, works on globals. 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, shuffles 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. Make sure to call addCardToBottom to add the cards, and to store it’s return value as the new bottom pointers.

(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, 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. For debugging, before doing this, ensure that the bottom pointer points to a bin with no card in it. If there is a card there, print out an error message to help you debug – this means either your array is full, or, your bottom pointer is wrong.

3

ASSIGNMENT 4
DEPARTMENT AND COURSE NUMBER: COMP 1010

(3 marks) getTopCard – takes a hand (integer array) and a top pointer (int), 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 it 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 globals 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.

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

4