Lab 8: A Can 'O Beans
Objectives
In this lab you'll have some more practice at writing a complex looping program. When you're done, you'll also have the chance to put that invariant knowledge of yours to work.
Update your repository
As always, start by running the following command in your repository to pull the most recent changes:
git pull
The Bean Can Game
During the last lecture we discussed a game involving a can filled with black and white beans. The rules of the game are as follows:
Before the game begins, you're told the number of black beans and the number of white beans in the can. Given this information, you are to guess the color of the last remaining bean in the can (after the game is played out).
Each "turn", you take two beans out of the can (at random) and put a bean back from a separate stash. The color of the bean you put back is based on the color of the beans you draw out -- the rules are:
- If you draw out two same colored beans (two white or two black), you put a black bean back into the can
- If you draw out two different colored beans (one white and one black), you put a white bean back into the can
You keep taking turns (with whoever else is playing) taking beans out of the can and putting one back in until there is only one bean left in the can.
If you guessed the correct color at the start of the game, you win!
Exercise 1
For this week's lab exercise you will write a program that randomly fills a can with a combination of black and white beans, allows you to enter a guess, then simulates the game for you, pausing at each turn to let you monitor the state of the game.
At the end, you will be informed if you won.
The following is sample output of a completed program:
There are 1 black bean(s) and 3 white bean(s).
What color bean do you think will be drawn last?
black
Hit enter to draw out two beans!
Grabbed 0 black and 2 white beans.
Putting back a black bean
2 black bean(s) and 1 white bean(s) left.
Hit enter to draw out two beans!
Grabbed 2 black and 0 white beans.
Putting back a black bean
1 black bean(s) and 1 white bean(s) left.
Hit enter to draw out two beans!
Grabbed 1 black and 1 white beans.
Putting back a white bean
0 black bean(s) and 1 white bean(s) left.
The last bean is white -- you LOST!
Note that, to keep your sanity, you'll probably want to populate the can with 0-10 black beans and 0-10 white beans to begin with (though it should be easy to change your program to use any number of random beans).
Save your code in "lab08/lab08game.rb"
We'll talk about this program more next week, so just try and get started on your own in lab, and think about it some more before coming to class. Good luck!
Hints
Only read on if you're stuck and want some hints.
Note that each time through your program loop, you'll need to keep track of the number of white/black beans, and randomly come up with the number of beans of each color that are drawn up (totalling 2 beans) -- this will probably be the most interesting part of your program, and you'll want to think about the logic for it before you start.
In fact, you might want separately test out your ideas for how to come up with a proper randomized combination of beans given the total number of each color.
For instance, if the number of white beans is stored in the variable nwhite
and the number of black beans in nblack
, is the following sufficient for generating a random number of beans of each color?
w_drawn = rand(3) # draw out between 0 and 2 white beans
b_drawn = 2 - w_drawn # the rest have to be black
What if there are no white or black beans left? What if there is only 1 of a given type? Does the above still work?
For an extra challenge, you might want to think about how to take into consideration the relative number of the different beans in the bag when generating a random draw. If there are 100 white bean and 2 black, for instance, the random draw should much more likely be 2 white beans than a combination of the two (or two black).