Click here to Skip to main content
15,886,634 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class Hangmaniona {
Scanner sc = new Scanner(System.in);
int life = 9; // Takes 0 into account hence not 10
  void play(){
    //Secret words
    String[] bagOfWords = new String[]{"the", "walrus", "and", "carpenter", "were", "walking", "close", "at", "hand"};
    
    //Initialize the random number generator
    System.out.println("Type an arbitrary number");
    int seed = sc.nextInt();
    Random random = new Random(seed);
    
    //Select a secret word
    String secret = bagOfWords[random.nextInt(bagOfWords.length)];
    char[] filler = new char[secret.length()];
    ArrayList<Character> givenCharacter = new ArrayList<Character>();    
    for(int i = 0; i < secret.length(); i++) { 
      filler[i] = '_'; 
    }
    System.out.println(filler);


    while(life > 0) {
    char x = sc.next().charAt(0); // call x userinput 

    if(givenCharacter.contains(x)) {
      life--;
      System.out.println(filler);
      System.out.println("Already entered");
      System.out.print("life remaining"+life);
      continue;
    }
     givenCharacter.add(x);

      if(secret.contains(x+"")) {
        for(int y=0; y<secret.length(); y++) {
          if(secret.charAt(y)==x) {
            filler[y]=x;
          }
        }
      }
      else {
        life--;
      }
      System.out.println(filler);
      System.out.println("life remaining:" + life);
      
      if(secret.equals(String.valueOf(filler))) {
        System.out.println("Well done, you won!");
        break;
      }

    }
    if(life==0) {
      System.out.println("Unlucky, you lost!");
      System.out.println("The secret word was:" + secret);
    }
  }
  public static void main(String[] args) { 
    new Hangman().play();
  }
}


What I have tried:

I have a bug with this program, which is that when the user inputs the same letter that has been guessed before, the program does not decline the "life" value by 1 but instead it keeps it constant. Can someone please help me solve this issue ? I believe the error occurs because the below if statement cannot be read.

Java
if(givenCharacter.contains(x)) {
      life--;
      System.out.println(filler);
      System.out.println("Already entered");
      System.out.print("life remaining"+life);
      continue;
    }
Posted
Updated 14-Sep-20 19:58pm
v2
Comments
CPallini 15-Sep-20 2:05am    
I can confirm your code works as intended. This is the output of a run on my Linux box:
Type an arbitrary number                                                                                                                        
3                                                                                                                                               
_______                                                                                                                                         
a                                                                                                                                               
_a_____                                                                                                                                         
life remaining:9                                                                                                                                
a                                                                                                                                               
_a_____                                                                                                                                         
Already entered                                                                                                                                 
life remaining8a                                                                                                                                
_a_____                                                                                                                                         
Already entered                                                                                                                                 
life remaining7a                                                                                                                                
_a_____                                                                                                                                         
Already entered                                                                                                                                 
life remaining6e                                                                                                                                
_a_____                                                                                                                                         
life remaining:5                                                                                                                                
e                                                                                                                                               
_a_____                                                                                                                                         
Already entered                                                                                                                                 
life remaining4i                                                                                                                                
_a__i__
life remaining:4
i
_a__i__
Already entered
life remaining3i
_a__i__
Already entered
life remaining2

1 solution

Quote:
when the user inputs the same letter that has been guessed before, the program does not decline the "life" value by 1 but instead it keeps it constant

I don't see an issue here and believe the code would work as intended.

By any chance, are you writing this code in an environment where all inputs have to be made before running program like some fiddle? If so, your program provides 10 chance to guess and if input is not of that length, it would throw an error in
char x = sc.next().charAt(0);


I say this as I have seen such case and error.
Exception in thread "main" java.util.NoSuchElementException

Just to validate, start with less number of chance and see.

BTW, just DEBUG and things would be crystal clear on what's happening and possibly leading to why.
 
Share this answer
 
Comments
CPallini 15-Sep-20 2:05am    
5.
Sandeep Mewara 15-Sep-20 2:52am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900