created game and have a question regarding once the player has won how

Topics merged. for a fourth time Please avoid double posting

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

46 Replies - 2754 Views - Last Post: 09 December 2008 - 10:35 PM Rate Topic: -----

#1 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

created game and have a question regarding once the player has won how

Posted 09 December 2008 - 03:56 PM

this is the game ive created almost finished have two more things to add and those are messages that when the player guesses the word in the right number of guesses to display you win and if not then you loose. i think it should be in my validate() method. any assistence would be greatly appreciated




import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

public class JavaHangMan extends JFrame{

	//declaring variables before starting.......
	private JButton myButton[];
	private JLabel myLabel[];
 	private String myAlpha[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
 	private String myWords[] = {"HAPPY", "PROGRAMMING", "JAVA", "GRADUATE", "EQUINOX", "IRONY", "KINETIC", "HANGMAN", "CARTOON", "CHARACTER"};
 	private JLabel thisLabel[];
 	private JMenuItem about;
 	private JMenuItem quit;
 	private JLabel lbl1;
 	private JLabel lbl2;
 	private String GuessWord;
 	private JMenuItem start;
 	private String images[] = {"Gallow.gif", "1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif"};
 	private ImageIcon images1[];
 	private int numOfGuesses=0;
	private String Fname;
	private JLabel lbl3;
	private int score = 0;
	
	
	
	public static void main(String[] args) {
		
   	//Creating Frame....
		JavaHangMan frame = new JavaHangMan();
 	   	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 	   	frame.setSize(800, 700);
		frame.setVisible(true);
		}
	
	//constructor for the game.......
	 public JavaHangMan(){
		
		super ("HangMan");
		Container pane = getContentPane();
		pane.setLayout(null);
	
	//creating a message box to enter the players name and display it to the screen.......
		lbl2 = new JLabel();
		Fname = JOptionPane.showInputDialog(null, "Welcome To HangMan, Please Enter Your Name:");
		lbl2.setText(Fname);
		lbl2.setBounds(500, 20, 450, 250);
		pane.add(lbl2);
		
		
		
	// assigning the image array......
		images1 = new ImageIcon[7];
		for(int i = 0; i <7; i++)
		{
			images1[i] = new ImageIcon(images[i]); 
		}
		
	//score display
		lbl3 = new JLabel("Player's Score: " + score);
		lbl3.setBounds(500, 60, 450, 250);
		pane.add(lbl3);
		
	// setting the image to the game of the gallow.........added to the start file with setVisible down the page....
		lbl1 = new JLabel();	
				ImageIcon icon = new ImageIcon("Gallow.gif");
				lbl1.setBounds(100, 20, 450, 450);
				lbl1.setIcon(icon);
				pane.add(lbl1);
				
				lbl1.setVisible(false);
	//pane.setBackground(Color.blue);
		
		
	//Creating a menu bar and top level menus.....
			handler myHandler = new handler();
		
			JMenuBar menuBar = new JMenuBar();
			JMenu fileMenu = new JMenu("File");
		
			fileMenu.setMnemonic('F');
		
			JMenu optionsMenu = new JMenu("Options");
			optionsMenu.setMnemonic('O');
			menuBar.add(fileMenu);
			menuBar.add(optionsMenu);
			this.setJMenuBar(menuBar);
			
			start = new JMenuItem("Start", 'S');
			fileMenu.add(start);
			start.addActionListener(myHandler);
			
			JMenuItem stop = new JMenuItem("Stop", 'T');
			fileMenu.add(stop);
		
			quit = new JMenuItem("Quit", 'Q');
			fileMenu.add(quit);
			quit.addActionListener(new handler());
			
			about = new JMenuItem("About", 'A');
			optionsMenu.add(about);
			about.addActionListener(new handler());
			
			
   	//create the keyboard for choosing letters...an array of letters and buttons was created and positioned.....
			myButton = new JButton[26];
			for (int i=0; i<26; i++)
			{
			myButton[i]=new JButton(myAlpha[i]);
			if(i<10)
			{
			myButton[i].setBounds(100 +(i*55), 550, 50, 20);
			}
			else if(i>9 && i <20)
			{
			myButton[i].setBounds(100 +((i-10)*55), 575, 50, 20);
			}
			else
			{
			myButton[i].setBounds(100 +((i-20)*55), 600, 50, 20);
			}
			pane.add(myButton[i]);
			myButton[i].addActionListener(myHandler);
			}
			
	//Creating a label to show the words in the array and the underscores beneath them......
			myLabel = new JLabel[20];
			thisLabel = new JLabel[20];
			for( int i=0; i<20; i++){
				myLabel[i] = new JLabel(" ");
				thisLabel[i] = new JLabel("_");
				myLabel[i].setBounds(140 +(i*25), 475, 20, 20);
				thisLabel[i].setBounds(140 + (i*25), 500, 20, 20);
				pane.add(myLabel[i]);
				pane.add(thisLabel[i]);
				myLabel[i].setVisible(false);
				thisLabel[i].setVisible(false);
				}
			
			
	}
	
		public void StartGame(){
	
	//Creating a method to call a random word from the Word array......
			
				
				
				Initalize();
				Random randomWords = new Random();
				int face;
				face = 1 + randomWords.nextInt(9);
				GuessWord = myWords[face];
				lbl1.setVisible(true);
				lbl2.setVisible(true);
				for (int i = 0; i < GuessWord.length(); i++ ){
				thisLabel[i].setVisible(true);
				myLabel[i].setText("" + GuessWord.charAt(i));
				}
			}
			
	//creates a method called Validate that sets the letter visible if guessed properly
		public void Validate(String s){
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
			}
			if (flag == false)
			{
				numOfGuesses++;
				lbl1.setIcon(images1[numOfGuesses]);
				//JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
				
			}
	}
		
