// Plays a game similar to hangman 	
	
import java.io.*;	
import java.util.*;	
import java.awt.*;	
import java.awt.event.*;	
import javax.swing.*;	
	
public class Smiley implements ActionListener {	
		
	// class constants 	
	private final static String WORD_LIST = "words.txt";	
	private final static int OFFSET = 2;	
	private final static int TEXT_WIDTH = 20;	
	private final static int CANVAS_WIDTH = 250;	
	private final static int CANVAS_HEIGHT = 150;	
	private final static int WINDOW_WIDTH = 300;	
	private final static int WINDOW_HEIGHT = 340;	
	private final static Color SKY_BLUE = new Color(217, 217, 255);	
	
	// GUI instance variables 	
	
	// window: frame for GUI 	
	private JFrame window = new JFrame(" :-) Smiley");	
	
	// answerLabel and answerPad: area for displaying mystery word 	
	private JLabel answerLabel = new JLabel(" Word");	
	private JTextField answerPad = new JTextField(TEXT_WIDTH);	
	
	// guessLabel and guessPad: entry area to make a guess 	
	private JLabel guessLabel = new JLabel("Guess");	
	private JTextField guessPad = new JTextField(TEXT_WIDTH);	
	
	// updateButton: signaler that a new guess has been made 	
	private JButton updateButton = new JButton("Update");	
	
	// facePad: drawing area for face 	
	private Canvas facePad = new Canvas();	
	
	// pastLabel and pastPad: area for displaying past guesses 	
	private JLabel pastLabel = new JLabel("Past guesses");	
	private JTextField pastPad = new JTextField(" ", TEXT_WIDTH);	
	
	// resultPad: status area for game result 	
 	private JLabel resultPad = new JLabel(" ");	
	
	// game instance variables 	
	private String word;          // word to be guessed 	
	private StringBuffer answer;  // portion of word guessed so far 	
	private char guess;           // current letter guess 	
	private int errors;           // how many bad guesses so far 	
	private int lettersLeft;      // how many letters still needed 	
	private boolean gameOver;     // has the game been won or lost 	
	
	// Smile(): word-specified constructor 	
	public Smiley(String w) {	
		// process parameter 	
		if (w != null) {	
			word = w;	
		}	
		else {	
			word = getRandomWord();	
		}	
			
		// initialize state information 	
	
		// record number of letters still unfound 	
		lettersLeft = word.length();	
			
		// current guess placeholder 	
		guess = ' ';	
			
		// no errant guesses as of yet 	
		errors = 0;	
			
		// the game is not over 	
		gameOver = false;	
	
		// create mystery word representation, e.g., _ _ _ _ _ _ _ 	
		String s = "";	
		for (int i = 0; i < word.length(); ++i) {	
			s += " _";	
		}	
		answer = new StringBuffer(s);	
	
		// configure GUI 	
		window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);	
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		window.setBackground(SKY_BLUE);	
	
		window.getContentPane()	
				.setBackground(window.getBackground());	
		guessPad.setBackground(window.getBackground());	
		answerPad.setBackground(window.getBackground());	
		resultPad.setBackground(window.getBackground());	
		pastPad.setBackground(window.getBackground());	
		updateButton.setBackground(window.getBackground());	
	
		answerPad.setText(answer.toString());	
			
		updateButton.addActionListener(this);	
	
