13 Replies - 893 Views - Last Post: 21 February 2010 - 01:19 PM Rate Topic: -----

#1 tuxx223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 10

Help with Hangman!

Posted 20 February 2010 - 08:57 AM

Hi! I am working on a basic hangman game.

where user enters a word and it asks user whether user wants to remove doubletons, vowels etc.

If the string is manipulated: for example:

original string= letter manipulated=ltr

The program then asks user to enter guess and user can enter letter by letter or word at once...
{
			
				StringBuffer uscore=new StringBuffer(manipulated.replaceAll(".","_ ")); //replace manipulated with underscores
				guess=JOptionPane.showInputDialog(uscore+prompt);

				if (guess.length()<2) //if guess is single letter
				{
					for (int i=0;i<original.length();i++) //original word length time iteration
					{
						if ((guess.charAt(0)==manipulated.charAt(i)))
						{
							uscore.setCharAt(i,guess.charAt(0));
							guess=JOptionPane.showInputDialog(uscore+prompt);
							String word5=uscore.toString();
							if (word5.equalsIgnoreCase(word4))
							{
								JOptionPane.showMessageDialog(null,"You guessed correctly.");
								//break;
			
							}
							
						}					
					
						
					}
					JOptionPane.showInputDialog(uscore+prompt);
				}
			
			}
}



Now the problem is if user enters "letter" letter by letter it shows index out of bound. My aim is even if user eneters entire original string than corrosponding letters of it in manipulated should be revealed.

In short I want to compare input char with original string but display only it on JOptionPane if it's present in the manipulated string...

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Hangman!

#2 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Help with Hangman!

Posted 20 February 2010 - 12:45 PM

put it inside the code tag.....
Was This Post Helpful? 0
  • +
  • -

#3 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Help with Hangman!

Posted 20 February 2010 - 12:50 PM

:code:
Was This Post Helpful? 0
  • +
  • -

#4 tuxx223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 10

Re: Help with Hangman!

Posted 20 February 2010 - 02:47 PM

View Postgl3thr0, on 20 February 2010 - 11:50 AM, said:

:code:


{

StringBuffer uscore=new StringBuffer(manipulated.replaceAll(".","_ ")); //replace manipulated with underscores
guess=JOptionPane.showInputDialog(uscore+prompt);

if (guess.length()<2) //if guess is single letter
{
for (int i=0;i<original.length();i++) //original word length time iteration
{
if ((guess.charAt(0)==manipulated.charAt(i)))
{
uscore.setCharAt(i,guess.charAt(0));
guess=JOptionPane.showInputDialog(uscore+prompt);
String word5=uscore.toString();
if (word5.equalsIgnoreCase(word4))
{
JOptionPane.showMessageDialog(null,"You guessed correctly.");
//break;

}

}


}
JOptionPane.showInputDialog(uscore+prompt);
}

}
}


Was This Post Helpful? 0
  • +
  • -

#5 tuxx223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 10

Re: Help with Hangman!

Posted 20 February 2010 - 03:14 PM

View Posttuxx223, on 20 February 2010 - 07:57 AM, said:

Hi! I am working on a basic hangman game.

where user enters a word and it asks user whether user wants to remove doubletons, vowels etc.

If the string is manipulated: for example:

original string= letter manipulated=ltr

The program then asks user to enter guess and user can enter letter by letter or word at once...
{
			
				StringBuffer uscore=new StringBuffer(manipulated.replaceAll(".","_ ")); //replace manipulated with underscores
				guess=JOptionPane.showInputDialog(uscore+prompt);

				if (guess.length()<2) //if guess is single letter
				{
					for (int i=0;i<original.length();i++) //original word length time iteration
					{
						if ((guess.charAt(0)==manipulated.charAt(i)))
						{
							uscore.setCharAt(i,guess.charAt(0));
							guess=JOptionPane.showInputDialog(uscore+prompt);
							String word5=uscore.toString();
							if (word5.equalsIgnoreCase(word4))
							{
								JOptionPane.showMessageDialog(null,"You guessed correctly.");
								//break;
			
							}
							
						}					
					
						
					}
					JOptionPane.showInputDialog(uscore+prompt);
				}
			
			}
}



Now the problem is if user enters "letter" letter by letter it shows index out of bound. My aim is even if user eneters entire original string than corrosponding letters of it in manipulated should be revealed.

In short I want to compare input char with original string but display only it on JOptionPane if it's present in the manipulated string...

Was This Post Helpful? 0
  • +
  • -

#6 tuxx223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 10

Re: Help with Hangman!

Posted 20 February 2010 - 03:47 PM

Hi guys!

I am trying to make a basic hangman program. So far I have done following:

import java.lang.String;
import javax.swing.*;
import java.lang.StringBuffer;
import java.util.Random;
public class StringGame
{
	
	
	
