9 Replies - 1283 Views - Last Post: 11 February 2012 - 02:04 PM Rate Topic: -----

#1 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-February 12

hidden Word

Posted 09 February 2012 - 09:11 PM

i'm trying to create kind of a Hidden word game.
maybe someone can take a look and help me figure out!
so i need to
represent my secret word if form of questions marks The disguised word is <?????????> like this for instance, then i need to enter a letter and determine if this character is in the secret word, if its, the disguised word is update to show the position of that letter.
its actually works but when i compile it.. it does represent my secret word in disguised form..
thats what i've got right now
package question3;
import java.util.Scanner;
public class HidWord {
	Scanner kb=new Scanner(System.in);
	 String secretPhrase, disguisedPhrase,guess;
	int correctGuess,inCorrectGuess,totalGuess;
	
	public HidWord(String newPhrase){
		secretPhrase=newPhrase;
	}
	//method to represent disguised form
	public  void disguisedPhrase(){
		
		disguisedPhrase="";
		for(int i=0;i<secretPhrase.length();i++){
			disguisedPhrase+="?";}
	}
			
	
		public String getDisguisedPhrase(){return disguisedPhrase;	}
		public String toString(){return disguisedPhrase;}
		public void guess(){
			System.out.println("Guess a letter: ");
			guess=kb.next().toLowerCase();
			
			//checkGuess(guess);
			totalGuess++;
		}
                  //check if the character that user input is in the secret word
		public void checkGuess(char guess){
			char current;
			boolean guessCorrectly=false;
			for(int i=0; i<secretPhrase.length(); i++){
				current=secretPhrase.charAt(i);
				if (current==guess)
				{
					guessCorrectly=true;
				if(i<secretPhrase.length())
				{disguisedPhrase=disguisedPhrase.substring(0,i)+current+disguisedPhrase.substring(i+1);}
				else
				{
					disguisedPhrase=disguisedPhrase.substring(0,i)+current;	}
				}
				}
			if(guessCorrectly)
			{
					correctGuess++;
				}
			else
			{inCorrectGuess++;}
			}
				
				public boolean isPhraseComplete(){
					return disguisedPhrase.equalsIgnoreCase(secretPhrase);
				}
			public void Output(){
				System.out.println("The disguised word is :"+ getDisguisedPhrase());
				//System.out.println("Guess a letter:");
				
			}
		
		public static void main(String[] args)
		{
			HidWord test=new HidWord("happiness");
		
			//System.out.println(test.getDisguisedPhrase());
			//	test.HidWord("");
			test.Output();
			//test.getDisguisedPhrase();
			test.guess();
			test1.disguisedPhrase();
			
		}
		
		}
		
	




the game keep going until the word is complete.
in the end it should be something like this:

The disguised word is <?????????>
Guess a letter:a
Guesses made: 1 with 0 wrong
The disguised word is <?a???????>
Guess a letter:e
Guesses made: 2 with 0 wrong

Is This A Good Question/Topic? 0
  • +

Replies To: hidden Word

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: hidden Word

Posted 09 February 2012 - 09:21 PM

Quote

its actually works but when i compile it.. it does represent my secret word in disguised form..
Ok so... what is the problem exactly? I'm assuming you meant it does NOT show your word in disguised form? You have shown us the output you are expecting, but you really haven't told us what the output you are getting is.
Was This Post Helpful? 0
  • +
  • -

#3 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-February 12

Re: hidden Word

Posted 09 February 2012 - 09:37 PM

View PostSheph, on 09 February 2012 - 09:21 PM, said:

Quote

its actually works but when i compile it.. it does represent my secret word in disguised form..
Ok so... what is the problem exactly? I'm assuming you meant it does NOT show your word in disguised form? You have shown us the output you are expecting, but you really haven't told us what the output you are getting is.


oh, sorry.
Yeah, you right, it does not sow my word in disguised form.
to be more specific, that what i have as my output right now

The disguised word is :null
Guess a letter:

i when i input me character, it does mot show anything more.
whats wrong or why it does not check my character and display my result?
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: hidden Word

Posted 09 February 2012 - 10:10 PM

The null means it was not initialized. You should initialize all of your variables to something inside of your constructor for the class. You only tell it to call the method guess() once in your main method, so that's all it does... You will probably have to implement a loop.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10444
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: hidden Word

Posted 10 February 2012 - 06:49 AM

Quote

The null means it was not initialized. You should initialize all of your variables to something inside of your constructor for the class.

Kind-of. By default, your instance variables are auto-initialized to their default values, regardless of whether or not you explicitly initialize them in your constructor. For objects, the default value is null. This means you have an Object variable that does not point to an actual Object in memory. This is why you should explicitly initialize your instance fields (especially the Objects) to appropriate values in the constructor.
Was This Post Helpful? 1
  • +
  • -

#6 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-February 12

Re: hidden Word

Posted 10 February 2012 - 08:37 AM

View Postmacosxnerd101, on 10 February 2012 - 06:49 AM, said:

Quote

The null means it was not initialized. You should initialize all of your variables to something inside of your constructor for the class.

Kind-of. By default, your instance variables are auto-initialized to their default values, regardless of whether or not you explicitly initialize them in your constructor. For objects, the default value is null. This means you have an Object variable that does not point to an actual Object in memory. This is why you should explicitly initialize your instance fields (especially the Objects) to appropriate values in the constructor.

Yeah,Thanks I understand that I'm not initialize iz,Im trying to fix it but cannot do that,can you help me with that??
Was This Post Helpful? 0
  • +
  • -

