Jump to page content Jump to navigation

College Board

AP Central

AP Exam Reader
AP Annual Conference - Save the Date
Siemens Awards for Advanced Placement

Print Page
Home > AP Courses and Exams > Course Home Pages > High/Low Card Game

High/Low Card Game

by Evelyn Rothman
CSTA Member
Arthur L. Johnson High School
Clark, New Jersey


Read more Strategies...

Overview
  • Simulate a deck of playing cards.
  • Shuffle the deck.
  • Deal (five) cards into the hand.
  • Turn over the first card.
  • Leave the remaining (four) face down.
  • The user must guess whether the next card is higher or lower.
  • Turn over the next card.
  • If the user guesses correctly, the game continues.
  • If the user guesses incorrectly, the game is over and the user loses.
  • If the user guesses correctly (four) times, the game is over and the user wins.
Objectives
  • Using classes to represent objects
    • Card
    • CardDeck
    • Hand
  • Using public and private methods
  • Using data structures
    • Two-dimensional array (card deck)
    • ArrayList or array (hand)
  • Using the compareTo method
    • Card class implements Comparable
    • Order is the rank
  • Implementing the CardDeck shuffle
    • Each Card in turn is swapped with a random Card
Level
  • AP Computer Science
    • Reinforces the objectives specified above
Time required
  • Four or five 45-minute classes


Guide to the Teacher

Concepts
  • Create and use classes: Card, CardDeck, Hand
  • Use the Comparable interface
    • The Card class compareTo method compares the ranks of two Card objects.
  • Use Data structures: ArrayList, array
  • Create a permutation of objects (i.e., shuffle the CardDeck)
  • Create a GUI
Prerequisites
Before starting this project, students should have an understanding of the following:
  • Strings
  • Methods
  • Arrays and/or ArrayLists
  • The Comparable interface and the compareTo method
  • Classes and objects
  • Applets
Notes
  • I introduce this project in the AP course to reinforce the concepts stated above. Before the students begin the high/low applet, we work as a class on:
    • The Card, CardDeck, and Hand classes
      • Some methods are done as a class; others the students do individually.
    • The creation of an ordered CardDeck
    • An algorithm for shuffling the CardDeck
    • Dealing Cards
  • After they complete the high/low card game, students can go on to write programs for card games that are more complex, such as blackjack or war.
  • For a greater challenge, consider games that require students to extend the Card class (e.g., card ranks differ from 2, 3, . . . , jack, queen, king, ace) and/or the CardDeck class (e.g., old maid eliminates one queen: see http://saturn.cs.ukzn.ac.za/~robd/2005/courses/comp200/pracs/05prac06/oldmaid.html).
High/Low Card Game -- Expected Results

Students will write the high/low card game applet, which will behave as follows:
  • A new CardDeck is constructed and shuffled.
  • A Hand of five* Cards is dealt; the image of the first Card is shown face up, and the remaining four are face down (card back image).
  • The user guesses higher or lower.
    • If the user is correct, the game continues.
    • If the user is incorrect, the game ends.
  • If the user guesses correctly four times (i.e., all card images are face up), the user wins.
  • The user can play again.
    • Reinitialize
      • New CardDeck, shuffle, deal
      • Variables reinitialized
* Of course, the number of Cards in the Hand can be greater or fewer than five.

This lesson can be introduced using the included Card Project PowerPoint presentation.
  High/Low Card Game -- PowerPoint Slides (.ppt/164KB)

Completed code for the classes and applet is also available.
  High/Low Card Game -- Completed Classes and Applet (.pdf/59KB)


Evelyn Rothman is a Computer Science Teachers Association (CSTA) member.


  ABOUT MY AP CENTRAL
    Course and Email Newsletter Preferences
  AP COURSES AND EXAMS
    Course Home Pages
    Course Descriptions
    The Course Audit
    Sample Syllabi
    Teachers' Resources
    Exam Calendar and Fees
    Exam Questions
    AP Credit Policy Information
  PRE-AP
    Teachers' Corner
    Publications
  AP COMMUNITY
    About Electronic Discussion Groups
    Become an AP Exam Reader

Back to top