	//creates a method to hide everything till game has started
		public void Initalize(){
			for( int i = 0; i < 20; i++)
			{
				thisLabel[i].setVisible(false);
				myLabel[i].setVisible(false);
			}
				lbl1.setVisible(false);
			  
				lbl1.setIcon(images1[0]);	  
		}
		
	//Creating an ActionListener
		class handler implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
	//created an ActionListener to read what buttons are pressed for the keyboard on screen.....
			
			for (int i=0; i<26; i++){
				if (e.getSource() == myButton[i])
				{
				Validate(myAlpha[i]);
				
				}
			}
			
	//start game ActionListener calling the StartGame Method....
			if (e.getSource()== start){
				StartGame();
				
			}
	
	//created an ActionListener to exit out of the application....
			if(e.getSource() == quit)
			{
			
			JOptionPane.showMessageDialog(null, "GoodBye, \nThank You For Playing HangMan!");
			System.exit(0);
			}
	
	//created an ActionListener to display a message on how to play the game......
			else if (e.getSource() == about)
			{
			JOptionPane.showMessageDialog(null, "How To Play HangMan:\nTo Play HangMan Guess The Word Before Your Man Gets Hung!\nGuess the word by choosing letters on the screen with your mouse as you choose \nif you get letters wrong your man will get body parts added once he is a full body you \nhave lost the game, you have 6 tries. \nGood Luck!");
			
			}
		}	
	}	
		
}
   


Is This A Good Question/Topic? 0
  • +

Replies To: created game and have a question regarding once the player has won how

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:06 PM

Yes this would be in the Validate().
To display the lose message you would check the guess, after you increment the guess and set the image you would check to see if guesses was greater than the max number of guesses, if it is display the lose message, if not continue on.

For the win message you could add an else to the if statement and check to see if all letters are correctly guessed, if they are display the win message if not continue.
Was This Post Helpful? 0
  • +
  • -

#3 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:14 PM

View PostBigAnt, on 9 Dec, 2008 - 03:06 PM, said:

Yes this would be in the Validate().
To display the lose message you would check the guess, after you increment the guess and set the image you would check to see if guesses was greater than the max number of guesses, if it is display the lose message, if not continue on.

For the win message you could add an else to the if statement and check to see if all letters are correctly guessed, if they are display the win message if not continue.



im not to sure i understand what you are trying to tell me too do sorry lol im really new at this ive only been doing java for a month. i already have it display a pi when they guess wrong. but not once they reach the limit of 6 pics i want them to receive a message saying "you lost".

do i create another counter?
or do i create another if statement saying if(numOfGuessses =>6) JOptionPane.ShowDialog("You Lose");

see what i mostly want is to have it explained to me what im doing and why im doing it this way. my teacher just tells us what to do he never fully eplains why we are doing it and im not learning very well
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:15 PM

General idea:

//tally total guesses

if(totalGuesses > MAX_GUESSES) //OR (totalGuesses == MAX_GUESSES) depends on your implementation
   loseMessage(); //perhaps its a function, perhaps its not
