+ Reply to Thread
Results 1 to 6 of 6

Thread: Guess The Word.

  1. #1
    Paradox is offline Newbie
    Join Date
    Jul 2008
    Posts
    17
    Rep Power
    16

    Guess The Word.

    I was just messing around today because i was board so i decided to make a program in Java where you can either make your own word for somebody to guess or you can have it select a random one for you.

    Code:
    import java.util.*;
    
    public class wordGuess {
    
        public static String word = null;
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("This is a guessing words game.");
            System.out.println("Type the word to use or type random to use a random word.");
            word = input.nextLine();
            if(word.equals("random")) {
                switch (rand(11)) {
                case 0:
                    word = "hello";
                    break;
                case 1:
                    word = "bye";
                    break;
                case 2:
                    word = "what";
                    break;
                case 3:
                    word = "cool";
                    break;
                case 4:
                    word = "hi";
                case 5:
                    word = "sweet";
                    break;
                case 6:
                    word = "call of duty";
                    break;
                case 7:
                    word = "funny";
                    break;
                case 8:
                    word = "woof";
                    break;
                case 9:
                    word = "computer";
                    break;
                case 10:
                    word = "laptop";
                    break;
                    
                }
            }
            wordOne();
        }
        private static int rand(int bound) {
            return (int) (Math.random() * bound);
        }
        public static void wordOne() {
            int tries = 0;
            boolean gotIt = false;
            String inputWord = null;
            Scanner input = new Scanner(System.in);
            System.out.println("You will have 50 tries to get this word");
            if(tries >= 49) {
                System.out.println("You have exided the maximum number of tries.");
                gameOver();
            }
            while (tries <= 50) {
                inputWord = null;
                System.out.println("Enter a letter you think the word may contain");
                System.out.println("If you want to try and guess just type the word.");
                System.out.println("");
                inputWord = input.nextLine();
                tries++;
                
                    if(inputWord.equals(word)) {
                        System.out.println("Youve got it nice one!!");
                        sleep(1000);
                        return;
                    }
                if(word.contains(inputWord)) {
                    System.out.println("You have guessed right, it does contain a "+inputWord);
                }
                else
                {
                    System.out.println("Sorry you have guessed wrong.");
                }
            }
            
        }
    
        public static void gameOver() {
            System.out.println("Thanks for playing my little game!");
        } 
        public static void sleep(int time) {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        
        }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42

    Re: Guess The Word.

    I only checked your code briefly, mainly because I'm not a Java programmer.
    One obvious thing I would make different is how you choose the random word. Instead of using a switch-statement I would keep all of the random words in an array. Then I would take the size of the array and generate a random number between 0 and the size of the array. This number will then be used for accessing the array.
    Instead of the many, many lines you have, you could solve it in two lines of code.

  4. #3
    Paradox is offline Newbie
    Join Date
    Jul 2008
    Posts
    17
    Rep Power
    16

    Re: Guess The Word.

    Thanks for the help.

    I was bored so I believe there would be a lot of mistakes and didn't rely spend time on it.

    ~Paradox

  5. #4
    shoaibbi's Avatar
    shoaibbi is offline Newbie
    Join Date
    Jan 2009
    Posts
    15
    Rep Power
    0

    Re: Guess The Word.

    gud one
    VIvAcIoUs pAkIsTaNi

  6. #5
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: Guess The Word.

    Why did you override the sleep method? Also your wordOne method seems a bit long. Could it be broken down a bit more?

    Another thing, why do you make a random method, that seems a bit pointless to replace 1 line of code. Your program only runs once, so do you need the rand method?

  7. #6
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Guess The Word.

    Quote Originally Posted by chili5 View Post
    Why did you override the sleep method? Also your wordOne method seems a bit long. Could it be broken down a bit more?

    Another thing, why do you make a random method, that seems a bit pointless to replace 1 line of code. Your program only runs once, so do you need the rand method?
    He could do that or make a simple AI

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Guess I should introduce myself
    By blackriderrom in forum Introductions
    Replies: 6
    Last Post: 01-28-2010, 10:27 PM
  2. Replies: 1
    Last Post: 12-14-2009, 10:42 AM
  3. Looking for help Guess game
    By flawkstawker in forum C# Programming
    Replies: 3
    Last Post: 02-18-2009, 08:53 PM
  4. Guess a Number
    By 3n! in forum C and C++
    Replies: 2
    Last Post: 12-02-2007, 01:44 PM
  5. Raycasting I guess
    By Maurice_Z in forum Programming Theory
    Replies: 9
    Last Post: 11-27-2007, 12:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts