[SOLVED]storing letters for hangman

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

49 Replies - 3941 Views - Last Post: 04 May 2010 - 12:14 PM Rate Topic: ***-- 2 Votes

#1 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

[SOLVED]storing letters for hangman

Posted 01 May 2010 - 10:19 AM

i have a method like this
 private void createword(JPanel p) throws Exception {
		ArrayList<String> lines = new ArrayList<String>();
		Scanner getWord = new Scanner(new File("hangman"));
		while (getWord.hasNextLine()) {
				lines.add(getWord.nextLine());
			}
		Random rand = new Random();
		String selectedword = rand.toString();
		int selection = rand.nextInt(lines.size());
		
		for(int i = 0; i < lines.get(selection).length(); i++){
			word += " _ ";
			}

		JLabel label = new JLabel(word, SwingConstants.CENTER);
		p.add(label);
		hangword = selectedword.toString();
		
	} 


what i want to know is, how can i make the selected random word in the program be stored in the String hangword like at the bottom of this? do i have it right already?

Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED]storing letters for hangman

#2 ianian112   User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 10:23 AM

not quite
 Random rand = new Random();
                String selectedword = rand.toString();
                int selection = rand.nextInt(lines.size());


You are doing it wrong here because you are getting the toString of the random object, it should be like this
 Random rand = new Random();
                
                int selection = rand.nextInt(lines.size());
String selectedword = lines.get(selection);//Assuming lines holds the words you want



Also
 hangword = selectedword.toString();


can just be
hangword= selectedword;


This post has been edited by ianian112: 01 May 2010 - 10:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 10:34 AM

i'm going to give you both of my codes(my main frame, and then my actin listener), for some reason i can't get the selected word to actually show up on my JOptionPane so i can verify that there is a String that holds my word
import java.awt.*;
import javax.swing.*;

import java.util.*;
import java.io.*;

public class Hangman extends JFrame {
	
	JButton[] buttons;
	int turns;
	int win;
	int lives;
	String word = " ";
	String hangword;
	
	
	
	ImageIcon hang0 = new ImageIcon("hangman0.gif");
	ImageIcon hang1 = new ImageIcon("hangman1.gif");
	ImageIcon hang2 = new ImageIcon("hangman2.gif");
	ImageIcon hang3 = new ImageIcon("hangman3.gif");
	ImageIcon hang4 = new ImageIcon("hangman4.gif");
	ImageIcon hang5 = new ImageIcon("hangman5.gif");
	ImageIcon hang6 = new ImageIcon("hangman6.gif");
	
	public Hangman() throws Exception{
		super("Hangman");
		setSize(500, 550);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new GridLayout(3,1));
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		add(panel1);
		add(panel2);
		add(panel3);
		addbuttons(panel1, panel2, panel3, hangword);
		createword(panel2);
		startingpicture(panel1);
		
		
		
