teaching machines

CS 145 Lab 5 – Loops

October 17, 2014 by . Filed under cs145, fall 2014, labs.

First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab. No credit will be awarded past these 15 minutes.

Don’t forget to work in pairs! Where possible, please work with someone that you did not work with last week. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.

Objective

In this lab you will learn about repeating code using loops. Each of today’s problems involves a deck of cards. Though we use card terminology, you do not need to have ever touched a deck of cards to follow the instructions. However, if you confused by any of the terminology, we’re happy to answer your questions.

We provide Deck and Card classes for you to use. Copy and paste them into Eclipse. Eclipse is smart. If you copy the entire code listing for a class, select your package in the Package Explorer, and paste, it’ll automatically create a Java source file with the appropriate name.

Here’s Card.java:

public class Card {
  public static int ACE = 1;
  public static int JACK = 11;
  public static int QUEEN = 12;
  public static int KING = 13;

  public static int HEARTS = 0;
  public static int DIAMONDS = 1;
  public static int SPADES = 2;
  public static int CLUBS = 3;

  /** Card's suit */
  private int suit;

  /** Card's rank */
  private int rank;

  /**
   * Creates a new card.
   * @param suit Card's suit
   * @param rank Card's rank
   */
  public Card(int suit, int rank) {
    this.suit = suit;
    this.rank = rank;
  }

  /**
   * Gets the card's rank.
   * @return One of ACE, JACK, QUEEN, KING, and 2-10.
   */
  public int getRank() {
    return rank;
  }

  /**
   * Gets the card's suit.
   * @return One of HEARTS, DIAMONDS, SPADES, and CLUBS.
   */
  public int getSuit() {
    return suit;
  }

  /**
   * Gets a string representation of this card.
   * @return Text description of card, e.g., "queen of spades."
   */
  public String toString() {
    String suitName;
    if (suit == HEARTS) {
      suitName = "hearts";
    } else if (suit == DIAMONDS) {
      suitName = "diamonds";
    } else if (suit == SPADES) {
      suitName = "spades";
    } else {
      suitName = "clubs";
    }

    String rankName;
    if (rank == ACE) {
      rankName = "ace";
    } else if (rank == KING) {
      rankName = "king";
    } else if (rank == QUEEN) {
      rankName = "queen";
    } else if (rank == JACK) {
      rankName = "jack";
    } else {
      rankName = "" + rank;
    }

    return rankName + " of " + suitName;
  }
}

A Card is an object like a Scanner is an object. You can construct a Card. You can invoke methods on a Card. For example:

Card card = new Card(Card.HEARTS, Card.QUEEN);
System.out.println(card.toString());
System.out.println(card.getSuit() == Card.SPADES);

Here’s the Deck class:

import java.util.Collections;
import java.util.Stack;

public class Deck {
  /** Set of cards in deck */
  private Stack<Card> cards;

  /**
   * Creates a new shuffled deck.
   */
  public Deck() {
    cards = new Stack<Card>();
    for (int s = 0; s < 4; ++s) {
      for (int r = Card.ACE; r <= Card.KING; ++r) {
        cards.push(new Card(s, r));
      }
    }
    Collections.shuffle(cards);
  }

  /**
   * Checks whether or not the deck is empty.
   * @return True if empty, false otherwise.
   */
  public boolean isEmpty() {
    return cards.isEmpty();
  }

  /**
   * Draws a card. Assumes at least one card is left in deck.
   * @return Card drawn
   */
  public Card draw() {
    return cards.pop();
  }
}

Decks are shuffled upon creation. To draw a Card from a Deck, call the Deck‘s draw method:

Deck deck = new Deck();
Card card = deck.draw();
System.out.println(card);

Checkpoints 1 and 2

Solve two of the following problems. Person A types for one, person B for the other.

  1. Play Hot-Cold. Have the computer draw a random card. Ask the user to guess what it is by typing input of the form “1 H”, “13 S”, or “9 D”. If the rank is wrong, report whether the guess is too high or too low. Also indicate if the suit is wrong. Repeat until she succeeds.
  2. Play a modified game of War. While the deck is not empty, have each player draw a card. If the rank of player A’s card is greater than player B’s, player A gets a point. And vice versa. If the ranks are the same, no one gets a point. Display the scores and winner at the end of the game.
  3. Draw cards just until you have one of every suit. Print out how many you have of each suit.
  4. Devise and implement a Blackjack strategy.

Extras

These problems are provided to give you extra practice. They’re worth is measured in neurons in your brain, not checkpoint credit.

  1. Determine short-suitedness. One is said to be short-suited when has no cards of a particular suit. Draw five cards and indicate which suits are not represented in your hand.
  2. Check for a flush, as in poker. Draw five cards and report whether all cards have the same suit.
  3. Suppose you have a variable an trump variable which is one of Card.HEARTS, Card.DIAMONDS, Card.SPADES, or Card.CLUBS. Draw 5 cards and print out which ones belong to the trump suit. A jack of the same color as the trump suit is also considered to be trump. For example, if hearts are trump, the jack of diamonds is also trump. Both hearts and diamonds are red.