Three Card Monte!

Three Card Monte (TCM) is a simple game. The dealer shuffles three face-down cards, then asks the player to guess which one is the jack of hearts. If the player was able to track the jack of hearts during the shuffle, then he might be able to find it.

Our online version dispenses with the tracking and shuffling. The user simply picks a card. If he guesses correctly, then he gets three points, otherwise he loses one point.

Although not a very interesting game, our version keeps track of the player's maximum score across multiple sessions. A session may involve multiple requests. A request corresponds to a single hand of TCM. In order to remember a player's maximum score across multiple sessions, a login and logout is required.

TCM is typical of many web applications. A user registers once. After that the user logs in to start a new session. A session may consist of multiple requests. The session ends when the user logs out.

We can think of the user and the session as state machines. Examples of state machines include processes such as applying for admission, shopping in an e-store, or helping a customer. Games such as moving through a maze or playing poker can also be viewed as state machines.

The initial state of the session machine is determined by the current state of the user machine during login. Requests cause state transitions in the session machine. The current state of the session machine causes a state transition in the user machine when the session ends.

Demonstration

Login (register.html)

TCM begins with the login page:

New User Registration

signup.html

If the user id or password don't match any known players, the user is escorted to the signup form:

signup.jsp

When the sign up is complete, the new player receives a confirmation:

welcome.jsp

If the player is known, he is escorted to the welcome page that displays his current state and begins a new session:

Pick a card (guess.jsp/guess.html)

A typical session involves multiple "Right?" requests sent to the guess.jsp page:

Pressing the "Quit" button ends the session:

Tracking users

The basic idea is to associate a unique user ID with a User bean (object) in the application's attribute table. The User bean contains information about the user such as name, password, history, and current state.

User.java

Note that updating (i.e., incrementing) the current score also updates the maximum score. In general, it's best to do as much application-specific work as possible in the bean.

Login and Signup

The login page (register.html) sends the login request to controller.jsp.

If the controller recognizes the user, then the request is forwarded to welcome.jsp, otherwise the request is forwarded to signup.html.

The welcome page uses the user id in the request to look up the user bean in the application's attribute table. It then creates an entry in the session's attribute table associating the user id to the user bean. Note that both the application table and the session table contain references to the same user bean.

The signup page allows the user to fill in a registration form that is then submitted to signup.jsp. The signup JSP checks to make sure that the requested user id is not already taken. If not, a new User bean is created, initialized, and added to the application's attribute table.

Playing the Game

The play of the game is controlled by guess.jsp, which conditionally includes guess.html.

The main feature of guess.html is a form that allows the user to either pick a card or to quit the session.

The guess JSP invalidates the session if the user submits using the "Quit" button. The user bean is displayed, and guess.html is not included.

Otherwise, a random card is generated. If the player's guess matches, three points are added to the user bean, otherwise 1 point is deducted. Also, guess.html is included.