2 Replies - 215 Views - Last Post: 29 April 2013 - 07:47 PM Rate Topic: -----

#1 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Hangman output

Posted 29 April 2013 - 03:32 PM

I need help finalizing the program. I need to get my output to actually work as opposed to just printing out the hangman drawing. I am close but have no idea what to do for the final method. Please help!!
import java.io.*; //for file
import java.util.*; //for scanner

public class Lab65 {
  public static void main (String[]args)
    throws FileNotFoundException {
    File f = new File ("hangman.txt");   //read from file
    Scanner input = new Scanner (f);
    int index;
   
    int numberguesses = 0;
    Random rand = new Random();
    index = rand.nextInt(10);
    while(numberguesses <= 6 ) { 
   System.out.println("Let's play hangman!"); 
   
   String[] words = new String[10];
   displayguesses();
      
    }
   
   
  }
  
  //hangman display
    public static void displayguesses() { 
     int numberguesses = 0;
     System.out.println("Begin guessing letters!");
     Scanner console = new Scanner(System.in);
     String guess = console.next();
       //user guesses
    if(numberguesses == 0) {
      System.out.println("-------");
      System.out.println("|      |");
      System.out.println("       |");
      System.out.println("       |");
      System.out.println("       |");
      System.out.println("       |");
      System.out.println("       |");
      System.out.println("       |");
    }
    else if(numberguesses == 1) {
        System.out.println("-------");
        System.out.println("|      |");
        System.out.println("O      |");
        System.out.println("       |");
        System.out.println("       |");
        System.out.println("       |");
        System.out.println("       |");
        System.out.println("       |");
    }
    else if(numberguesses == 2) {
        System.out.println("-------");
        System.out.println("|      |");
        System.out.println("O      |");
        System.out.println("|      |");
        System.out.println("       |");
        System.out.println("       |");
        System.out.println("       |");
        System.out.println("       |");
    }
    else if(numberguesses == 3) {
        System.out.println("-------");
        System.out.println("|      |");
        System.out.println("O      |");
        System.out.println("|      |");
        System.out.print("//       |");
        System.out.println("         |");
        System.out.println("       |");
        System.out.println("       |");
    }
    else if(numberguesses == 4) {
        System.out.println("-------");
        System.out.println("|      |");
        System.out.println("O      |");
        System.out.println("|      |");
        System.out.print("//       |");
        System.out.print("\\       |");
        System.out.println("       |");
        System.out.println("       |");
    }
    else if(numberguesses == 5) {
        System.out.println("-------");
        System.out.println("|      |");
        System.out.println("O      |");
        System.out.println("|      |");
        System.out.print("//       |");
        System.out.print("\\       |");
        System.out.println("//     |");
        System.out.println("       |");
    }
    else if(numberguesses == 6) {
        System.out.println("-------");
        System.out.println("|      |");
        System.out.println("O      |");
        System.out.println("|      |");
        System.out.print("//       |");
        System.out.print("\\       |");
        System.out.println("//     |");
        System.out.println("\\     |");
    }
    }
    // keep track of wrong guesses
    public static String countwrongguesses() {
      Scanner console = new Scanner(System.in);
      System.out.println("Enter a letter!");
      String guess = console.next();
      char y = guess.charAt(0);
      return guess;
    }
    
      }   
     








Is This A Good Question/Topic? 0
  • +

Replies To: Hangman output

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Hangman output

Posted 29 April 2013 - 06:26 PM

Where in this code do you check whether or not a guess was CORRECT? There are probably better ways of doing it, but here's my suggested structure for such an event.

index = rand.nextInt(9); //10 is out of bounds

List<char> alreadyGuessed = new ArrayList<char>();

if(userGuess.length() > 1)
{
    //you'll need to use the same case for everything, I would put it everwhere, but I'm lazy.
    if(words[index].toLowerCase().equals(userGuess.toLowerCase());
        //user wins!
    else
        incorrectGuess();
}
else
{
    //guess is invalid if it's been previously guessed before
    if(!alreadyGuessed.contains(userGuess.atChar(0)))
    {
        //trigger a method to advance the hangman stage
        if(!words[index].contains(userGuess));
            incorrectGuess();
        //add userGuess to the list of previous guesses
        alreadyGuessed.add(userGuess.atChar(0));
    }
    
    //check to see whether or not the user has correctly guessed the word
    for(int i = 0; i < words[index].length(); i++)
    {
        //check letter by letter to see whether or not the user has guessed them all
        if(alreadyGuessed.contains(words[index].charAt(i));
            continue;
        else
            break;
        //if the loop makes it to the end without breaking, the user wins.
        if(i == (words[index].length() - 1))
            //user wins!
    }
}


Has been edited a few times, so it might be segmented, or flat-out incorrect -- but it defines a structure.

This post has been edited by Gungnir: 29 April 2013 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: Hangman output

Posted 29 April 2013 - 07:47 PM

View PostGungnir, on 29 April 2013 - 06:26 PM, said:

Where in this code do you check whether or not a guess was CORRECT? There are probably better ways of doing it, but here's my suggested structure for such an event.

index = rand.nextInt(9); //10 is out of bounds

List<char> alreadyGuessed = new ArrayList<char>();

if(userGuess.length() > 1)
{
    //you'll need to use the same case for everything, I would put it everwhere, but I'm lazy.
    if(words[index].toLowerCase().equals(userGuess.toLowerCase());
        //user wins!
    else
        incorrectGuess();
}
else
{
    //guess is invalid if it's been previously guessed before
    if(!alreadyGuessed.contains(userGuess.atChar(0)))
    {
        //trigger a method to advance the hangman stage
        if(!words[index].contains(userGuess));
            incorrectGuess();
        //add userGuess to the list of previous guesses
        alreadyGuessed.add(userGuess.atChar(0));
    }
    
    //check to see whether or not the user has correctly guessed the word
    for(int i = 0; i < words[index].length(); i++)
    {
        //check letter by letter to see whether or not the user has guessed them all
        if(alreadyGuessed.contains(words[index].charAt(i));
            continue;
        else
            break;
        //if the loop makes it to the end without breaking, the user wins.
        if(i == (words[index].length() - 1))
            //user wins!
    }
}


Has been edited a few times, so it might be segmented, or flat-out incorrect -- but it defines a structure.


Ahh this lab is driving me crazy. We have only learned basic java so some of that stuff in there is confusing me haha. I am so lost
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1