		facePad.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);	
		facePad.setBackground(Color.WHITE);	
	
		answerPad.setEditable(false);		
		pastPad.setEditable(false);	
	
		window.setLayout(new FlowLayout());	
	
		window.add(answerLabel);	
		window.add(answerPad);	
		window.add(guessLabel);	
		window.add(guessPad);	
		window.add(updateButton);	
		window.add(facePad);	
		window.add(pastLabel);	
		window.add(pastPad);	
		window.add(resultPad);	
	
		window.setVisible(true);	
	}	
	
	// isValid(): is current input guess valid 	
	private boolean isValid(String input) {	
		// input guess must be a single unseen letter 	
		if (input.length() != 1) {	
			// not a single character 	
			return false;	
		}	
		else if (! Character.isLetter(input.charAt(0))) {	
			// not a letter 	
			return false;	
		}	
		else { // its a letter -- is it previously unseen 	
			String past = pastPad.getText();	
			input = input.toLowerCase();	
			// indexOf() only returns -1 if the s is not there 	
			return (past.indexOf(input) == -1);	
		}	
	}	
	
	// updateAnswer(): see where current guess fits in word 	
	private void updateAnswer() {	
		// assume the character is not there 	
		boolean foundMatch = false;	
			
		// go through word character by character and look for 	
		// matches with guess 	
		for (int i = 0; i < word.length(); ++i) {	
			if (word.charAt(i) == guess) { // got a match 	
				foundMatch = true;	
				--lettersLeft;	
				answer.setCharAt(2*i + 1, guess);	
			}	
		}	
			
		// update errors and gameOver as needed 	
		if (! foundMatch) {	
			++errors;	
		}	
		else if (lettersLeft == 0) {	
			gameOver = true;	
		}	
			
		// update GUI 	
		answerPad.setText(answer.toString());	
	}	
	
	// drawContour(): draw the outline of the face 	
	private void drawContour() {	
		int w = facePad.getWidth();	
		int h = facePad.getHeight();	
		int ow = w - 2*OFFSET;	
		int oh = h - 2*OFFSET;	
		Graphics g = facePad.getGraphics();	
		g.setColor(window.getBackground());	
		g.fillOval(OFFSET, OFFSET, ow, oh);	
	}	
	
	// drawLeftEye(): draw the draw left eye 	
	private void drawLeftEye() {	
		int w = facePad.getWidth();	
		int h = facePad.getHeight();	
		int ow = w / 8;	
		int oh = h / 8;	
		Graphics g = facePad.getGraphics();	
		g.setColor(Color.white);	
		g.fillOval(OFFSET + w/4, OFFSET + h/4, ow, oh);	
	}	
	
	// drawNose(): draw the nose for our face 	
	private void drawNose() {	
		int w = facePad.getWidth();	
		int h = facePad.getHeight();	
		int ow = w / 8;	
		int oh = h / 8;	
		Graphics g = facePad.getGraphics();	
		g.setColor(Color.white);	
		g.fillOval(OFFSET + w/2 - ow/2, OFFSET + h/2, ow, oh);	
	}	
	
	// main(): application entry point 	
	public static void main (String[] args) {	
		Smiley game = new Smiley();	
	}	
	
	// ***************** methods to be completed ***************** 	
	
	// default constructor 	
	public Smiley() {	
		// to be completed 	
	}	
	
	// getRandomWord(): choose a random word from word file. file 	
	// begins with number of words n, followed by n words 	
	private String getRandomWord() {	
		// to be completed
		return null;  // placeholder	
	}	
	
	// actionPerformed(): process interaction 	
	public void actionPerformed(ActionEvent e) {	
		// to be completed	
	}	
	// updatePast(): add current guess to list of guesses 	
	private void updatePast() {	
		// to be completed 	
	}	
	
	// resetGuess(): blank out current guess 	
	private void resetGuess() {	
		// to be completed	
	}	
	
	// updateStatus(): if game is over, report results 	
	private void updateStatus() {	
		// to be completed	
	}	
		
	// updateFace(): update the face based on error count 	
	private void updateFace() {				
		// to be completed	
	}	
	
	// drawLeftEyebrow(): draw the draw left eyebrow 	
	private void drawLeftEyebrow() {	
		// to be completed	
	}	
	
	// drawRightEye(): draw the draw right eye 	
	private void drawRightEye() {	
		// to be completed	
	}	
	
	// drawRightEyebrow(): draw the draw right eyebrow 	
	private void drawRightEyebrow() {	
		// to be completed	
	}	
	
	// drawMouth(): draw the mouth for our face 	
	private void drawMouth() {	
		// to be completed	
	}	
}	
	
	
