HangMan, problem displaying letters guessed.

I cannot display letters guessed and displaying underscores.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 5312 Views - Last Post: 18 January 2011 - 10:11 PM Rate Topic: -----

#1 droops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-September 10

HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 05:31 PM

Hello, may you guys please help me? I have a problem with my hangman program. I cannot make it work as i want it to. I want it to display the letters the user guessed and dashes where the user failed to guess each turn.

import java.io.*; // imports java package
  
  public class WheelofFortune // the class mastermind
{
  int p = 0; // the variable that the user enter his data in 
  int n = 0; // a counter variable
  String s = ""; // user input
   String[] word = {"soccer"}; // the word the user has to guess
   String[] guess = new String[1]; //decleration of an array
  InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);
   public void input() 
   {
     try
    {   
            System.out.println(" Hello, this is hangman, please enter a letter you THINK is in the word. ");
            //get the letter from user
            for(int i = 0; i < 1; ++i) 
            {
                s = br.readLine();    
                 guess[i] = s; // user input is stored in array
                i++;
                 
    }
     }
   catch (IOException ex)
     {
       ex.printStackTrace();
     }
   }
  
    public void check()
 {
      for(int counter = 0; counter < 1 ; counter ++)  
  {
   if((guess[counter]) .equals (word[counter]))
   { 
     System.out.println(s); //trying to print out the parts the user guessed...
   }
    counter++;
      }
    
    }
    
  }


And my runner :
public class WheelofFortunerunner
{
  public static void main (String[] args) 
  {
   WheelofFortune black = new WheelofFortune();
  int r = 0;
  
  do{ 
  black.input(); // get input method
  black.check(); 
  
  }while(r < 7); // , he has 7 tries.... if he passes those tries he will loose

  
  }
    
}


Is This A Good Question/Topic? 0
  • +

Replies To: HangMan, problem displaying letters guessed.

#2 droops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-September 10

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 05:45 PM

pbl can you help me again please? You helped me with my mastermind... :)?

Or anyone please ... :)

This post has been edited by droops: 13 January 2011 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 06:00 PM

Well what exactly are the problems? do you get any error messages? if yes post them.
I glanced at your code, and it lacks the logic.
an algorithm to play that game would be:
1. pick a word to guess.
2.display the word like _ _ _ _ _ ('_' as the number of letters in the word)
3. ask the user for an input
4. loop while the player hasn't guess, or loop a declared amount of guesses (player has only 3 guesses)
  4.1. check if the letter is in the word, and present it to the player (_ e_ _ _ e)
5. reveal the word

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 06:01 PM

An array of [1] is kind of useless

Scanner scan = new Scanner(System.in);
System.out.print("Enter a letter: ");
char letter = scan.nextLine().charAt(0);


This is the way to read a letter from the console
Was This Post Helpful? 2
  • +
  • -

#5 TokoYami200  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 44
  • Joined: 18-October 10

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 06:17 PM

You're testing guess and word, but guess doesn't contain any information. Furthermore, you're testing one character vs. a whole word.

For example, the letter s isn't the word soccer. You'll need to use String's charAt function to test the input with the letters then keep track of the right inputs.
Was This Post Helpful? 1
  • +
  • -

#6 droops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-September 10

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 06:39 PM

@ tokyo. Im guessing you would assume this is for a school project. My teacher wont allow me to use anything that he didnt teach us. So i cant use the API.

@pbl, Cant use CharAt because our teacher didnt teach us that. Same reason as tokyo.

I was thinking that maybe i should change
String[] word = {"soccer"}; 

into

String[]word = {"s","o","c","c","e","r"};


?? anythought and the use if statements to check these individually?

Thanks for your help so far guys! :)

This post has been edited by droops: 13 January 2011 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 06:50 PM

Why do you declare String arrays when not needed?
Instead of:
String[] word = {"soccer"};

write
String word = "soccer";

same with the second declaration.
If you don't want to use charAt (weird, but I'll comment about it later)
use a char array instead:
char[] word = {'s', 'o', 'c', 'c', 'e','r'};

and refer to each letter by the array's indices (instead of charAt)
char c = word[i]; //i is a valid index of course


Quote

anythought and the use if statements to check these individually?
Avoid checking individually at all costs! use loops whenever possible. to check if a letter is in the array word I declared:
char someChar = some_char_value;
for(int i = 0; i < word.length; i++){
  if(someChar == word[i]){
    //found!
   }
}


And last but not least. Learning a programming language is done mainly by yourself. your teacher won't, can't and shouldn't teach you about all the classes\methods and pacgages available. It is your duty, as a programmer to learn it by yourself.

This post has been edited by japanir: 13 January 2011 - 06:51 PM

Was This Post Helpful? 3
  • +
  • -

#8 droops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-September 10

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 07:28 PM

Im sorry for my lack of programming knowledge and understanding, but I dont know what you are declaring when you type

View Postjapanir, on 13 January 2011 - 05:50 PM, said:

char someChar = some_char_value;
}

Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 07:44 PM

char is a primitive data type.
a char holds a character, that in java is represented like: 'a', 'd', '3' etc.
when I declared:
char someChar = some_char_value;

It was just a default declaration. I just wanted to use a char in my example, so I declared it and gave it a "some char value" which can be any char literal.
in your program the some_char_value could be the letter inputted by the user as his guess.
Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2587
  • View blog
  • Posts: 15,619
  • Joined: 24-September 08

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 08:10 PM

Why does the thread say hangman, but the classes say wheel of fortune? :x

