Casino

The Game

Casino is a variant of blackjack that customizes the echo framework.

When a game begins the dealer generates a random number and the player's total is set to 0:

1 <= dealerTotal <= 21
playerTotal = 0

The player has four commands:

hit: playerTotal += card, where 1 <= card <= 10 is a random number. Player loses if 21 < playerTotal

stay: end the game, player loses if playerTotal < dealerTotal

new: start a new game

help: display these commands

Starting the Server

After carefully exporting all of my binaries, here is how I start the server:

C:\Users\000030xxx\echo\bin>java echo.Server casino.CasinoHandler
server address: 0.0.0.0/0.0.0.0
Server listening at port 5555

A Sample Session

C:\Users\000030xxx\echo\bin>java echo.SimpleClient
-> hit
received: total = 2, again?
-> hit
received: total = 5, again?
-> hit
received: total = 12, again?
-> hit
received: total = 17, again?
-> stay
received: player total = 17, casino total = 16, you win!
-> hit
received: Game over, enter new or quit.
-> new
received: Want a card?
-> hit
received: total = 9, again?
-> hit
received: total = 11, again?
-> hit
received: total = 17, again?
-> hit
received: total = 23, you lose!
-> new
received: Want a card?
-> yes
received: Unrecognized command: yes
-> quit
bye

Implementation

Here's my implementation:

CasinoHandler.java

Notes

·       The random number generator, rng, will be shared by all casino handler threads, so it is static. However, since it's a shared resource, I made it private. Each thread must access it through the synchronized static method, hitMe.

Casino Tracker

We would like to enhance Casino by adding two new commands:

stats: returns the player's win percentage for the current session

max: returns the all-time best session win percentage of all players

Unfortunately, we are forbidden from altering CasinoHandler.

To solve this problem we introduce a CasinoTracker proxy: CasinoTracker.java.