	static String word,word1,word3,word2,word4,guess;
	static int w3=0,w2=0,w1=0,s;
	public static Random r;
	
	public static void main(String args[])
	{	

		StringGame str=new StringGame();
		
		
		//For a dialog box asking for a word
		word=JOptionPane.showInputDialog(null, "Please enter a word");

		//Just to confirm entered word
		System.out.println("The entered word is:"+word);

		//if word length is more then 2 letters display method 1 option
		if ((word.length()>=2))
		{
			w3=JOptionPane.showConfirmDialog(null,"Do you want to run method 1 on string you just input?","Do you want to run method 1 on string you just input?", JOptionPane.YES_NO_CANCEL_OPTION);
			System.out.println("The value returned is:"+w3);
		}
		//if user enters less then 2 chars
		else
		{
			JOptionPane.showMessageDialog(null, "Please enter a word greater then 2 letters");

		}


		//If pressed Yes executes removeDoubletons
		if (w3==0)
		{
			str.removeDoubletons(word);
			System.out.println("The word is:"+word1);
		}
		else
		{
			word1=word;
		}

		//Prompt for running method 2
		if ((word.length()>=2) && ((w3==0)||(w3==1)||(w3==2)))
		{
					w2=JOptionPane.showConfirmDialog(null,"Do you want to run method 2 on string you just input?","Do you want to run method 2 on string you just input?", JOptionPane.YES_NO_CANCEL_OPTION);
					System.out.println("The value returned is:"+w2);
		}
		//If user presses Yes, it executes randomMove
		if (w2==0)
		{
			str.randomMove(word1);
			System.out.println("Word after random move: "+word2);
		}
		else
		{
			word2=word1;
		}
	
		//prompt for running method 3
		if ((word.length()>=2) && ((w2==0)||(w2==1)||(w2==2)))
		{
					w1=JOptionPane.showConfirmDialog(null,"Do you want to run method 3 on string you just input?","Do you want to run method 3 on string you just input?", JOptionPane.YES_NO_CANCEL_OPTION);
					System.out.println("The value returned is:"+w1);
		}
		//If user presses Yes then it executes removeVowels method
		if (w1==0)
		{
			str.removeVowels(word2);
		}
		else
		{
			word4=word2;
		}
		String prompt="\n Please enter a single character or a word as your final guess";
		if (w1==1)//NON MANIPULATED STRING
		{
			//New StringBuffer for word
			String sbx=word;
			//Replacing all characters of sbx with _
			StringBuffer sb2=new StringBuffer(sbx.replaceAll(".","_ "));
			//Concatanates _ of original string length and prompt
			String prompt2=sb2+prompt;
			System.out.println("sb2 is"+sb2);
			//Displays original _s and propmt
			guess=JOptionPane.showInputDialog(prompt2);
		}
		
			if (w1==0)//MANIPULATED STRING
			{
			
				//New stringbuffer stores _s of length of manipulated string
				StringBuffer uscore=new StringBuffer(word4.replaceAll(".","_ "));
				System.out.println("New Word 4 is:"+word4);
				guess=JOptionPane.showInputDialog(uscore+prompt);

				if (guess.length()<2)
				{
					for (int i=0;i<word.length();i++)
					{
						for (int j=0;j<word4.length();j++)
						{				
								
							if ((guess.charAt(0)==word4.charAt(j))&& (guess.charAt(0)==word.charAt(i)))
							{
								uscore.setCharAt(j*2,guess.charAt(0));
								guess=JOptionPane.showInputDialog(uscore+prompt);	
								String word5=uscore.toString();
								word5=word5.replaceAll("\\s+","");
								if (word5.equalsIgnoreCase(word4))
								{
									JOptionPane.showMessageDialog(null,"You guessed correctly.");
									break;
			
								}
							
							}					
					
						
						}
						//JOptionPane.showInputDialog(uscore+prompt);
					}
			
				}

			}

		if (guess.equalsIgnoreCase(word))
		{

			JOptionPane.showMessageDialog(null,"You guessed correctly.");
			
		}
		else
		{	
			JOptionPane.showMessageDialog(null,"You guessed incorrectly.");
		}
	

		//Char[] wordChars = word.toCharArray();
		//System.out.println("The word is:"+word);
		
	}

	public static void removeDoubletons(String w2)
	{
		
		StringBuffer sb=new StringBuffer(word);
		String r = "";
		for (int i = 0; i <sb.length()-1; i ++)
		{
		
            		if (sb.charAt(i)==sb.charAt(i+1))
			{
								
				
				//r=r+sb.charAt(i);
				sb=sb.deleteCharAt(i+1);
				word1=sb.toString();
				
				//word=word.replace(r,"")+word.substring(i+1);
				
			}
			else
			{
				//word1=word;
			}			
			
		}
		//System.out.println("r is now:"+r);
		
		
	}

