9 Replies - 5421 Views - Last Post: 12 April 2008 - 10:08 AM Rate Topic: -----

#1 nogal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 08

secret phrase

Post icon  Posted 28 March 2008 - 09:42 PM

Hi all,

I am trying to do a program where I have a phrase, not completely hidden, and the player should be able to guess it. When the player type a letter or word (lower case or upper case) the program should be able to put it in the right place and be displayed or tell the player that the letter or word is not inside the phrase. At the end when the player guesses the phrase, the program should display a congratulatory message. the code that I posted is what I have so far but it does not run.

I have been working in this program for two weeks but I think I am stuck (probably because I don't have enough knowledge yet...... my first java course)

If anybody could help me I will really appreciate that.

thanks


errors:

SecretPhrase.java:34: illegal start of expression
}
^
SecretPhrase.java:55: while expected
}
^
SecretPhrase.java:56: illegal start of expression
for(int i = 0; i < palabra.length(); i++)
^
3 errors




/*SecretPhrase.java*/

import javax.swing.JOptionPane;
import java.util.Arrays;

public class SecretPhrase
{

	public static void main(String [ ] args)
	{

	String palabra = "Life is a field of unlimited possibilities",
								 wordtoguess="**********f******f*u***m*t************t***", phrasetoguess;
	int x=0, y=0;
	String aciertos="";
	String errados="";
	String letra="";
	char[ ] dashes = wordtoguess.toCharArray();
	char[ ] compare = palabra.toCharArray();

		do
		{

			phrasetoguess=JOptionPane.showInputDialog("Pick a word or letter?");

			if (phrasetoguess.equalsIgnoreCase("palabra"))

			{
			phrasetoguess=JOptionPane.showInputDialog("Guess the phrase");

			if (phrasetoguess.equalsIgnoreCase(palabra))

			System.out.println("Life is a field of unlimited possibilities\nYou got it!\nCongratulations!!!");
			
			}

			else 
			{

			x=0;
			y++;

			String player=JOptionPane.showInputDialog("Enter a letter or phrase");
			}
		
				if (player.charAt(0)==phrase.charAt(x))


		while(palabra != wordtoguess);
		{
			if(palabra.indexOf(letra) >= 0)
			aciertos+=letra;
			else
			errados+=letra;
		}
		}
		for(int i = 0; i < palabra.length(); i++)
		{
			String l = palabra.substring(i,i+1);
			if(aciertos.indexOf(letra) >= 0)
		{
			System.out.println(" " + letra);
		}
		else
			System.out.println(" _ ");
		}
		
		
System.exit(0);
	}
}





Is This A Good Question/Topic? 0
  • +

Replies To: secret phrase

#2 gl3thr0  Icon User is offline

  • D.I.C Regular

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

Re: secret phrase

Posted 29 March 2008 - 11:40 AM

i know this wont help the code u have now... but if u want an easy way to do this look into ArrayLists

they r like arrays bt r as big or small as need amd jv a bunch of helpful methods
if u take this advice look into the method indexof() it will basicly make ur life a 100000 times simpiler



~~~~~~~~~~~~~~~~~~~~~~~~~~~~

the problemS in your code begin on line 44