#7 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: hidden Word

Posted 10 February 2012 - 08:40 AM

Just look at the order of your steps

You try to output disguisedPhrase, THEN you assign something to disguisedPhrase
Was This Post Helpful? 1
  • +
  • -

#8 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-February 12

Re: hidden Word

Posted 10 February 2012 - 03:31 PM

now i got disguised form of the secret word but then nothing happens.
what if create one more method to control everything. i mean - i will repeatedly collect letters from the user and make guesses(but i need to call the method ) until the word is complete. and make all input and output in this method.
Does anybody can help me to create this and finish this game.
Was This Post Helpful? 0
  • +
  • -

#9 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: hidden Word

Posted 10 February 2012 - 05:44 PM

For one thing you will need a game loop, your program flow should look something like this

Set the hidden word
Make the disguised word the length of hidden word as ????

while (wordnotguessed)
{
   print disguised word
   print get a letter
   getuserinput as char
   if (hidden word contains userinput)
   {
      disguisedword is replaced with a new string with the letter changed
   }
   if (hidden word is fully guessed)
   {
      wordnotgussed = false;
   }
}

// player wins




You should consider what methods you will need to do this:

A main method which should create a new hiddernword game
A method that creates the disguisedWord
A method that gets user input
A method that checks if the word is fully guessed or not
A method that replaces the string with the guessed character shown

Some of those can be moved to just your loop like reading input, but it does help neaten things up, Your code is a mess at the moment, you should really indent it properly so people, especially us can read it. (I have to go into eclipse to fix your code just to read it)
Was This Post Helpful? 1
  • +
  • -

#10 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-February 12

Re: hidden Word

Posted 11 February 2012 - 02:04 PM

View PostMylo, on 10 February 2012 - 05:44 PM, said:

For one thing you will need a game loop, your program flow should look something like this

Set the hidden word
Make the disguised word the length of hidden word as ????

while (wordnotguessed)
{
   print disguised word
   print get a letter
   getuserinput as char
   if (hidden word contains userinput)
   {
      disguisedword is replaced with a new string with the letter changed
   }
   if (hidden word is fully guessed)
   {
      wordnotgussed = false;
   }
}

// player wins




You should consider what methods you will need to do this:

A main method which should create a new hiddernword game
A method that creates the disguisedWord
A method that gets user input
A method that checks if the word is fully guessed or not
A method that replaces the string with the guessed character shown

Some of those can be moved to just your loop like reading input, but it does help neaten things up, Your code is a mess at the moment, you should really indent it properly so people, especially us can read it. (I have to go into eclipse to fix your code just to read it)

But i do have this method that check whether or not letter is in the word....
i try to create a loop and its not the biggest problem..
so right now i'm stuck with the checkGuess method. i mean i can't call it and check if the letter in the word.. i do can check for the full word and display - that you win. but i cannot check for one letter and update the disguised word (???p???)
package question3;
import java.util.Scanner;
public class HidWord {
		Scanner kb=new Scanner(System.in);
		String secretPhrase;
		String disguisedPhrase;
		String guess;
		int correctGuess,inCorrectGuess,totalGuess;
	
		//constructor, takes in a single argument the word to be guessed
	public HidWord( String newPhrase){
		secretPhrase=newPhrase;
		totalGuess++;
		}
	
	//method - version of word in the form of question form 
		public  void disguisedPhrase(){
		disguisedPhrase="";
		for(int i=0;i<secretPhrase.length();i++){
			disguisedPhrase+="?";}
				}
	
				public String getDisguisedPhrase(){return disguisedPhrase;	}
		//public String toString(){return secretPhrase;}
		
		//determines if the character is in the	word
		public void checkGuess(char letter){
				guess=kb.next().toLowerCase();
			letter=guess.charAt(0);
			char current;
			boolean guessCorrectly=false;
			for(int i=0; i<secretPhrase.length(); i++){
				current=secretPhrase.charAt(i);
				if (current==letter)
				{	guessCorrectly=true;
				if(i<secretPhrase.length())//check for rightness 
				{	
					disguisedPhrase=disguisedPhrase.substring(0,i)+current+disguisedPhrase.substring(i+1);}
				else
				{
					disguisedPhrase=disguisedPhrase.substring(0,i)+current;	}
				}
				}
			if(guessCorrectly)
			{ correctGuess++; }
			else
			{inCorrectGuess++;}
						}
		
				//method that check if the word is fully guessed or not
			public boolean isPhraseComplete(){
					return disguisedPhrase.equalsIgnoreCase(secretPhrase);
				}
	//in this meth i want to collect letters and make guesses					
			public void play(){
				while(!isPhraseComplete()==true)
				{		
					
				System.out.println("The disguised word is: " +"<"+disguisedPhrase+">");
				System.out.println("Guess a letter: ");
				guess=kb.next().toLowerCase();
				
				
				//just for my self to check if user input all word 
				if (guess.equals(secretPhrase)) {
					System.out.println("The answer was: " + guess);
					System.out.println("You WIN!");
					break;
				} else {
					System.out.println();
					System.out.println(" " + disguisedPhrase);
					}
				}
			}
							
						
		public static void main(String[] args)
		{
			System.out.println("lets play HiddenWord");
			HidWord test=new HidWord("happiness");
				
			test.disguisedPhrase();
			//checkGuess(letter);
			test.play();
		}		
		}
		


in play method i wanna collect letters from user and make guesses (call method checkGuess) but i cannot do that..(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1