else if (allLettersAreGuessedCorrectly)
	winMessage(); //same as lose, could be a function
else
   //continue the game?


This post has been edited by KYA: 09 December 2008 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:19 PM

View PostKYA, on 9 Dec, 2008 - 03:15 PM, said:

General idea:

//tally total guesses

if(totalGuesses > MAX_GUESSES) //OR (totalGuesses == MAX_GUESSES) depends on your implementation
   loseMessage(); //perhaps its a function, perhaps its not
else if (allLettersAreGuessedCorrectly)
	winMessage(); //same as lose, could be a function
else
   //continue the game?




ok this will sound weird i understand what you wroe totally but i still dont understand how to incorporate that into my validate method. sorry
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:21 PM

	public void Validate(String s){
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
			}
			if (flag == false)
			{
				numOfGuesses++;
				 if (numGuesses == MAX_GUESSES) //HERE
					   JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
				lbl1.setIcon(images1[numOfGuesses]);
			}
	}


Was This Post Helpful? 1
  • +
  • -

#7 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:23 PM

View PostKYA, on 9 Dec, 2008 - 03:21 PM, said:

	public void Validate(String s){
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
			}
			if (flag == false)
			{
				numOfGuesses++;
				 if (numGuesses == MAX_GUESSES) //HERE
					   JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
				lbl1.setIcon(images1[numOfGuesses]);
			}
	}




oh ok wow that makes sense lol thank you so much i really appreciate this im so glad i signed up for this site ive learned more here then i have in class lol which is bad to say
Was This Post Helpful? 0
  • +
  • -

#8 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:38 PM

ok i got the you loose statement working now when the guy guesses 6 time wrongs the messga pops up and write you loose try again. now i also want to add a you win if he guesses correctly some words arre 11 characters long so i cant put in the same code to write you win right i need to write it in the upper half of my validate message coreect?
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:42 PM

This is how I would go about doing that. See code comments.

public void Validate(String s){
			boolean flag = false;
		   //add a counter to see if all letters match if so == win
		   int numRight = 0;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				score++;
				numRight++; //increment the counter each time for a correct letter
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
			}
			if (flag == false)
			{
				numOfGuesses++;
				 if (numGuesses == MAX_GUESSES) //HERE
					   JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
				lbl1.setIcon(images1[numOfGuesses]);
			}
			else //if flag is true, meaning there is at least one letter that matches
			{
					if(numRight == totalLettersInTheWord)
						 //print out a win message, JOptionPane, etc...
			}
	}


This post has been edited by KYA: 09 December 2008 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#10 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 04:55 PM

View PostKYA, on 9 Dec, 2008 - 03:42 PM, said:

This is how I would go about doing that. See code comments.

public void Validate(String s){
			boolean flag = false;
		   //add a counter to see if all letters match if so == win
		   int numRight = 0;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				score++;
				numRight++; //increment the counter each time for a correct letter
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
			}
			if (flag == false)
			{
				numOfGuesses++;
				 if (numGuesses == MAX_GUESSES) //HERE
					   JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
				lbl1.setIcon(images1[numOfGuesses]);
			}
			else //if flag is true, meaning there is at least one letter that matches
			{
					if(numRight == totalLettersInTheWord)
						 //print out a win message, JOptionPane, etc...
			}
	}




ok im kinda stuck how would i declare the totaLettersinWord?
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 05:05 PM

private String GuessWord;



This is what you have as the guess word, therefore:

 if(numRight == GuessWord.length()) //String function
						 //print out a win message, JOptionPane, etc...




edit: double post on accident, erased. DIC is being slow today

This post has been edited by KYA: 09 December 2008 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

#12 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 05:08 PM

View PostKYA, on 9 Dec, 2008 - 04:05 PM, said:

private String GuessWord;



This is what you have as the guess word, therefore:

 if(numRight == GuessWord.length()) //String function
						 //print out a win message, JOptionPane, etc...




edit: double post on accident, erased. DIC is being slow today



i get an error not sure why though here is what i have put

//creates a method called Validate that sets the letter visible if guessed properly
		public void Validate(String s)
		{
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				NumRight++;
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
				
			}
			if (flag == false)
			{
				numOfGuesses++;
				
				if (numOfGuesses == Maxguesses)
				{
				JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
				lbl1.setIcon(images1[numOfGuesses]);
				}
				
				else
				{
				
				if (NumRight == GuessWord)
				{
				OptionPane.showMessageDialog("Congratulations, You Win!");

				}
				  

				}
				
			}
		}

