Homework 6 - Functions: In Between


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

Due: At midnight, November 2, 2001, submit is turned off automatically. Work turned in after that time will not be accepted for grading.

Program Description

If you read this before it has changed. I had inadvertently made a solution to the problem, which I used as an example in the Spring, available in the class notes.

For this assignment you are to create computer game called "In Between". This is a card game played with a standard 52 card deck. The basic idea is that the player is dealt two cards. If the next card drawn falls between the two cards dealt to the player, then the player wins, otherwise the player loses. The player controls the game by deciding how much to bet after seeing their two cards, but before seeing the third card (obviously).

Here are the exact rules.

  1. The player is given an initial bank rolll of $100.
  2. Before each hand the player may quit.
  3. If the player chooses to continue playing, deal them 2 cards.
  4. If the 2 cards are a pair (the same pip value), or if the two cards are adjacent (e.g. 2 hearts and 3 diamonds, or a king and queen) the player automatically loses $1. No third card is drawn and no bet is made.
  5. If the preceeding two rules do not apply, then a third card is drawn and the user is asked to place a bet. The maximum bet is $10 (or the amount in their bank roll whichever is smaller).
  6. If the third card is between the player's cards, the player wins the amount of their bet.
  7. If the third card has the same pip value as one of the player's cards, then it is a draw.
  8. Otherwise, the player loses the full amount of their bet.
  9. The deck is reshuffled after every 10 rounds of play.
For this assignment your program must adhere precisely to the input specifications given here. That is, you must be able to accept the input from the examples below and obtain the identical final bank roll. You are free to use your own wording of the queries and to modify the format of the output lines.

Here are the additional requirements:

  1. The player can indicate their desire to play another round by typing in "y", "Y", "yes", or "Yes". The can indicate their desire to quit by typing in "n", "N", "no", or "No". If they type anything else, ask them again.
  2. Start the game by asking the player for an integer that will be used to "seed" the random number generator. See the example below.
  3. The player's bet must be between $1 and $10 unless they have less than $10 remaining in which case the maximum bet is their bank roll amount. If they enter an illegal bet, prompt them again until they enter a legal bet.

For this assignment you will need to use some non-standard types that represent cards and a deck. You can find the documentation for these classes here. These classes are installed on the lab machines in the same location as tio so you don't need to do anything extra to use them in the lab. If you want to use them at home, copy cards.jar into the same location you copied tio.jar. Be sure and right click on the link and use "Save As". Just left clicking the link will not work.

You will need to add import cards.*; at the top of your source file. To create a deck of cards use:

    Deck theDeck = new Deck(seed);
The seed is some user specified integer to "seed" the random number generator. Using a seed allows us to play the exact same sequence of cards over and over for testing.

To shuffle the deck use:

    theDeck.shuffle();
To draw a card use:
    Card someCard = theDeck.draw();
To find out the value of a card use:
    int value = someCard.getValue(); // a value in the range 1 to 13
You can try out my version of the program on the lab machines by typing:
    java SampleInBetween
Here are some sample executions. To save you some typing, you will want to use file redirection during the final stages of testing. The input for the two examples below can be found in hw6Input1 and hw6Input2. Save these to a file and then you can test with something like "java myProgram < hw6Input1".
os-prompt>java SampleInBetween
Enter a random number to start the game.
22
Let's play In-Between.
After seeing your cards you can bet up to $10 that the next card will be
between the two cards in your hand.
If the next card drawn is between the 2 cards in your hand you win the
amount bet.
If the next card matches one of your cards it is a draw and the next hand
is free.
If your hand is a pair, or the cards are adjacent (e.g. 4 and5), then you automatically lose $1 (no third card is drawn).
Otherwise you lose the amount bet.


Shuffling the deck...

You have: 7:diamonds , 9:clubs 
You have $100. How much do you want to bet? $1 to $10
1
The next card is Jack:clubs 
You lose.

You have $99
Do you want to continue playing (y/n)?
y
You have: 3:spades , 2:diamonds 
You lose $1.

You have $98
Do you want to continue playing (y/n)?
y
You have: 10:diamonds , 9:spades 
You lose $1.

You have $97
Do you want to continue playing (y/n)?
y
You have: 4:hearts , 7:hearts 
You have $97. How much do you want to bet? $1 to $10
1
The next card is 10:clubs 
You lose.

You have $96
Do you want to continue playing (y/n)?
y
You have: 3:hearts , Ace:diamonds 
You have $96. How much do you want to bet? $1 to $10
1
The next card is King:diamonds 
You lose.

You have $95
Do you want to continue playing (y/n)?
y
You have: 9:hearts , 4:spades 
You have $95. How much do you want to bet? $1 to $10
1
The next card is 10:spades 
You lose.

You have $94
Do you want to continue playing (y/n)?
y
You have: Ace:clubs , 2:clubs 
You lose $1.

You have $93
Do you want to continue playing (y/n)?
y
You have: 4:clubs , Queen:spades 
You have $93. How much do you want to bet? $1 to $10
10
The next card is 8:clubs 
You win.

You have $103
Do you want to continue playing (y/n)?
y
You have: 9:diamonds , King:clubs 
You have $103. How much do you want to bet? $1 to $10
1
The next card is Queen:clubs 
You win.

You have $104
Do you want to continue playing (y/n)?
n

Thanks for playing. You ended up with $104.



os-prompt>java SampleInBetween
Enter a random number to start the game.
1234
Let's play In-Between.
After seeing your cards you can bet up to $10 that the next card will be
between the two cards in your hand.
If the next card drawn is between the 2 cards in your hand you win the
amount bet.
If the next card matches one of your cards it is a draw and the next hand
is free.
If your hand is a pair, or the cards are adjacent (e.g. 4 and5), then you automatically lose $1 (no third card is drawn).
Otherwise you lose the amount bet.


Shuffling the deck...

You have: 6:diamonds , Jack:spades 
You have $100. How much do you want to bet? $1 to $10
10
The next card is Queen:hearts 
You lose.

You have $90
Do you want to continue playing (y/n)?
y
You have: Queen:diamonds , Ace:hearts 
You have $90. How much do you want to bet? $1 to $10
10
The next card is Ace:clubs 
That's a draw.

You have $90
Do you want to continue playing (y/n)?
x
Please enter yes or no.
yes
You have: 10:clubs , 10:diamonds 
You lose $1.

You have $89
Do you want to continue playing (y/n)?
y
You have: 8:spades , 2:diamonds 
You have $89. How much do you want to bet? $1 to $10
20
Sorry, that is not a legal bet.
How much do you want to bet? $1 to $10
10
The next card is Jack:diamonds 
You lose.

You have $79
Do you want to continue playing (y/n)?
y
You have: Queen:spades , 9:spades 
You have $79. How much do you want to bet? $1 to $10
10
The next card is 3:diamonds 
You lose.

You have $69
Do you want to continue playing (y/n)?
y
You have: King:diamonds , 5:spades 
You have $69. How much do you want to bet? $1 to $10
10
The next card is King:hearts 
That's a draw.

You have $69
Do you want to continue playing (y/n)?
n

Thanks for playing. You ended up with $69.


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

This page maintained by Charlie McDowell. Email regarding this site.