Difference between revisions of "CISC181 S2019 Lab4"

From class_wiki
Jump to: navigation, search
(Instructions)
(Instructions)
 
(4 intermediate revisions by the same user not shown)
Line 13: Line 13:
 
[[Image:poker_payouts.png|150px]]<br>
 
[[Image:poker_payouts.png|150px]]<br>
  
Playing each game one by one is rather boring (but you should probably do this in the course of your testing).  So instead you will modify and add to the above classes to create a *simulator* to tabulate gains or losses over many games played automatically.  Specifically, your player starts with a bankroll of $1,000,000 and bets $1 per game.  Win or lose, play 1,000,000 games ''automatically'' according to the following super-simple strategy and print (1) the final amount, and (2) the ''best'' hand that you ever achieved.  It would also be nice to see (3) the amount you would earn for the same hands if you didn't use the strategy at all -- if you just accepted the initial 5 cards that were dealt.
+
Playing each game one by one is rather boring (but you should probably do this in the course of your testing).  So instead you will modify and add to the above classes to create a *simulator* to tabulate gains or losses over many games played automatically.  Specifically, your player starts with a bankroll of $1,000,000 and bets $1 per game.  Win or lose, play 1,000,000 games ''automatically'' according to the following super-simple strategy and print (1) the final amount of money remaining, and (2) the ''best'' hand that you ever achieved.  It would also be nice to see (3) the amount you would earn for the same hands if you didn't use the strategy at all -- if you just accepted the initial 5 cards that were dealt.
  
 
* If you have worse than a pair, discard everything
 
* If you have worse than a pair, discard everything
Line 25: Line 25:
  
 
* Currently there is no way to compare individual cards to see which one is "better".  Make the <tt>Card</tt> class implement the <tt>Comparable<Card></tt> interface
 
* Currently there is no way to compare individual cards to see which one is "better".  Make the <tt>Card</tt> class implement the <tt>Comparable<Card></tt> interface
* Write a <tt>Comparator</tt> class <tt>ByHandValue</tt> to compare objects from the <tt>Hand</tt> class.  The comparison function will be <tt>int compare(ArrayList<Card> H1, ArrayList<Card> H2)</tt>, where a return value < 0 indicates that H1 would lose to H2, a return value > 0 means that H1 would beat H2, and a return value of 0 means H1 and H2 are "tied."  
+
* Write a <tt>Comparator</tt> class <tt>ByHandValue</tt> to compare objects from the <tt>Hand</tt> class.  The comparison function will be <tt>int compare(ArrayList<Card> H1, ArrayList<Card> H2)</tt>, where a return value < 0 indicates that H1 would lose to H2, a return value > 0 means that H1 would beat H2, and a return value of 0 means H1 and H2 are "tied." Assume that cards come from a standard 52-card deck with no wildcards, and aces can be high or low.  This will be a complicated function -- please make it as modular as possible by calling other support functions.
Assume that cards come from a standard 52-card deck with no wildcards, and aces can be high or low.  This will be a complicated function -- please make it as modular as possible by calling other support functions.
+
** '''One of your classmates pointed out that this would make more sense as <tt>int compare(Hand H1, Hand H2)</tt> and I agree -- this was an oversight by me.  I will accept either way'''
 
* Your "simulation" can happen entirely in main() in <tt>Lab4.java</tt>. Below is some starter code to initialize and shuffle a deck, then draw 5 cards from it.
 
* Your "simulation" can happen entirely in main() in <tt>Lab4.java</tt>. Below is some starter code to initialize and shuffle a deck, then draw 5 cards from it.
 +
 +
Deck D = new Deck();
 +
D.shuffle();
 +
ArrayList<Hand> hands = D.deal(1, 5);
 +
hands.get(0).print();
  
 
==Submission==
 
==Submission==
  
 
Submit your modified <tt>Card.java</tt> and <tt>Hand.java</tt> classes and <tt>ByHandValue.java</tt>, as well as  <tt>Lab4.java</tt>, on Canvas (be sure to add your name).
 
Submit your modified <tt>Card.java</tt> and <tt>Hand.java</tt> classes and <tt>ByHandValue.java</tt>, as well as  <tt>Lab4.java</tt>, on Canvas (be sure to add your name).

Latest revision as of 20:32, 18 March 2019

Preliminaries

  • Make a new project with n = 4 (following these instructions)
  • Name your main class "Lab4" (when creating a new module in the instructions above, in the Java class name field)
  • Remember that this week you can choose a partner, or work alone. No partners will be assigned

Instructions

Suppose you want to write a video poker simulator based on the provided Card, Deck, Hand, and CardGroup classes. See "The Game" section here for the rules, in particular the "Jacks or Better" variant. In short, for each hand you are given 5 random cards, and you can choose from 0 to 5 of them to replace. Depending on the result (a pair, two pairs, three of a kind, flush, etc.), you get a payout based on the odds, as shown below:

Poker payouts.png

Playing each game one by one is rather boring (but you should probably do this in the course of your testing). So instead you will modify and add to the above classes to create a *simulator* to tabulate gains or losses over many games played automatically. Specifically, your player starts with a bankroll of $1,000,000 and bets $1 per game. Win or lose, play 1,000,000 games automatically according to the following super-simple strategy and print (1) the final amount of money remaining, and (2) the best hand that you ever achieved. It would also be nice to see (3) the amount you would earn for the same hands if you didn't use the strategy at all -- if you just accepted the initial 5 cards that were dealt.

  • If you have worse than a pair, discard everything
  • If you have no better than a pair, keep it and discard the other three
  • If you have no better than two pairs, keep them and discard the remaining card
  • If you have no better than three of a kind, keep it and discard the other two
  • If you have better than three of a kind, keep everything

A fundamental issue here is comparing cards and comparing hands: how much money does a particular hand win, do any of the above strategies apply, and is the current hand better than any previous hand? The ranking of hands is detailed here. Here are some of the key changes you will need to make for this to happen:

  • Currently there is no way to compare individual cards to see which one is "better". Make the Card class implement the Comparable<Card> interface
  • Write a Comparator class ByHandValue to compare objects from the Hand class. The comparison function will be int compare(ArrayList<Card> H1, ArrayList<Card> H2), where a return value < 0 indicates that H1 would lose to H2, a return value > 0 means that H1 would beat H2, and a return value of 0 means H1 and H2 are "tied." Assume that cards come from a standard 52-card deck with no wildcards, and aces can be high or low. This will be a complicated function -- please make it as modular as possible by calling other support functions.
    • One of your classmates pointed out that this would make more sense as int compare(Hand H1, Hand H2) and I agree -- this was an oversight by me. I will accept either way
  • Your "simulation" can happen entirely in main() in Lab4.java. Below is some starter code to initialize and shuffle a deck, then draw 5 cards from it.
Deck D = new Deck();
D.shuffle();
ArrayList<Hand> hands = D.deal(1, 5);
hands.get(0).print();

Submission

Submit your modified Card.java and Hand.java classes and ByHandValue.java, as well as Lab4.java, on Canvas (be sure to add your name).