Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Serious problem with Hangman.java

$
0
0
[import java.util.Scanner;
import java.util.Random;

public class HangmanMy {
  public static void main (String[] args){
    System.out.println("Welcome to the game Hangman :)/>/>/>/>");
    Scanner input = new Scanner(System.in);
    boolean done = false;
    String guess;
    String guesses = "";
    char letter;
    int wrongGuess = 0;
    StringBuffer dashes;
    System.out.println("Choose a category -> places / games / animals / subjects ");
    String words = WordsToChoose(input.nextLine());
    dashes = makeDashes(words);
    while (! done)
    {
      System.out.println("Here is your word: " + dashes);
      System.out.println("Guesses so far: " + guesses);
      System.out.print("enter a guess (letter or word): ");
      guess = input.next().replaceAll("\\W", "");
      // process the guess
      
      if (guess.length() > 1)
      {
        if (guess.equalsIgnoreCase(words))
          System.out.println("you win!");
        else 
          System.out.println("Thats the wrong guess");
        done=true;
      }
      else // process single letter guess
      {
        letter = guess.charAt(0);
        guesses += letter;
        if (words.indexOf(letter) < 0)  // not there
        {
          wrongGuess++;
          if(wrongGuess < 6)
            System.out.print("Bad guess: ");
          else if(wrongGuess >= 6)System.out.println("Too many Guesses,The real word was: " + words);
        }
        else // letter is in the words
        {
          // put it in dashes where it belongs
          matchLetter(words, dashes, letter); 
        }
        if(words.equalsIgnoreCase(dashes.toString())){ 
          System.out.print("\nyou win!");
          done = false;
        }
      }
    }
  } 
  public static void matchLetter(String words, StringBuffer dashes, char letter)
  {
    for (int index = 0; index < words.length(); index++)
      if (words.charAt(index) == letter)
      dashes.setCharAt(index, letter);
          words = words.replaceAll("\\W","");
    System.out.print("Good guess: ");
  }
  public static StringBuffer makeDashes(String s)
  {
    StringBuffer dashes = new StringBuffer(s.length());
    for (int count=0; count < s.length(); count++)
      dashes.append('-');
    return dashes;
  }
  
  public static String WordsToChoose(String category)
  {
    Random generator = new Random();
    String name = "";
    if (category.equalsIgnoreCase("places"))
    {
      String places[] = {"Barc elona", "Lond on", "Par is", "Los Angeles", "Miami Beach", "San Diego", "Sydney", "Puerto Rico"};
      name = places[generator.nextInt(8)];
    }
    else if (category.equalsIgnoreCase("games"))
    {
      String games[] = {"Call of duty", "Halo", "FIFA", "Gta", "AssassinsCreed", "Need for speed", "AngryBirds", "FruitNinja"};
      name = games[generator.nextInt(8)];
    }
    else if (category.equalsIgnoreCase("animals"))
    {
      String animals[] = {"eagle", "tiger", "lion", "Jaguar", "rhinoceros", "sharks", "dolphin", "whale"};
      name = animals[generator.nextInt(8)];
    }
    else if (category.equalsIgnoreCase("subjects"))
    {
      String subjects[] = {"physics", "Computer Science", "chemistry", "gym", "english", "religion", "computer tech", "accounting"};
      name = subjects[generator.nextInt(8)];
    }
    return name.toLowerCase();
  }  
} 


My program will not end and also the when there is too many guesses the right word shows on top of everything not on the bottom and also it dosent end after it keeps on going
Could you tell me what i did wrong??

Also the program creates a dash for the space fix that also

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>