Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Logic of Hangman

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Logic of Hangman

    I'm working on a project for school and was curious
    whether there is a better way to perform some the
    logic in my hangman program. It plays like hangman
    but you can't lose depending on the number of moves.

    import java.util.*;
     
    class Hangman {
        static Scanner scan = new Scanner(System.in);
        static Random rand = new Random();
     
        static boolean isIn(char c, String str) {
            int occurrence = str.indexOf(c);
            if (occurrence >= 0)
                return true;
            else
                return false;
        }
     
        static boolean printCurrStatus(String strToGuess, String userInputs) {
            String current = "";
            String positionTemp = "";
            String currentTemp = "";
            for (int i = 0; i < strToGuess.length(); i++) {
                current += "_" + " ";
                positionTemp += strToGuess.charAt(i) + " ";
            }
     
            for (int i = 0; i < userInputs.length(); i++) {
                char input = userInputs.charAt(i);
                if (isIn(input, strToGuess)) {
                    int index = 0;
                    int position = 0;
                    while (index > -1) {
                        index = positionTemp.indexOf(input, position);
                        if (index < 0)
                            break;
                        else {
                            currentTemp = current.substring(0, index) + input;
                            currentTemp += current.substring(index + 1,
                                    positionTemp.length() - 1);
                            current = currentTemp;
                            position += 1;
                        }
                    }
                }
            }
            System.out.println(current);
     
            if (current.indexOf('_') < 0)
                return true;
            else
                return false;
     
        }
     
        static String getNextWordToGuess() {
     
            String[] nextWord = { "elephant", "tiger", "monkey", "baboon",
                    "barbeque", "giraffe", "simple", "zebra", "porcupine",
                    "aardvark" };
            int newRandom = rand.nextInt(nextWord.length);
     
            return nextWord[newRandom];
     
        }
     
        static void playGame() {
            String userInput = "";
            String wordToGuess = getNextWordToGuess();
            while (!printCurrStatus(wordToGuess, userInput))
            {
                System.out.println("Enter next letter:");
                userInput += scan.next();
            }
     
     
        }
     
        public static void main(String[] args) {
            String choice = "y";
            while (choice.equals("y")) {
                playGame();
                System.out.println("Do you want to play again?  (y or n)");
                choice = scan.next();
            }
            System.out.println("Thanks for playing!");
        }
    }

    Everything works fine but I'm curious on whether there
    is a way to simplify the printCurrStatus method. I do not
    want to change this method into multiple methods.

    I'm new to the site and appreciate your time. I hope to
    be a contributor soon.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Logic of Hangman

    Hard to tell. The comments in the code don't say anything about the program's logic.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Hangman Help
    By devindadude in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 25th, 2013, 09:24 PM
  2. Hangman program
    By psgtyler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 12:58 AM
  3. Java Hangman!
    By JavaManNoob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2012, 11:17 PM
  4. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  5. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM