Program 7
The last program will be a project due in three installments. The project
will be to build a Wheel of Fortune game. If you don't know what Wheel of
Fortune is, its on TV in the late afternoons, so you can watch it a couple
of times and see what its like.
Basically, the game is a word guessing game, where a word or phrase
is to be guessed by guessing letters one at a time. For example, if
the phrase is "socket programming is fun", the players would see a set of
blanks representing the letters "------ ----------- -- ---". As the
players guess the letters, they are put into the word. For example, if
someone guessed the letter "m", "------ ------mm--- -- ---" would be
displayed. Another guess of "n" would result in "------ ------mm-n- -- ---".
Each guess that is correct results in the banking of an amount of money,
which is decided by a spin of a wheel. If the wheel spin showed $500, then
guessing an "m" would result in winnings of $1000. However, you don't get to
keep the money unless you are the player that finally guesses the phrase.
Vowels can't be guessed, they
have to be purchased for $50 for a guess. For example, if someone bought the
letter "o", the o's would be shown in the phrase, and it would cost that
player $50. A player has to have already accumulated some money to guess a vowel.
An incorrect guess passes the turn to the next player.
A correct guess allows the same player to spin and guess again.
The wheel has some other values on it, such as "Lose Your Turn" and
"Bankrupt". Losing your turn passes the opportunity to guess to the
next player in turn. Going bankrupt takes away all of your money and
you lose your turn - bummer!
Each player or client would have to be able to see the current state of
the phrase with all guessed letters showing, the amount of money they
have accumulated, and ALL letters that have been guessed.
Structure
The server would be responsible for signing up players - getting a name
and setting up communications, generating the spin, which would be a
random value chosen from the set of possible wheel values, and processing
user guesses that are sent to it. It should keep each player updated after
every operation.
The client would mostly just process information and display it on the
screen, and get the guesses from the user to pass to the server.
Extras
There are some additional things that "Wheel" does. The wheel sometimes has
chits for avoiding a "Lose Your Turn" spin, or bonus locations, like trips
and such. Handling these is optional. You could also allow "Speed Wheel",
where consonants have a set value and there is no spinning.
The Phrase Database
The phrases will probably come from some sort of database. Normally, the
players get a hint of some kind, and there are even some special categories.
For example:
| Category | Puzzle |
| People | BONNIE AND CLYDE |
| Occupation | SMOKE JUMPER |
| Household Item | COFFEE TABLE |
| Historical Event | BOSTON TEA PARTY |
| Wisdom | A STITCH IN TIME SAVES NINE |
| Before and After | A BIRD IN THE HAND SHAKE |
The last example is a special category that comes up occassionaly and the
puzzle starts with the start of some common phrase and ends with the
end of another common phrase.
Random Numbers
Random number generation is easy. You get a random number uniformly
distributed between 0 and RAND_MAX (2147483647) by calling rand. For example:
r = rand ();
To convert this to a uniformly distributed random number between
some low value and some high value, you perform the following:
r = rand ();
r1 = (int) (low + ((float) r)/(float)RAND_MAX * high);
For example, to get a random number between 1 and 100,
r = rand ();
r1 = (int) (1 + ((float) r)/(float)RAND_MAX * 100);
However, this will always generate the same sequence of random numbers
which isn't much fun (but useful for debugging), so you can call another
function to set the random number seed, srand.
srand (seed_val);
If the seed_val is the same, you get the same sequence, so you would
like some way to set the seed randomly. This seems like a Catch-22, but
you can use the clock to get a random starting point like this:
struct timeval timeofday;
struct timezone tz;
long seed;
gettimeofday (&timeofday, &tz);
seed = timeofday . tv_sec + timeofday . tv_usec;
srand48 (seed);
Remember, you only seed the random generator once when your program starts
and after that, it generates a long sequence of numbers which are
close to random.
Scheduling
The project is going to be submitted in stages.
- November 13 - The client and server should be able to connect
and exchange basic info. The server should be able to read
a database of phrases - say 20 or so, to get the phrase to use
and send the client the correct organization of blanks.
The phrase should be selected randomly.
- November 27 - Play a rudimentary game with multiple players,
and a fixed amount for the values of letters (no spinning).
- December 4 - To the previous level, add randomized spinning
including the "bankrupt" and "lose your turn".
- December 11 - Add random puzzle selection and a database of
puzzles. Also, keep track of the amount
won by each player. Keep everything updated at the client side.
If you choose not to finish the project, you will be graded on the basis
of what you turned in for earlier milestones.
The fourth milestone will be evaluated for completeness and quality and
will be treated as a bonus.
Architecture
The server should use TCP (stream sockets) to communicate with the
clients. Each new client must create a child to handle the
communication with that client. The child processes and the parent
server can communicate with any reasonable means, but pipes are probably
the easiest.
The client has to use UDP to broadcast to find a server, but all other
communications will be done with TCP. If you had multiple servers, you
could do fancy things, like multiple games simultaneously, or you could
choose the game based on some parameter like difficulty or who the other
players are. Multiple servers is not a requirement.
The server and the child servers will have to deal with multiple I/O streams
operating asynchronously - i.e. not scheduled. Use the select function
to manage the sockets and pipes.