Thread: Hangman project; Feedback appreciated

Results 1 to 2 of 2
  1. #1 Hangman project; Feedback appreciated 
    Donator

    Nintendo's Avatar
    Join Date
    Oct 2008
    Posts
    1,681
    Thanks given
    195
    Thanks received
    184
    Rep Power
    231
    Main;

    Code:
    package com.mycompany.hangman;
     
    import java.util.Scanner;
     
    /**
     * Starts the game; once finished game asks the player to play again. Also exits
     * the application here.
     *
     * @author Nintendo
     */
    public class GameRunner {
     
        public static void main(String[] args) {
            HangMan game = new HangMan();
     
            System.out.println("Welcome to Hangman.");
            do {
                game.resetGame();
                game.start();
            } while (replay());
            System.out.println("Thank-you, for playing.");
        }
     
        /**
         * @return true if user input starts with a "y" else return false.
         */
        private static boolean replay() {
            Scanner input = new Scanner(System.in);
            System.out.println("Would you like to play again?");
            String playAgain = input.nextLine().toUpperCase();
            return playAgain.charAt(0) == 'Y';
        }
    }
    Game;

    Code:
    package com.mycompany.hangman;
     
    import static java.lang.System.out;
    import java.util.Scanner;
     
    /**
     * The main class of the application we run the game through here.
     *
     * @author Nintendo
     */
    public class HangMan {
     
        private String secretWord;
        private final String[] setHangMansBody = new String[5];
        private final int MAX_TRIES = 5;
        private int tries;
     
        /**
         * Starts the application here by deciding the games secret word.<p>
         * Also checks if the player did guess the secret word or not.
         */
        public void start() {
            secretWord = SecretGameWords.getSecretWord();
            out.println("You have " + MAX_TRIES + " tries, goodluck.\n");
            if (guessedSecretWord()) {
                out.println("You won!\n");
                out.println("The word was: " + secretWord);
            } else {
                out.println("You lost!\n");
                out.println("The word was: " + secretWord);
            }
        }
     
        /**
         * @return true if player manages to guess the secret word within 5
         * tries.<p>
         * false if player fails to guess the secret word.
         */
        private boolean guessedSecretWord() {
            Scanner input = new Scanner(System.in);
            char guessedLetter = 0;
            char[] hiddenWord = secretWord.toCharArray();
            StringBuilder revealWord = new StringBuilder(secretWord.replaceAll("\\S", "-"));
            out.println(revealWord);
            while (tries < MAX_TRIES) {
                out.print("Guess the word or just a letter: ");
                String guessWord = input.nextLine().toUpperCase();
                try {
                    guessedLetter = guessWord.charAt(0);
                } catch (StringIndexOutOfBoundsException e) {
                }
                if (secretWord.indexOf(guessedLetter) != -1) {
                    for (int charPos = 0; charPos < hiddenWord.length; charPos++) {
                        if (hiddenWord[charPos] == guessedLetter) {
                            revealWord.setCharAt(charPos, guessedLetter);
                        }
                    }
                    if (revealWord.toString().equals(secretWord) || guessWord.equals(secretWord)) {
                        return true;
                    }
                    out.println(revealWord);
                    out.println("\nYou got it right!");
                } else {
                    tries++;
                    out.println("You guessed wrong letter, you have " + (MAX_TRIES - tries) + " tries left.");
                    drawHangMan();
                }
            }
            return false;
        }
     
        /**
         * Resets the game variables to default.
         */
        public void resetGame() {
            tries = 0;
            secretWord = null;
            for (int bodyParts = 0; bodyParts < setHangMansBody.length; bodyParts++) {
                setHangMansBody[bodyParts] = " |                 ";
            }
        }
     
        /**
         * Reveals hang man's body depending on failed tries.
         */
        private void drawHangMan() {
            switch (tries) {
                case 1:
                    setHangMansBody[0] = " | /      |           ";
                    break;
                case 2:
                    setHangMansBody[1] = " |      (*_*)      ";
                    break;
                case 3:
                    setHangMansBody[2] = " |       \\|/       ";
                    break;
                case 4:
                    setHangMansBody[3] = " |        |        ";
                    break;
                case 5:
                    setHangMansBody[4] = " |       / \\      ";
                    break;
                default:
                    resetGame();
     
            }
            out.println(" __________");
            for (String revealBody : setHangMansBody) {
                out.println(revealBody);
            }
            out.println(" |                 ");
            out.println(" |________________ ");
        }
    }

    Words

    Code:
    package com.mycompany.hangman;
     
    import java.util.*;
     
    /**
     * Utility class that handles hangman's secret words.
     *
     * @author Roner Reked
     */
    public class SecretGameWords {
     
        private SecretGameWords() {
        }
     
        private static final List<String> secretWords = new ArrayList<>();
     
        /**
         * Creating a new array with the game words so we can add them to our
         * secretWord list and then shuffle it.
         */
        static {
            String[] gameWords = {"JAVA", "COMPUTER", "UMBRELLA", "STAR",
                "SLEEPY", "TRASH", "GOLD", "IMPOSTER"};
            secretWords.addAll(Arrays.asList(gameWords));
            Collections.shuffle(secretWords);
        }
     
        /**
         * @return a new word from the secretWords list every time this method is
         * called.
         */
        public static String getSecretWord() {
            return secretWords.get(nextWord++);
        }
     
        private static int nextWord = 0;
    }
    I mostly need feedback on my variable names, and javadoc basically the structure of the application. If anyone could look through it and give me some feedback on bad naming It would be really appreciated!
    Reply With Quote  
     

  2. #2  
    Donator

    Join Date
    Mar 2013
    Posts
    1,777
    Thanks given
    324
    Thanks received
    386
    Discord
    View profile
    Rep Power
    318
    Quote Originally Posted by Nintendo View Post
    Code:
    package com.mycompany.hangman;
     
    import java.util.Scanner;
     
    /**
     * Starts the game; once finished game asks the player to play again. Also exits
     * the application here.
     *
     * @author Nintendo
     */
    public class GameRunner {
     
        public static void main(String[] args) {
            HangMan game = new HangMan();
     
            System.out.println("Welcome to Hangman.");
            do {
                game.resetGame();
                game.start();
            } while (replay());
            System.out.println("Thank-you, for playing.");
        }
     
        /**
         * @return true if user input starts with a "y" else return false.
         */
        private static boolean replay() {
            Scanner input = new Scanner(System.in);
            System.out.println("Would you like to play again?");
            String playAgain = input.nextLine().toUpperCase();
            return playAgain.charAt(0) == 'Y';
        }
    }
    Some quick feedback, haven't really looked too much into the other classes but:

    1. Just create one method to execute the application.
    2. Use the while loop, makes the code more clearer. Brings me to my final point,
    2. Either make the replay() method public or remove it completely, really no point.

    Heres an updated class:

    Code:
    import java.util.Scanner;
    
    public class Main {
     
        public static void main(String[] args) {
            HangMan game = new HangMan();
            System.out.println("Welcome to Hangman.");
    		Scanner input = new Scanner(System.in);
    		while (true) {
                System.out.println("Would you like to play again?");
                String playAgain = input.nextLine().toUpperCase();
    			game.resetGame();
                game.start();
    			System.out.println("Thank-you, for playing.");
    		}
        }
    	
    }
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Hangman project; replace the secret word with underscores
    By Nintendo in forum Application Development
    Replies: 3
    Last Post: 11-14-2014, 06:45 AM
  2. Replies: 11
    Last Post: 08-22-2013, 01:24 PM
  3. Java Practice - Feedback Appreciated
    By Guthan in forum Application Development
    Replies: 6
    Last Post: 06-25-2013, 09:53 AM
  4. Replies: 19
    Last Post: 11-22-2009, 02:50 AM
  5. [PROJECT]FeedBack Via FTP
    By Ken in forum Application Development
    Replies: 6
    Last Post: 07-29-2009, 12:40 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •