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 3 of 3

Thread: Please help with this code.....

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Please help with this code.....

    I dont understand whats wrong with my code. I have created a word guess game where on the menu the user has to select an option so a letter is generated in the word a bit like hangman but it does not appear to select a random letter everytime...Im a beginner to Java, any help would really be appreciated. It also does not appear to display the message : You lost. no attempts left...


    import java.util.Random;
    public class Game {
     
        private Word w;
        private int attempts;
     
        Game(){
            startGame();
        }
     
        private void startGame(){
            Database db = new Database();
            w = new Word (db.getRandomWord());
            attempts = 10;
        }
     
        private int showDisplayOptions(){
            int input = 3;
            System.out.println("Your Options:");
            System.out.println("Guess another letter      1");
            System.out.println("Guess the word            2");
            System.out.println("Give up                   3");
            System.out.println();
     
            boolean validInput =false;
     
            while(!validInput){
     
                System.out.print("Enter your choice:  ");
                InputReader in = new InputReader();
                input = in.getInt();
     
                if (!(input==1 || input==2 || input==3)) {
                    validInput=false;
                    System.out.println("Invalid, Please enter options 1-3");
                }
                else
                    validInput=true;
     
            }    
     
            return input;
        }
     
        public void run(){
     
            boolean exit = false;
     
            while (!exit){
                if (attempts<1) {
                    System.out.println("You lost. No attempts left");
                    startGame();
                }
     
                System.out.println("You know:                ");
                System.out.println(w.toString());
                System.out.println("You have "+attempts+" attempts left");
                System.out.println();
     
                switch (showDisplayOptions()) {
                case 1:chooseLetter();
                    break;
                case 2:chooseWord();
                    break;
                case 3: exit = true;
                    break;
                default:exit = true;
                    break;
                }
            }
     
        }
     
        private void chooseLetter() {
            boolean chosen=false;
            Random genericGenerator = new Random();
            char letter = (char) (genericGenerator.nextInt(26)+65);
            chosen=w.hasLetter(letter);
     
            attempts--;
        }
     
     
        private void chooseWord() {
            InputReader in = new InputReader();
            boolean chosen;
            System.out.println("\nEnter word:  ");
            String letter = in.getString();
            chosen = w.isRight(letter);
     
        }
     
    }


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Location
    kottayam, kerala, India
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help with this code.....

    Game.java:4: cannot find symbol
    symbol : class Word
    location: class Game
    private Word w;
    ^
    Game.java:12: cannot find symbol
    symbol : class Database
    location: class Game
    Database db = new Database();
    ^
    Game.java:12: cannot find symbol
    symbol : class Database
    location: class Game
    Database db = new Database();
    ^
    Game.java:13: cannot find symbol
    symbol : class Word
    location: class Game
    w = new Word (db.getRandomWord());
    ^
    Game.java:30: cannot find symbol
    symbol : class InputReader
    location: class Game
    InputReader in = new InputReader();
    ^
    Game.java:30: cannot find symbol
    symbol : class InputReader
    location: class Game
    InputReader in = new InputReader();
    ^
    Game.java:85: cannot find symbol
    symbol : class InputReader
    location: class Game
    InputReader in = new InputReader();
    ^
    Game.java:85: cannot find symbol
    symbol : class InputReader
    location: class Game
    InputReader in = new InputReader();
    ^
    8 errors

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help with this code.....

    Go here: Random Letter problem please help!
    I don't know which thread you're using :S
    Last edited by Cuju; March 28th, 2010 at 11:34 AM.