	public static void randomMove(String w3)
	{	
		r=new Random();
		int number=2;
		StringBuffer sb1=new StringBuffer(word1);
		for (int j=2;j<=sb1.length();j++)
		{
			number=r.nextInt(sb1.length());//because maximum possible value is 2				
		}
		System.out.println("The number entered is:"+number);
		for(int n=1;n<=number;n++)
		{
			String newString= word1.substring(word1.length()-n)+word1.substring(0,word1.length()-n);
			word2=newString;
			System.out.println("New string is: "+newString);
			System.out.println("Word 2 is:"+word2);
		}
		//word=sb1.toString();		

	}
	
	public static void removeVowels(String word3)
	{			
		word4=word1.replaceAll("[aeiouAEIOU]","");
		System.out.println("Word 4 is:" +word4);
	}
}
			



My aim is to ask user to provide a word and then to ask for 3 different manipulation methods. Manipulations methods are working fine but I have some problem when comparing guessed letters to the main string and revealing only manipulated letters.

For exaple,

I am entering "letter" as word it manipulates it to 'ltr' if I perform all three methods.

While guessing if I provide "letter" as a whole or letter by letter it should only display ltr in place of _ _ _ (If string is fully manipulated...most importantly if remove vowels is performed) and should generate popup that the guess is true.

I think there is some problem with two loops for manipulated strigs but unable to figure out!
Was This Post Helpful? 0
  • +
  • -

#7 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Help with Hangman!

Posted 20 February 2010 - 04:04 PM

Double posting is a no-no.
Was This Post Helpful? 0
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Help with Hangman!

Posted 20 February 2010 - 04:16 PM

take a look at that for loop:
for (int i=0;i<original.length();i++) //original word length time iteration 
{ 
if ((guess.charAt(0)==manipulated.charAt(i))) 

you are looping the "indexes" of the "original" String.(original.length())
but you assign those indexes in the "manipulated" String.
are you sure those two String Objects have the same length?

why don't you simply loop the manipulated string?
for (int i=0;i<manipulated.length();i++) //<<<<<<manipulated length time iteration 
{ 
if ((guess.charAt(0)==manipulated.charAt(i))) 

Was This Post Helpful? 1
  • +
  • -

#9 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Help with Hangman!

Posted 20 February 2010 - 04:19 PM

@ Japanir

wouldnt it be ..

for (int i=0;i<manipulated.length()-1;i++) 


Was This Post Helpful? 0
  • +
  • -

#10 tuxx223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 10

Re: Help with Hangman!

Posted 20 February 2010 - 04:20 PM

View Postxor-logic, on 20 February 2010 - 03:04 PM, said:

Double posting is a no-no.

That was just a bit of code and I guess readers couldn't understand what I mean to say that's why I posted it here in detail. Sorry! I'm new to this forum! I wanna remove previous but how to delete it?
Was This Post Helpful? 0
  • +
  • -

#11 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Help with Hangman!

Posted 20 February 2010 - 04:33 PM

@m-e-g-a-z
StringObject.length() - 1 is indeed the last index you can refer to in a String Object.
However, the for loop used in that code, loops as long as "i" is smaller than manipulated.length().
which means, "i" will never be equal to manipulated.length(), which means the "biggest" value that i can have in that loop is actually manipulated.length() - 1, which is fine.

in that for loop, However:
for (int i=0;i <= manipulated.length();i++) 

in the last iteration of the loop, "i" will have the value of manipulated.length(), and trying to access a char in that locations will give us the StringIndexOutOfBoundsEcxeption.
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Help with Hangman!

Posted 20 February 2010 - 04:36 PM

Topics merged, please do not create duplicate topics
Was This Post Helpful? 0
  • +
  • -

#13 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Help with Hangman!

Posted 20 February 2010 - 04:39 PM

oops my bad Japanir ..i get it :)
Was This Post Helpful? 0
  • +
  • -

#14 tuxx223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 10

Re: Help with Hangman!

Posted 21 February 2010 - 01:19 PM

View Postjapanir, on 20 February 2010 - 03:16 PM, said:

take a look at that for loop:
for (int i=0;i<original.length();i++) //original word length time iteration 
{ 
if ((guess.charAt(0)==manipulated.charAt(i))) 

you are looping the "indexes" of the "original" String.(original.length())
but you assign those indexes in the "manipulated" String.
are you sure those two String Objects have the same length?

why don't you simply loop the manipulated string?
for (int i=0;i<manipulated.length();i++) //<<<<<<manipulated length time iteration 
{ 
if ((guess.charAt(0)==manipulated.charAt(i))) 

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1