If your assignment allows, why not use a Scanner? It's much simpler, and you generally don't have to use a try block. My guess is that since you didn't know how data type declarations work this isn't your code and you're just trying to understand it? Teacher gave you some code to play with? Either way, using Scanner would make your life a lot easier, also instead of doing the useless array, do the String like japanir said, or the char array. A little trick so you don't have to type all the letters out (for a big word or phrase) is
String sphrase = "soccer";
char[] cphrase = sphrase.toCharArray();

;) http://download.orac...l#toCharArray()
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: HangMan, problem displaying letters guessed.

Posted 13 January 2011 - 08:16 PM

View Postdroops, on 13 January 2011 - 07:39 PM, said:

@pbl, Cant use CharAt because our teacher didnt teach us that. Same reason as tokyo.

This is ridiculous.
There are thousands and thousabds of classes and method in the API. If you can't use the ones not already showed by your teacher you will spend a few years with your teacher.

An old Chinese proberb says: "Give a fish to a man you will feed him once, teach him how to fish, you will feed him all its life"

A good teacher should teach you how to search in the API rather than explaining to you method after method. As a teacher how would rather explain what is the class String and then I would give as homework: "Write a program that uses 15 of the methods defined in the class String".

You must have misunderstood your teacher instruction or change teacher.

I can understand that a teacher gives an homework like: create a program using dynamic size arrays but do not use ArrayList and/or Vector but not that crap you gave us.
Was This Post Helpful? 3
  • +
  • -

#12 Guest_droops*


Reputation:

Re: HangMan, problem displaying letters guessed.

Posted 14 January 2011 - 03:04 PM

View PostNeoTifa, on 13 January 2011 - 07:10 PM, said:

If your assignment allows, why not use a Scanner? It's much simpler, and you generally don't have to use a try block. My guess is that since you didn't know how data type declarations work this isn't your code and you're just trying to understand it? Teacher ;) http://download.orac...l#toCharArray()


I cannot use a scanner, because acorrding to my teacher, it is for noobs and their is no point of teaching it because it virtually becomes useless when we enter the higher grades of programming.
Was This Post Helpful? 0

#13 droops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-September 10

Re: HangMan, problem displaying letters guessed.

Posted 14 January 2011 - 05:24 PM

Guys i have an error, how can i get someone's input with only using a char? Because japanir gave me and example like this :
char someChar = some_char_value;
It seems that br.readline is only possible by using a string data type :(, anyway how to change this?
My code so far :

import java.io.*; // imports java package
  
  public class WheelofFortune // the class mastermind
{
  int p = 0; // the variable that the user enter his data in 
  int n = 0; // a counter variable
  int c = 0;
  String s = "";
   // user input
  
 
  
  char[] word = {'s', 'o', 'c', 'c', 'e','r'}; // the word the user has to guess
  InputStreamReader isr = new InputStreamReader(System.in); 
  BufferedReader br = new BufferedReader(isr);
   public void input() 
   {
     try
    {   
            System.out.println(" Hello, this is hangman, please enter a letter you THINK is in the word. ");
            //get the letter from user
            for(int i = 0; i < 1; ++i) 
            {
               c = buffer.read;
              char character = (char) c; 
               
    }
     }
   catch (IOException ex)
     {
       ex.printStackTrace();
     }
   }
  
    public void check()
 
    {
char someChar = some_char_value;
 for(int i = 0; i < word.length; i++){  

   if((c) == (word[i]))
   {  

     System.out.println(c);  

    }  

} 
  }
  }
the runner is still the same
Was This Post Helpful? 0
  • +
  • -

#14 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: HangMan, problem displaying letters guessed.

Posted 14 January 2011 - 05:41 PM

Isn't that ironic? :)
The common way of getting a char as input is by getting a String input, and use charAt(index i) on that String to retrieve the first char.
like:
char c = br.readLine().charAt(0);

assuming br is BufferedReader Object
this code get the String Object from readLine, and return the first character in that String. (so it actually like getting a char input).

This post has been edited by japanir: 14 January 2011 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

#15 droops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-September 10

Re: HangMan, problem displaying letters guessed.

Posted 14 January 2011 - 05:53 PM

View Postjapanir, on 14 January 2011 - 04:41 PM, said:

Isn't that ironic? :)
The common way of getting a char as input is by getting a String input, and use charAt(index i) on that String to retrieve the first char.
like:
char c = br.readLine().charAt(0);

assuming br is BufferedReader Object
this code get the String Object from readLine, and return the first character in that String. (so it actually like getting a char input).


Thank you, I still have problems tho :(, the program wont print what i want it to print (which is the value of c)It just keeps looping like it totally ignored the condition i set.
import java.io.*; // imports java package
  
  public class WheelofFortune // the class mastermind
{
  int p = 0; // the variable that the user enter his data in 
  int n = 0; // a counter variable
  int c = 0;
  String s = "";
   // user input
  
 
  
  char[] word = {'s', 'o', 'c', 'c', 'e','r'}; // the word the user has to guess
  InputStreamReader isr = new InputStreamReader(System.in); 
  BufferedReader br = new BufferedReader(isr);
   public void input() 
   {
     try
    {   
            System.out.println(" Hello, this is hangman, please enter a letter you THINK is in the word. ");
            //get the letter from user
            for(int i = 0; i < 1; ++i) 
            {
              char c = br.readLine().charAt(0);
               
    }
     }
   catch (IOException ex)
     {
       ex.printStackTrace();
     }
   }
  
    public void check()
 
    {

 for(int i = 0; i < word.length; i++)
 {  

   if((c) == (word[i]))
   {  

     System.out.println(c);  

    }  
} 
  }
  }

once again thank you for your help :)

This post has been edited by droops: 14 January 2011 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3