0

Can someone please help me make my could into 2-3 multiple java files? I really don't know how to break it up and I am afraid if I do, I'll mess it up and have to start all over again. So if any could please help me it would be greatly appreciated.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;


public class HangMan
{
    
    String dictionary[] = { "boy", "girl", "mother", "father", "sister",
        "love", "sky", "wind", "water", "study", "ball", "cat", "dog",
        "puppy", "kitten", "apple", "pear", "lemon", "mango", "peach",
        "apricot", "chips", "steak", "fries", "cheese", "patato", "wedge",
        "heel", "hand", "foot", "arm", "leg", "nose", "face", "mouth",
        "tongue", "fingers", "toes", "line", "space", "phone", "cord",
        "core", "grass", "trees", "birds", "animals", "lazy", "funny",
        "king", "queen", "heart", "heat", "cold", "sun", "moon", "movie",
        "theater", "hairy", "big", "small", "large", "huge", "pig",
        "donkey", "cow", "chicken", "pizza", "bread", "stones", "sticks",
        "leaves", "letters", "alphabet", "soup", "hungry", "tired",
        "sleepy", "noisy", "caring", "friends", "month", "day", "light",
        "toothbrush", "savings", "bank", "account", "teller", "paper",
        "pencil", "tea", "coffee", "spirit", "ghost", "can", "melon",
        "necklace", "screen", "balloon", "string", "calendar", "work",
        "toys", "kids", "school", "class", "campus", "freedom", "liberty",
        "happiness", "university", "message", "marker", "crayon", "eraser",
        "music", "lyrics", "songs", "ballads", "shapes", "triangle",
        "circle", "rectangle", "square", "oval", "show", "video", "player",
        "team", "sport", "basketball", "football", "soccer", "softball",
        "baseball", "tennis", "hockey", "lacrosse", "volleyball", "circuit",
        "blade", "scratch", "hit", "home", "house", "safe", "safety",
        "number", "count", "bear", "goose", "llama", "panda", "lion",
        "tiger", "cheetah", "computer", "crackers", "rice", "fan", "shoes",
        "book", "story", "princess", "prince", "jester", "court", "jury",
        "judge", "bench", "scandal", "name", "newspaper", "press", "shove",
        "tear", "cry", "magic", "tricks", "cereal", "breakfast", "lunch",
        "dinner", "main", "course", "fork", "spoon", "knife", "lamp",
        "desk", "bottle", "highlighter", "cap", "medicine", "six", "seven",
    "flower", "rose", "petal" };
    
    String wordInMemory = null;
    String hang[] = {"(","0","0",")","-","<","=","<"};
    
    public HangMan()
    {
        super();
        
    }
    
    public void startHangman()
    {
        boolean done = false;
        
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        do
        {
            System.out.print("\t\tEnter your selection:\n\t\t1. Play Hangman \n\t\t2.Exit program\n\t\tChoice = ");
            
            int choice = 0;
            try
            {
                choice = Integer.parseInt(input.readLine());
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            switch (choice)
            {
                case 0:
                {
                    System.out.println("Press 1 or 2");
                    break;
                }
                case 1:
                {
                    play();
                    break;
                }
                case 2:
                {
                    done = true;
                    break;
                }
                default:
                {
                    System.out.println("Press 1 or 2");
                    break;
                }
            }
        }
        while (!done);
    }
    
    private void play()
    {
        int rand_no = (int) (Math.random() * 200);
        if (rand_no > 200)
        {
            rand_no = 200;
        }
        wordInMemory = dictionary[rand_no];
        int numberOfLetters = wordInMemory.length();
        
        //System.out.println("WORD : "+wordInMemory+"\n");
        int falseAttempts = 0;
        int pendingLetters = numberOfLetters;
        boolean won = false;
        String letterGuess = null;
        Vector successAttempt = new Vector();
        Vector wordInVector = new Vector();
        Vector wordTyped = new Vector();
        for(int ini=0;ini<numberOfLetters;ini++)
        {
            successAttempt.addElement(" _ ");
            wordInVector.addElement(""+wordInMemory.charAt(ini));
        }
        System.out.println();
        System.out.print("\t\tWORD : ");
        for(int i=0;i<numberOfLetters;i++)
        {
            System.out.print(successAttempt.elementAt(i));
        }
        
        System.out.println("\n\n");
        
        BufferedReader inBuff = new BufferedReader(new InputStreamReader(System.in));
        do
        {
            System.out.print("\t\tEnter your guess = ");
            try
            {
                letterGuess = inBuff.readLine().toLowerCase();
                if(wordTyped.contains(letterGuess))
                {
                    System.out.println("\t\tYou already guessed this letter early ");
                    continue;
                }
                else
                {
                    wordTyped.addElement(letterGuess);
                    if(wordInMemory.contains(letterGuess))
                    {
                        for(int j=0;j<numberOfLetters;j++)
                        {
                            String letter = (String)wordInVector.elementAt(j);
                            if(letterGuess.equals(letter))
                            {
                                successAttempt.setElementAt(letterGuess,j);
                                pendingLetters--;
                            }
                        }
                        if(pendingLetters == 0)
                        {
                            won = true;
                        }
                        
                    }
                    else
                    {
                        falseAttempts++;
                    }
                }
                System.out.println();
                System.out.print("\t\tWORD : ");
                for(int i=0;i<numberOfLetters;i++)
                {
                    System.out.print(successAttempt.elementAt(i));
                }
                System.out.println();
                System.out.print("\n\t\t");
                for(int f=0;f<falseAttempts;f++)
                {
                    System.out.print(hang[f]);
                }
                System.out.println();
                System.out.print("\n\t\tNo: of Attempts Left : "+(8-falseAttempts));
                System.out.println();
                if(falseAttempts == hang.length)
                {
                    System.out.println("\n\t\t Word is "+wordInMemory);
                }
                System.out.println();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        while((falseAttempts < 8) && !won);
        
        if(won)
        {
            System.out.println("\t\tYou guessed it right !");
        }
        else
        {
            System.out.println("\t\tYou lose !");
        }
    }
    
    public static void main(String[] args)
    {
        
        HangMan man = new HangMan();
        man.startHangman();
    }
}
2
Contributors
1
Reply
4
Views
9 Years
Discussion Span
Last Post by jwenting
0

If you back up your work before experimenting you can always revert to a version that worked and start from there...

At the very least get rid of that hardcoded dictionary in your main class, and separate the business logic from the user interface.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.