Start New Discussion within our Software Development Community

<HTML>
<BODY>
<APPLET CODE= MyHangman.class WIDTH=1000 HEIGHT=1000>
</APPLET>
</HTML>

I need to know what to do next below is my java file and I am confused with how to check the words i have in the array and the letters guessed in the word....anything would be useful thanks......and do you guys think that an ActionListener would be the easiest to see which button is clicked

Attachments
/**
 * @(#)Hangman.java
 *
 * Hangman Applet application
 *
 * @author 
 * @version 1.00 2008/5/27
 */
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JOptionPane;

public class MyHangman extends Applet  {
 
 
 private boolean usd[] = new boolean[26];

 private String guess;

 private int numguesses = 0;

 private boolean finished = false;

 private boolean won = false;

 private Button b[];
 
 char secretWord[];//holds letters in word
 
 char word[];//holds letters guessed in word
 
 char wrongWord[];

	
	public void init() {
	
		
		int a;
		StringBuffer buff = new StringBuffer();
		
		
		setLayout ( new FlowLayout());
		b = new Button[26];
		
		for(a = 0; a < 26; a++)
		{
			buff = new StringBuffer();
			buff.append((char)(a+65));
			b[a] = new Button(buff.toString());
			//b[a].addActionListener(this);}
			
			add(b[a]);//adds all of the buttons
			//guess = getParameter("wrd").toUpperCase();
			
			
		}
		
	}
	
	public void paint(Graphics g)
	{
		setBackground(Color.orange);
		
		g.fillRect(450, 700, 200, 20);
		g.fillRect(450,550,10,100);
		g.fillRect(450,300,20,400);
		g.fillRect(450,300,150,20);
		g.setColor(Color.blue);
		g.fillRect(525,320,25,75);//draws the gallows
		g.setColor(Color.white);
		/*g.drawRect(535,445,10,100);
		g.drawOval(515,395,50,50);
		g.drawRect(545,475,30,5);
		g.drawRect(505,475,30,5);
		g.drawOval(525,410,5,5);
		g.drawOval(540,410,5,5);
		g.drawRect(530,420,6,6);
		g.setColor(Color.black);
		g.drawRect(530,440,18,1);*/
	
		
		if ( numguesses >= 1 )
			g.setColor(Color.white);
			g.fillOval(515,395,50,50);
			
			g.setColor(Color.blue);
			g.fillOval(525,410,5,5);
			g.fillOval(540,410,5,5);
			g.drawRect(530,437,18,1);
			g.drawRect(530,420,6,6);
			
			
			g.setColor(Color.red);
		
		if ( numguesses >= 2)
			g.fillRect(535,445,10,100);
			
		if ( numguesses >= 3)
			g.fillRect(545,475,30,5);
			
		if ( numguesses >= 4)
			g.fillRect(505,475,30,5);
			
		if ( numguesses >= 5)
			g.drawLine(535,540,500,570);
			
		if( numguesses >= 6)
			g.drawLine(580,570,545,540);		
			
			StringBuffer wrong = new StringBuffer();
			
			  for (int l = 0; l<=25; l++) {
              if (usd[l]) wrong.append((char)(l+65));
              else wrong.append(".");
	
			}
			
			g.setColor(Color.green);//Setting up the incorrect guessed letters
			
			Font f = new Font("Palatino",Font.BOLD+Font.ITALIC,65);
			g.setFont(f);
			g.drawString(wrong.toString(),300,850);//end of writing the wrong letters
		
			StringBuffer right = new StringBuffer();
									
			Font ff = new Font("Palatino",Font.BOLD,85);
			g.setColor(Color.black);
			
			g.setFont(ff);
			
				for (int ii = 0; ii < guess.length(); ii++)
				if(usd[(int)guess.charAt(ii)-65])
					right.append(guess.charAt(ii));//Marks down the right answers
					else
					right.append(".");	//If the same letter is used twice it won't be put down
										//or counted
		
			g.drawString(right.toString(),600,400);	
				
				
			if(numguesses >= 6)
			{
				JOptionPane.showMessageDialog(null, " Wow you just got your buddy hung nice job");
				finished = true;
			}
  
  			if(won)
  			{
  				JOptionPane.showMessageDialog(null, " Phew your buddy lives another day");
  			}




    }




			
			
		
		
			
			
	String wordList[] = {
		"BEAU",
		"SHERLOCK",
		"ROBIN",
		"LUKE",
		"AMY",
		"JASON",
		"AUGUSTIN",
		"FAITH",
		"BRANDON",
		"JENNIFER",
		"JAVA",
		"GREENE",
		"MORTON",
		"BLODGETT",
		"HOLMES",
		"PAPWORTH",
		"PETERSEN",
		"CHAMBERS",
		"HARRAL",
		"CAMPOS",};				
}	//end of main

Keep in mind that swing is event-driven.

In order to check a word for a character, you should create a method that iterates through each character of that word.

public boolean charIsInWord(String word, char arg)
{
char[] charWord = word.toCharArray();

//iterate through the values of the char array and compare them to the char argument

//return true if a match was found, or return false if no match was found
}

If this method returns false when someone clicks a button then something needs to happen. Like I said, swing is event driven so your buttons need their ActionListener interfaces set to non-null so their ActionListener.actionPerformed(ActionEvent e) methods can fire.

Basically, you need to implement the ActionListener interface, one way or another (either through an interface implementation directly to your class, to an interface object, in an enum, in some other class that supports buttons... any of those ways work!) and setActionCommand(String arg) for each button.

This question has already been answered. Start a new discussion instead.