if (player.charAt(0)==phrase.charAt(x))
firstly there is no '{' :P
next the varible player does nt exist any more(out of scope
it was declared in the if statments and once those finish it dies
the varible phrase.. YOUVE nevvvver made it :P your varible is phrasetoguess

soo to fix it your code should look like
	char[ ] dashes = wordtoguess.toCharArray();  //16
	char[ ] compare = palabra.toCharArray();	 //17
	String player;			//18

//code continues...
	 player=JOptionPane.showInputDialog("Enter a letter or phrase");//41
//^ notice the slight change here
}
		if (player.charAt(0)==phrasetoguess.charAt(x)){ //44



soo after you fix all this(and add the' }') there still one problem on line 47

while(palabra != wordtoguess);

now if this is because your using a do-while statement then im not sure y u have the '{'
but if its just supposed to be a while then your also missing the ending of the do-while :P

confusing?? i think so:P


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
actualy nvm with all the arraylist shit:P it will just screw u over :P sorry for that
so as repayment i wrote a quick example that you can use to write your code
and yess its kinda bulky.. but its gt a few neat things in it enjoy

import java.util.ArrayList;
import java.util.Scanner;

public class WordGuesser {
	
	String phrase; //PHRASE	 to decipher
	Scanner scan = new Scanner(System.in);
	
	WordGuesser(String phrase){
		this.phrase = phrase.toLowerCase(); //gets rid of the annoying capital letters 
	}
	
	public static void main(String args[]){
		WordGuesser wg = new WordGuesser("helloooo i am DYLAN");  //the phrase to decipher
		wg.go(); //starts the program :P
	}
	
	void go(){
		String guess;
		String word="";
		String[] words = new String[phrase.length()]; //makes array to hold every character in the phrase
		ArrayList<String> letersGuessed = new ArrayList();
		
		for(int i=0;i<phrase.length();i++){//creates an array that holds '-' instead of letters.  
			if(phrase.charAt(i)== ' '){words[i] = " ";} // shows the user the spaces in the phrase 
			else{words[i] = "-";} // character to be uncovered as useres guess the correct letters
		}
		
		int Gcount =0; //guess count
		
		while(!word.equals(phrase)){ //keeps the guessing going until user deciphers the phrase
			word = "";//will be filled with the array of '-'s
			int Lcount = 0;
			System.out.print("Guess a letter> ");
			guess = scan.next();
			
			//notice the charAt(0) this is in case the user enters more then 1 letter.
			for(int i=0;i<phrase.length();i++){
				if((guess.charAt(0)+"").equals(phrase.charAt(i)+"")&&(letersGuessed.indexOf(guess.charAt(0)+"")==-1)){
					//this long statement checks if the letters match AND if it has already been guessed. 
					//you could seperate them and have a message print out to tell the user he already guessed tht letter
					words[i] = ( guess.charAt(0))+ ""; 	//uncovers a letter in the array
					Lcount++; //letter count
				}
			}
									letersGuessed.add(guess.charAt(0)+""); //adds the letter to the array 
			System.out.println("You found " + Lcount +" letters"); //in case there ws more than 1 letter found
			
			for(int i=0;i<words.length;i++){
				word=word+words[i]; //fills word with the array so that the while condition can be met
			}
			System.out.println(word);
			Gcount ++;
		}
		System.out.println("YOU WIN :o\nit took you " +Gcount + " guess'");
	}
}


This post has been edited by gl3thr0: 30 March 2008 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 nogal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 08

Re: secret phrase

Posted 30 March 2008 - 01:34 PM

Wow!!!! Thanks gl3thr0. After looking at what you did. I now understand why I could not finish it.

thanks.

I have been reading it and it is really interesting the way you made the code.


Question:
When I compile it, I get this message:
Note: C:\Java Exercises\WordGuesser.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

From what I read on internet, the program is using something not really safe. Could you please help me to fix this, if necessary, or it is ok the way it is.

When the player put a letter and it is one of the letters from the phrase, it puts it the but how do the program can say to the player that the letter he/she just input is not in the phrase.

Also, how do I make for the phrase to be not completely hide...... like: -e------ i -m -y---"


Thanks you

This post has been edited by nogal: 30 March 2008 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 gl3thr0  Icon User is offline

  • D.I.C Regular

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

Re: secret phrase

Posted 31 March 2008 - 09:37 AM

the warnings dont reallly matter but if u want to get rid of them do this
Change the line
ArrayList<String> letersGuessed = new ArrayList();
to
ArrayList<String> letersGuessed = new ArrayList<String>();
should fix it


for the other part just add if statments after the for loop
}//ends for
if(letersGuessed.indexOf(guess.charAt(0)+"")!=-1){
				System.out.println("Sorry that letter has already been guessed");
				//Gcount --; if you want repeated guesses counted un comment this
			}
			else{
				System.out.println("Letter not in phrase");
				//say its not in the phrase
			}



make sure this it OUTSIDE the for loop otherwise ull get the message a shit loada times :P

um.. mine dsnt completly hide the phrase..my out put looks like this..
~~~~~~~~~~~~~~~~~~~
Guess a letter> b
Letter not in phrase
You found 0 letters
-------- - -- -----
Guess a letter> l
Letter not in phrase
You found 3 letters
--ll---- - -- --l--
~~~~~~~~~~~~~~~~~~~

??? maybe you mean like when the program starts have some letters already filled in??
Was This Post Helpful? 0
  • +
  • -

#5 nogal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 08

Re: secret phrase

Posted 31 March 2008 - 09:25 PM

Yes gl3thr0 that is what I mean

??? maybe you mean like when the program starts have some letters already filled in??

Now I am getting it. Slow but I am learning. Thanks
Was This Post Helpful? 0
  • +
  • -

#6 gl3thr0  Icon User is offline

  • D.I.C Regular

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

Re: secret phrase

Posted 01 April 2008 - 09:47 AM

i wont write the code for this bt ill give u the basics


i would first generate a random number between 0 and phrase.length. and then have it uncover that index number in the words array(words[ranNum] = phrase[ranNum];) i think that would prolly be the easiest way. IF you wanted you could put tht in a for loop and then you could do it multipule times.
this is a verrry basic way of doing it. if you get it working move it up a step and instead of uncovering the letter at tht index uncover alll of that letter.
then if you get tht u can move it up a little more and add that letter that all ready been shown into the ArrayList

if your having trouble just post
Was This Post Helpful? 0
  • +
  • -

#7 nogal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 08

Re: secret phrase

Posted 03 April 2008 - 07:58 PM

Thanks gl3thr0, ..........work is keeping me away from finishing this.

I will work on that and if anything i will be back.

This post has been edited by nogal: 03 April 2008 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 JIN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-May 07

Re: secret phrase

Posted 12 April 2008 - 07:53 AM

so how would u change the code so the user puts word or phrase in to be guessed
Was This Post Helpful? 0
  • +
  • -

#9 JIN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-May 07

Re: secret phrase

Posted 12 April 2008 - 08:10 AM

View PostJIN, on 12 Apr, 2008 - 07:53 AM, said:

so how would u change the code so the user puts word or phrase in to be guessed by the player

Was This Post Helpful? 0
  • +
  • -

#10 gl3thr0  Icon User is offline

  • D.I.C Regular

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

Re: secret phrase

Posted 12 April 2008 - 10:08 AM

its already set up for that the constructor of the class asks for a phrase
WordGuesser(String phrase){
		this.phrase = phrase.toLowerCase(); //gets rid of the annoying capital letters 
	}


so from a class outside of this u would simple do this

import java.util.Scanner;
public class PhraseTry{
   
	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		String phrase ="";
		System.out.print("Enter Phrase ");
		phrase = scan.nextLine();
		
		WordGuesser w = new WordGuesser(phrase);
		w.go();
		 
	}
}




with this new class in you could take the main class out of WordGuesser

This post has been edited by gl3thr0: 12 April 2008 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1