Was This Post Helpful? 0
  • +
  • -

#13 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 05:15 PM

i fixed the one error that was just a simple mistake with JoptionPane i had optionpane and i forgot the null. but i get an error pointing at the NumRight == GuessWord
Was This Post Helpful? 0
  • +
  • -

#14 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Posted 09 December 2008 - 05:28 PM

now i dont get any error i have fixed the guessword i was supposed to put guessword.legth() my mistake but the message box does not show up saying you win can anyone help me?
here is my code maybe i missed something:

 //creates a method called Validate that sets the letter visible if guessed properly
		public void Validate(String s)
		{
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				NumRight++;
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
				
			}
			if (flag == false)
			{
				numOfGuesses++;
				lbl1.setIcon(images1[numOfGuesses]);
				if (numOfGuesses == Maxguesses)
				{
				JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
			
				}
				
			
			
			
				else
				{
				
				if (NumRight == GuessWord.length())
				{
				JOptionPane.showMessageDialog(null, "Congratulations, You Win!");

				}

				}
				  

				
				
			}
		}

Was This Post Helpful? 0
  • +
  • -

#15 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: created game and have a question regarding once the player has won how

Post icon  Posted 09 December 2008 - 06:26 PM

i created a hangman game and its finshed exect one thing is not working properly when the player guesses the word a message box should pop and ya you win but once the player has guessed the word it waits for another letter to be chosen b4 it pops up and says you win. here is my code any helpful hints would be appreciated its under the Validate method:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

public class JavaHangMan extends JFrame{

