/************************************************************************************
 * Hangman's Noose Client                                                           *
 *  Joins game using server at port 5000                                            *
 *  Gets word from a large dictionary                                               *
 *  Lets user select letter, reporting these to server                              *
 *  Displays results, including graphical hanging man                               *
 *                                                                                  *
 * Charles E. Hughes -- November 14, 1997                                           *
 ************************************************************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;



public class Client extends JApplet implements Runnable, ActionListener {
	Socket server;			// socket created when connect to server
	DataInputStream input;		// input stream associated with server socket
	DataOutputStream output;	// output stream associated with server socket

	// Start up by connecting to server and opening streams
	Thread playerThread;		// player runs in this thread
	public void start(){
		try{
			server = new Socket(InetAddress.getLocalHost(), 6000);
			input = new DataInputStream(server.getInputStream());
			output = new DataOutputStream(server.getOutputStream());
		}
		catch( IOException e){
			e.printStackTrace();
		}
		playerThread = new Thread( this );
		playerThread.start();
	}

	// Just start up the method that monitors communication from server
	public void run() {
		play();
	}

	boolean laugh = false;		// start with flush; laugh next time
	boolean firstTime = true;	// this is the first time we call sound method
	// Play sounds appropriate to user's last selection
	void playsound(char previousWrong, char newWrong) {
		if (newWrong > previousWrong){ // a little variety on mistake razzing
			if (laugh) play(getCodeBase(), "audio/laugh.au");
			else play(getCodeBase(), "audio/flush.au");
		} else if (!firstTime)
			play(getCodeBase(), "audio/excellent.au");
		laugh = !laugh;		// toggle between laugh and flushing sound
		firstTime = false;	// recall we've been here before
	}

	String word = "";					// partial word
	String message = "Choose a letter!";	// encourage player
	// Play the game
	void play() {
		play(getCodeBase(), "audio/playgame.au");
		char previousIncorrect = '0';
		char result = 'P';
    		try {

			while (result == 'P') {
    				String line = input.readUTF();

				word = line.substring(2, line.length()-2);

    				result = line.charAt(1);
    				char incorrectGuesses = line.charAt(0);
    				image = images[Character.digit(incorrectGuesses, 10)];
    				repaint();
    				playsound(previousIncorrect, incorrectGuesses);
    				previousIncorrect = incorrectGuesses;
    			}
			if (result == 'W') {
				message = "Congratulations, You WIN!!!";
				play(getCodeBase(), "audio/woohoo.au");
			} else {
				message = "Ha, Ha, You Lose!!!";
				play(getCodeBase(), "audio/darn.au");
			}
			repaint();

			input.close();

			output.close();
			server.close();

    		} catch(IOException e) {
    			e.printStackTrace();
			System.exit(1);
		}
	}

	// Get images (hangman) and create letter buttons
	Button[] alpha = new Button[26];	// letter buttons
	Image image;				// current hangman image
	Image[] images = new Image[7];	// collection of hangamn images
	public void init() {
		setBackground(Color.yellow);
		images[0] = getImage(getCodeBase(), "images/hman0.gif");
		images[1] = getImage(getCodeBase(), "images/hman1.gif");
		images[2] = getImage(getCodeBase(), "images/hman2.gif");
		images[3] = getImage(getCodeBase(), "images/hman3.gif");
		images[4] = getImage(getCodeBase(), "images/hman4.gif");
		images[5] = getImage(getCodeBase(), "images/hman5.gif");
		images[6] = getImage(getCodeBase(), "images/hman6.gif");
		image = images[0];
		Panel p1 = new Panel();
		p1.setLayout(new GridLayout(2, 13, 1, 1));
        	for (int i = 0; i <26; i++) {
            	alpha[i] = new Button((new Character((char)('A'+i))).toString());
			p1.add(alpha[i]);
			alpha[i].addActionListener(this);
		}

		getContentPane().add("North", p1);
                repaint();
	}

	// Handle letter selections
	public void actionPerformed(ActionEvent e) {
		try {
			char command = e.getActionCommand().charAt(0);
			output.writeByte(command);	// report letter to server
			alpha[command-'A'].setEnabled(false);	// don't allow this one to be selected again
    		} catch (IOException ev) {
			ev.printStackTrace();
		}
	}

	// refresh screen with word
	public void paint(Graphics g) {
                g.clearRect(0,0,getContentPane().getWidth(),getContentPane().getHeight());
  		g.setFont(new Font("TimesRoman", Font.BOLD, 18));
		g.setColor(Color.black);
		g.drawString(word, 270, 140);
		g.drawString(message, 270, 80);
		g.drawImage(image, 60, 80, Color.white, this);
	}

}