		setVisible(true);
	}
	
	private void addbuttons(JPanel p, JPanel p2, JPanel p3, String s){
		buttons = new JButton[26];
		for(int i = 0; i < 26; i++){
			buttons[i]= new JButton(Character.toString((char) ('a'+i)));
			buttons[i].addActionListener(new HangmanListener(buttons[i] ,this, p, p2, p3, s));
			p3.add(buttons[i]);
		}
	}
	
	private void createword(JPanel p) throws Exception {
		ArrayList<String> lines = new ArrayList<String>();
		Scanner getWord = new Scanner(new File("hangman"));
		while (getWord.hasNextLine()) {
				lines.add(getWord.nextLine());
			}
		Random rand = new Random();
		int selection = rand.nextInt(lines.size());
		String selectedword = lines.get(selection);
		
		for(int i = 0; i < lines.get(selection).length(); i++){
			word += " _ ";
			}

		JLabel label = new JLabel(word, SwingConstants.CENTER);
		p.add(label);
		hangword = selectedword;
		
	}
	
	private void startingpicture(JPanel p){
		JLabel hangmanstart = new JLabel(hang0, SwingConstants.CENTER);
		p.add(hangmanstart);
	}
	
	public void reset() throws Exception{
		this.dispose();
		new Hangman();
	}
	
	
	
	/*private void update(){
		
		
		
	} */
	
	public void updatepicture(JPanel p){
		try{
			switch(lives){
			case 1:JLabel hangman1 = new JLabel(hang1, SwingConstants.CENTER);
			p.add(hangman1); break;
			case 2:JLabel hangman2 = new JLabel(hang2, SwingConstants.CENTER);
			p.add(hangman2); break;
			case 3:JLabel hangman3 = new JLabel(hang3, SwingConstants.CENTER);
			p.add(hangman3); break;
			case 4:JLabel hangman4 = new JLabel(hang4, SwingConstants.CENTER);
			p.add(hangman4); break;
			case 5:JLabel hangman5 = new JLabel(hang5, SwingConstants.CENTER);
			p.add(hangman5); break;
			case 6:JLabel hangman6 = new JLabel(hang6, SwingConstants.CENTER);
			p.add(hangman6); JOptionPane.showMessageDialog(this, "You have run out of guesses", "Game Over", JOptionPane.INFORMATION_MESSAGE);
			reset();break;
			default: break;
			}
		} catch(Exception e){}
	}
	
	/*public void updatepicture(JPanel p){
		try{
		if(lives == 1){
			JLabel hangman1 = new JLabel(hang1, SwingConstants.CENTER);
			p.add(hangman1);
		} else if(lives == 2){
			JLabel hangman2 = new JLabel(hang2, SwingConstants.CENTER);
			p.add(hangman2);
		} else if(lives == 3){
			JLabel hangman3 = new JLabel(hang3, SwingConstants.CENTER);
			p.add(hangman3);
		} else if(lives == 4){
			JLabel hangman4 = new JLabel(hang4, SwingConstants.CENTER);
			p.add(hangman4);
		} else if(lives == 5){
			JLabel hangman5 = new JLabel(hang5, SwingConstants.CENTER);
			p.add(hangman5);
		} else if(lives == 6){
			JLabel hangman6 = new JLabel(hang6, SwingConstants.CENTER);
			p.add(hangman6);
			JOptionPane.showMessageDialog(this, "You have run out of guesses", "Game Over", JOptionPane.INFORMATION_MESSAGE);
			reset();
		}
		} catch(Exception e){}
	}*/
	
	public static void main(String[] args) throws Exception {
		Hangman hang = new Hangman();
	}

}



that's my main code

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

public class HangmanListener implements ActionListener {

	
	JButton place;
	Hangman hang;
	JPanel panel1;
	JPanel panel2;
	JPanel panel3;
	String hangword;
	int lives;
	
	
	public HangmanListener(JButton i, Hangman h, JPanel p, JPanel p1, JPanel p2, String s) {
		place = i;
		hang = h;
		panel1 = p;
		panel2 = p1;
		panel3 = p2;
		hangword = s;
		
	}
	
	public void updatebuttonpicture(){
		place.setEnabled(false);
	}
	
	public void checkletter(){
		String letter = place.getText();
		JOptionPane.showMessageDialog(hang, hangword, "Game Over", JOptionPane.INFORMATION_MESSAGE);
		
		
		
		
	}
	
	

	public void actionPerformed(ActionEvent event){	
		updatebuttonpicture();
		place.getText();
		checkletter();
		hang.updatepicture(panel1);
		//event.getSource();
	}

}

that's my action listener, and i don't know why the word won't show up on the JOptionPane that i make popup with the word to verify there is a word stored in string hangman
Was This Post Helpful? 0
  • +
  • -

#4 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 10:37 AM

It looks like all you need to do is

hangword = lines.get(selection);


this means you can get rid of

String selectedword = rand.toString();


which does nothing but return a string representation of rand.

Edit: looks like I'm a little late to the punch

also, I don't see where you even use HangManListener();

This post has been edited by Luckless: 01 May 2010 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 10:50 AM

it's in the addbuttons method in my main class
Was This Post Helpful? 0
  • +
  • -

#6 ianian112   User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 10:53 AM

its because you assign hangword in createword()
 addbuttons(panel1, panel2, panel3, hangword);
                createword(panel2);


but you call addbuttons first
Was This Post Helpful? 1
  • +
  • -

#7 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 11:06 AM