	//declaring variables before starting.......
	private JButton myButton[];
	private JLabel myLabel[];
 	private String myAlpha[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
 	private String myWords[] = {"HAPPY", "PROGRAMMING", "JAVA", "GRADUATE", "EQUINOX", "IRONY", "KINETIC", "HANGMAN", "CARTOON", "CHARACTER"};
 	private JLabel thisLabel[];
 	private JMenuItem about;
 	private JMenuItem quit;
 	private JLabel lbl1;
 	private JLabel lbl2;
 	private String GuessWord;
 	private JMenuItem start;
 	private String images[] = {"Gallow.gif", "1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif"};
 	private ImageIcon images1[];
 	private int numOfGuesses=0;
	private String Fname;
	private JLabel lbl3;
	private int score = 0;
	private int Maxguesses = 6;
	private int numRight;
	
	public static void main(String[] args) {
		
   	//Creating Frame....
		JavaHangMan frame = new JavaHangMan();
 	   	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 	   	frame.setSize(800, 700);
		frame.setVisible(true);
		}
	
	//constructor for the game.......
	 public JavaHangMan(){
		
		super ("HangMan");
		Container pane = getContentPane();
		pane.setLayout(null);
	
	//creating a message box to enter the players name and display it to the screen.......
		lbl2 = new JLabel();
		Fname = JOptionPane.showInputDialog(null, "Welcome To HangMan, Please Enter Your Name:");
		lbl2.setText(Fname);
		lbl2.setBounds(500, 20, 450, 250);
		pane.add(lbl2);
		
		
		
	// assigning the image array......
		images1 = new ImageIcon[7];
		for(int i = 0; i <7; i++)
		{
			images1[i] = new ImageIcon(images[i]); 
		}
		
	//score display
		lbl3 = new JLabel("Player's Score: " + score);
		lbl3.setBounds(500, 60, 450, 250);
		pane.add(lbl3);
		
	// setting the image to the game of the gallow.........added to the start file with setVisible down the page....
		lbl1 = new JLabel();	
				ImageIcon icon = new ImageIcon("Gallow.gif");
				lbl1.setBounds(100, 20, 450, 450);
				lbl1.setIcon(icon);
				pane.add(lbl1);
				
				lbl1.setVisible(false);
	//pane.setBackground(Color.blue);
		
		
	//Creating a menu bar and top level menus.....
			handler myHandler = new handler();
		
			JMenuBar menuBar = new JMenuBar();
			JMenu fileMenu = new JMenu("File");
		
			fileMenu.setMnemonic('F');
		
			JMenu optionsMenu = new JMenu("Options");
			optionsMenu.setMnemonic('O');
			menuBar.add(fileMenu);
			menuBar.add(optionsMenu);
			this.setJMenuBar(menuBar);
			
			start = new JMenuItem("Start", 'S');
			fileMenu.add(start);
			start.addActionListener(myHandler);
			
			JMenuItem stop = new JMenuItem("Stop", 'T');
			fileMenu.add(stop);
		
			quit = new JMenuItem("Quit", 'Q');
			fileMenu.add(quit);
			quit.addActionListener(new handler());
			
			about = new JMenuItem("About", 'A');
			optionsMenu.add(about);
			about.addActionListener(new handler());
			
			
   	//create the keyboard for choosing letters...an array of letters and buttons was created and positioned.....
			myButton = new JButton[26];
			for (int i=0; i<26; i++)
			{
			myButton[i]=new JButton(myAlpha[i]);
			if(i<10)
			{
			myButton[i].setBounds(100 +(i*55), 550, 50, 20);
			}
			else if(i>9 && i <20)
			{
			myButton[i].setBounds(100 +((i-10)*55), 575, 50, 20);
			}
			else
			{
			myButton[i].setBounds(100 +((i-20)*55), 600, 50, 20);
			}
			pane.add(myButton[i]);
			myButton[i].addActionListener(myHandler);
			}
			
	//Creating a label to show the words in the array and the underscores beneath them......
			myLabel = new JLabel[20];
			thisLabel = new JLabel[20];
			for( int i=0; i<20; i++){
				myLabel[i] = new JLabel(" ");
				thisLabel[i] = new JLabel("_");
				myLabel[i].setBounds(140 +(i*25), 475, 20, 20);
				thisLabel[i].setBounds(140 + (i*25), 500, 20, 20);
				pane.add(myLabel[i]);
				pane.add(thisLabel[i]);
				myLabel[i].setVisible(false);
				thisLabel[i].setVisible(false);
				}
			
			
	}
	
		public void StartGame(){
	
	//Creating a method to call a random word from the Word array......
			
				
				
				Initalize();
				Random randomWords = new Random();
				int face;
				face = 1 + randomWords.nextInt(9);
				GuessWord = myWords[face];
				lbl1.setVisible(true);
				lbl2.setVisible(true);
				for (int i = 0; i < GuessWord.length(); i++ ){
				thisLabel[i].setVisible(true);
				myLabel[i].setText("" + GuessWord.charAt(i));
				}
			}
			
	//creates a method called Validate that sets the letter visible if guessed properly
		public void Validate(String s)
		{
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				numRight++;
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;	
				}
				
			}
			if (flag == false)
			{
				numOfGuesses++;
				lbl1.setIcon(images1[numOfGuesses]);
				if (numOfGuesses == Maxguesses)
				{
				JOptionPane.showMessageDialog(null, "You Lose,Sorry Try Again!");
				
				}
				else if (numRight == GuessWord.length())
				{
				JOptionPane.showMessageDialog(null, "Congratulations, You Win!");
				
				}

				
			 }
		}
	
	
	//creates a method to hide everything till game has started
		public void Initalize(){
			for( int i = 0; i < 20; i++)
			{
				thisLabel[i].setVisible(false);
				myLabel[i].setVisible(false);
			}
				lbl1.setVisible(false);
			  
				lbl1.setIcon(images1[0]);	  
		}

		
	//Creating an ActionListener
		class handler implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
	//created an ActionListener to read what buttons are pressed for the keyboard on screen.....
			
			for (int i=0; i<26; i++){
				if (e.getSource() == myButton[i])
				{
				Validate(myAlpha[i]);
				myButton[i].setEnabled(false);
				
				}
			}
			
	//start game ActionListener calling the StartGame Method....
			if (e.getSource()== start){
				StartGame();
				
			}
	
	//created an ActionListener to exit out of the application....
			if(e.getSource() == quit)
			{
			
			JOptionPane.showMessageDialog(null, "GoodBye, \nThank You For Playing HangMan!");
			System.exit(0);
			}
	
	//created an ActionListener to display a message on how to play the game......
			else if (e.getSource() == about)
			{
			JOptionPane.showMessageDialog(null, "How To Play HangMan:\nTo Play HangMan Guess The Word Before Your Man Gets Hung!\nGuess the word by choosing letters on the screen with your mouse as you choose \nif you get letters wrong your man will get body parts added once he is a full body you \nhave lost the game, you have 6 tries. \nGood Luck!");
			
			}
		}	
	}	
		
}
   
  
 
	
		
	
  

		

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »