Homework 4
For this homework assignment, you are to create a hangman server and
client. The game of hangman is a simple word guessing game, and a
sample online version of the game is available here. You can play
the sample game and figure out how the basic principles work.
Your game is to be non-graphic, so you don't need to draw a
hangman. The client as well as the server are to be text based,
and will utilize collections, threads, and sockets. The basic
program logic is as follows:
When the server starts up, it loads all the words in this file and creates a HashSet to store all the
words. This file has about 5000 words in it, basically including
words that begin with the letter A and running through words beginning
with the letter "E".
A server creates a Socket listener on a certain port (you decide which
port), and the client connects to that port (you pass in the server and
port to the client program).
When a client connects to a server, a new thread is created on the
server side to support each new session game for each new client.
When a new thread is created to run a new client game, a word is chosen
from the words collection at "random".
At the start of each client session, two strings are sent back to the
client, such as (assume the word is "assume"):
"- - - - - -"
"You have 10 lives left."
The client then prompts the user to enter a letter. Say the user
types an "s". The "s" is sent back to the server thread, and the
server looks to see if there is a hit in the chosen word "assume" (the
search is CASE INSENSITIVE). The letter "s" is added to the list
of already-asked letters on the server. There is an "s", of
course, so the strings sent back to the client would be:
"- s s - - -"
"You have 10 lives left."
Now, the client displays this information and prompts the user to enter
another letter. Suppose the user types a "z". The "z" is
sent back to the server thread, and the server thread checks the word
for a match. There is no z in the word "assume", so the server
thread decrements its lives counter from 10 to 9, and adds the letter
"z" to the list of already-asked letters. So the server would
then send back the following strings:
"- s s - - -"
"You have 9 lives left."
The client and server continue to communicate until either the word is
correctly guessed, in which case the server thread will respond:
"Congratulations! The correct word was indeed 'assume'"
"Want to play again?"
Or, in the case of failure, the server would respond:
"Sorry, you have used up all 10 of your lives. The correct word
was 'assume'".
"Want to play again?"
The client will display these strings, and will close the socket
connection. If the user wants to play again, a new socket
connection is created and the game begins anew. If the user is
done playing, the client exits gracefully.