Thanks ian, i just realized that myself, but now the only problem i am having, is how do i check the stored word for the letter of the button that i pressed?
Was This Post Helpful? 0
  • +
  • -

#8 ianian112   User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 11:11 AM

if (thiString.contains(charPressed))


Was This Post Helpful? 0
  • +
  • -

#9 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 11:22 AM

i came up with
public void checkletter(){
		String word = " ";
		String letter = place.getText();
		//JOptionPane.showMessageDialog(hang, hangword, hangword, JOptionPane.INFORMATION_MESSAGE);
		for(int i = 0; i < hangword.length(); i++){
			if(hangword.charAt(i) == letter.charAt(0)){
				word += " " + letter + " ";
			} else{
				word += " _ ";
			}
		}
		JLabel label = new JLabel(word, SwingConstants.CENTER);
		panel2.add(label);


and the only problem that i seem to be having with this is that, everytime i click a letter, it seems to be addding another word to the panel eg. say i have _ _ _ _ _, if i click the A button, it turns into _ _ _ _ _ _ _ _ _ _ any solution to this problem?
Was This Post Helpful? 0
  • +
  • -

#10 ianian112   User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 11:26 AM

are you deleting/erasing the old JLabel?
Was This Post Helpful? 0
  • +
  • -

#11 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 11:28 AM

how do you delete/erase an existing JLabel?
Was This Post Helpful? 0
  • +
  • -

#12 ianian112   User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 11:36 AM

you can either add all new JLabels to one JPanel, then calling .removeAll() on that JPanel everytime you want to update it. Or you could create one public JLabel that is used by all methods. If you are already doing that you can just call .setText("") on it to clear it
Was This Post Helpful? 1
  • +
  • -

#13 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 11:43 AM

.removeAll() worked, now im having the problem that i need to resize the JFrame for the update to show, how do i fix that?

nevermind, validate() at the end seemed to work alright
Was This Post Helpful? 0
  • +
  • -

#14 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 01:06 PM

  public void checkletter(){
		String word = " ";
		String letter = place.getText();
		//JOptionPane.showMessageDialog(hang, hangword, hangword, JOptionPane.INFORMATION_MESSAGE);
		for(int i = 0; i < hangword.length(); i++){
			if(hangword.charAt(i) == letter.charAt(0)){
				word += " " + letter + " ";
			} else{
				word += " _ ";
				
			}
		}
		
		panel2.removeAll();
		JLabel label = new JLabel(word, SwingConstants.CENTER);
		panel2.add(label);
		panel2.validate();
		
		
	} 


is the code i use to check to see if the letter of the JButton that is pressed is in the word, and if it is then it replaces certain _ _ _ with the actual letters...now the problem that i am having is that once i click a letter, how do i store it inside of something so when i click another button, it will check the code for the currently pressed button, and the buttons i have previously pressed....and if the button that i click is not in the word, how do i make it to where it will update an int value so i can pass it in another method to update the hangman picture?(add arms, head, etc.. that i have stored in imageicons)
Was This Post Helpful? 0
  • +
  • -

#15 slixtrix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: [SOLVED]storing letters for hangman

Posted 01 May 2010 - 01:08 PM

  public void checkletter(){
		String word = " ";
		String letter = place.getText();
		//JOptionPane.showMessageDialog(hang, hangword, hangword, JOptionPane.INFORMATION_MESSAGE);
		for(int i = 0; i < hangword.length(); i++){
			if(hangword.charAt(i) == letter.charAt(0)){
				word += " " + letter + " ";
			} else{
				word += " _ ";
				
			}
		}
		
		panel2.removeAll();
		JLabel label = new JLabel(word, SwingConstants.CENTER);
		panel2.add(label);
		panel2.validate();
		
		
	} 


is the code i use to check to see if the letter of the JButton that is pressed is in the word, and if it is then it replaces certain _ _ _ with the actual letters...now the problem that i am having is that once i click a letter, how do i store it inside of something so when i click another button, it will check the code for the currently pressed button, and the buttons i have previously pressed....and if the button that i click is not in the word, how do i make it to where it will update an int value so i can pass it in another method to update the hangman picture?(add arms, head, etc.. that i have stored in imageicons)
Was This Post Helpful? -1
  • +
  • -

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