Results 1 to 6 of 6
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Hangman, show letters

    I want the letters to show but I don't know how to select and element from the array, and then check if that element has a letter in the actual word and then print it. Any thoughts on how?

    Java Code:
    import java.util.Random;
    import javax.swing.JOptionPane;
    
    public class Hangman extends BasicGame
    {
    
        Random randy = new Random();
        private int guesses;
        private final int maxGuesses = 6;
        private String word = "";
        private int strikes;
        private String[] words = {"Apple", "Beret", "Arose", "Along", "Beamy", "becks", "decks", "barks",
                                  "stark", "start", "stabs", "baggy", "asked", "asset", "asses", "audit",
                                  "bowls", "boxes", "bongs", "balls", "boats", "boxer", "brick", "bound",
                                  "brass", "caked", "braid", "caged", "essay", "fault", "dents", "dutch",
                                  "ethos", "dunks", "paids", "faxes", "mummy", "mixer", "mills", "might",
                                  "moral", "teeth" };
        
        public String getWords (){
            String randomWord = words[randy.nextInt( words.length)];
            return randomWord;
        }
        
        public void mainScreen (){
            while (guesses != maxGuesses){
                JOptionPane.showInputDialog(null, "The word has five letters, make your guess!");
                
            }
            
        }
        
        public void bodyParts(){
            if(guesses == 1){
                JOptionPane.showMessageDialog(null, "You now have a head! Guesses left: 5");
            }
            if(guesses == 2){
                JOptionPane.showMessageDialog(null, "You now have a body! Guesses left: 4");
            }
            if(guesses == 3){
                JOptionPane.showMessageDialog(null, "You now have a right arm! Guesses left: 3");
            }
            if(guesses == 4){
                JOptionPane.showMessageDialog(null, "You now have a left arm! Guesses left: 2");
            }
            if(guesses == 5){
                JOptionPane.showMessageDialog(null, "You now have a right leg! Guesses left: 1");
            }
            if(guesses == maxGuesses){
                JOptionPane.showMessageDialog(null, "You lose!");
            }
        }
    }

  2. #2
    Herjan is offline Member
    Join Date
    Jan 2013
    Location
    FailLand
    Posts
    6
    Rep Power
    0

    Default Re: Hangman, show letters

    This guy will definitely help you out with all your string problems:


  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Hangman, show letters

    I have a problem. I want to get the String letterInput, but I can't since I use it for a certain method that I need. Also it's a local variable. How do I put that String into an instance variable. Have any ideas? Maybe a getLetterInput method but it'd still have that problem right?

    Java Code:
    import java.util.Random;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    public class Hangman extends BasicGame
    {
    
        Random randy = new Random();
        private int guesses;
        private final int maxGuesses = 6;
        private String word = "";
        private int strikes;
        private String letter;
        private String letterSelected;
        private String[] words = {"Apple", "Beret", "Arose", "Along", "Beamy", "becks", "decks", "barks",
                                  "stark", "start", "stabs", "baggy", "asked", "asset", "asses", "audit",
                                  "bowls", "boxes", "bongs", "balls", "boats", "boxer", "brick", "bound",
                                  "brass", "caked", "braid", "caged", "essay", "fault", "dents", "dutch",
                                  "ethos", "dunks", "paids", "faxes", "mummy", "mixer", "mills", "might",
                                  "moral", "teeth" };
        
        
        public String getWords (){
            String randomWord = words[randy.nextInt( words.length)];
            return randomWord;
        }
        
        public void start(){
            strikes = 0;
            word = getWords();
            String letterInput = JOptionPane.showInputDialog(null, "The word has five letters, make your guess!");
        }
       
        public void continueGame(){
            if(words.indexOf(getWords()).equalsIgnoreCase(letterInput)){
            strikes++;
        }
            bodyParts();
            JOptionPane.showInputDialog(null, "");
        }
        
        public void mainScreen (){
            start();
            while (guesses != maxGuesses){
                continueGame();
                
            }
            gameOver();
        }
        
        public void bodyParts(){
            if(guesses == 1){
                System.out.println("You now have a head! Guesses left: 5");
            }
            if(guesses == 2){
                System.out.println("You now have a body! Guesses left: 4");
            }
            if(guesses == 3){
                System.out.println("You now have a right arm! Guesses left: 3");
            }
            if(guesses == 4){
                System.out.println("You now have a left arm! Guesses left: 2");
            }
            if(guesses == 5){
                System.out.println("You now have a right leg! Guesses left: 1");
            }
            if(guesses == maxGuesses){
                System.out.println("You lose!");
            }
        }
        
        public void gameOver(){
            System.out.println("Game Over!");
        }
    }
    ignore the intermingle of println and panes lol

  4. #4
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Hangman, show letters

    Does anyone know how to edit the continue() method to where I can change the String word to where the letter they select replaces the "-----"?

    Java Code:
    /**
     * TODO:
     * Make it print dashes for the letters.
     * Make it print the letter where index is.
     * Make it continue after start.
     * Add letter to the continue method.
     * Compare random word and selected indexes.
     * Find index of random word, use that index to put the letter into that index
     * then replace the '_' with the letter with that index.
     * Index with Element of the word
     * ...
     */
    
    
    
    /** Import necessary packages*/
    import java.util.Random;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.lang.String;
    
    public class Hangman extends BasicGame
    {
        /**Call all of the variables*/
        Random randy = new Random();
        private int guesses;
        private final int maxGuesses = 6;
        private String myGeneratedRandomWord = getWords();
        private String word = "-----";
        private String letter;
        private String letterSelected;
        private String[] words = {"Apple", "Beret", "Arose", "Along", "Beamy", "becks", "decks", "barks",
                                  "stark", "start", "stabs", "baggy", "asked", "asset", "asses", "audit",
                                  "bowls", "boxes", "bongs", "balls", "boats", "boxer", "brick", "bound",
                                  "brass", "caked", "braid", "caged", "essay", "fault", "dents", "dutch",
                                  "ethos", "dunks", "pains", "faxes", "mummy", "mixer", "mills", "might",
                                  "moral", "teeth", "wings", "works", "walls", "tolls", "crawl", "toxin",
                                  "urine", "tough"};
        
        /**Get the random word from the array*/
        public String getWords (){
            String randomWord = words[randy.nextInt( words.length)];
            return randomWord;
        }
        
        /**Get the indexes of the letter of the random word indices don't start with 1*/
        public String getSelected(){
            return letterSelected;
        }
        
        /**Finds index of the letter of randomWord*/
        public int getIndex(){
            int index = getWords().indexOf(getSelected());
            return index;
        }
        
        /**Get the first input, set strikes to zero, get the random word*/
        public void start(){
            guesses = 0;
            word = getWords();
            letterSelected = JOptionPane.showInputDialog(null, word + "Make your guess!");
            if(myGeneratedRandomWord.indexOf(letterSelected ) != -1) {
                // it contains the letter
                
           }
           else {
                // wrong guess
                guesses++;
           }
        }
        
        /**Continue the game on, checks if the letter is there if not then apply the letter*/
        public void continueGame(){
           letterSelected = JOptionPane.showInputDialog(null, "Take another guess!");
           if(myGeneratedRandomWord.indexOf(letterSelected ) != -1) {
                // it contains the letter
           }
           else {
                // wrong guess
                guesses++;
           }
        }
        
        /** Runner basically*/
        public void mainScreen (){
            start();
            while (guesses != maxGuesses){
                continueGame();
                
            }
            gameOver();
        }
        
        /**Gets the */
        
        
        /**After certain strikes head is called etc.*/
        public void checkBodyParts(){
            if(guesses == 1){
                System.out.println("You now have a head! Guesses left: 5");
            }
            if(guesses == 2){
                System.out.println("You now have a body! Guesses left: 4");
            }
            if(guesses == 3){
                System.out.println("You now have a right arm! Guesses left: 3");
            }
            if(guesses == 4){
                System.out.println("You now have a left arm! Guesses left: 2");
            }
            if(guesses == 5){
                System.out.println("You now have a right leg! Guesses left: 1");
            }
            if(guesses == maxGuesses){
                System.out.println("You lose!");
            }
        }
        
        /**You lose.*/
        public void gameOver(){
            System.out.println("Game Over!");
            System.exit(0);
        }
    }

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Hangman, show letters

    Can anyone help me finish this thing, seriously... All I need is for it to show the freaking words.

    Java Code:
    /**
     * TODO:
     * Make it print dashes for the letters.
     * Make it print the letter where index is.
     * Make it continue after start.
     * Add letter to the continue method.
     * Compare random word and selected indexes.
     * Find index of random word, use that index to put the letter into that index
     * then replace the '_' with the letter with that index.
     * Index with Element of the word
     * ...
     */
    
    
    
    /** Import necessary packages*/
    import java.util.Random;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.lang.String;
    
    public class Hangman extends BasicGame
    {
        /**Call all of the variables*/
        Random randy = new Random();
        private int guesses;
        private final int maxGuesses = 6;
        private String myGeneratedRandomWord = getWords();
        private String letter;
        private String letterSelected;
        private String[] words = {"apple", "beret", "arose", "along", "beamy", "becks", "decks", "barks",
                                  "stark", "start", "stabs", "baggy", "asked", "asset", "asses", "audit",
                                  "bowls", "boxes", "seats", "balls", "boats", "boxer", "brick", "bound",
                                  "brass", "caked", "braid", "caged", "essay", "fault", "dents", "dutch",
                                  "ethos", "dunks", "pains", "faxes", "mummy", "mixer", "mills", "might",
                                  "moral", "teeth", "wings", "works", "walls", "tolls", "crawl", "toxin",
                                  "bangs", "tough"};
        
        /**Get the random word from the array*/
        public String getWords (){
            String randomWord = words[randy.nextInt( words.length)];
            return randomWord;
        }
        
        /**Get the indexes of the letter of the random word indices don't start with 1*/
        public String getSelected(){
            return letterSelected;
        }
        
        /**Finds index of the letter of randomWord*/
        public int getIndex(){
            int index = getWords().indexOf(getSelected());
            return index;
        }
        
        /**Get the first input, set strikes to zero, get the random word*/
        public void start(){
            guesses = 0;
            getWords();
            letterSelected = JOptionPane.showInputDialog(null, "Make your guess!");
            if(myGeneratedRandomWord.indexOf(letterSelected ) != -1) {
                // it contains the letter
                
           }
           else {
                // wrong guess
                guesses++;
           }
        }
        
        /**Continue the game on, checks if the letter is there if not then apply the letter*/
        public void continueGame(){
           letterSelected = JOptionPane.showInputDialog(null, "Take another guess!");
           if(myGeneratedRandomWord.indexOf(letterSelected ) != -1) {
                // it contains the letter
                getIndex(getWords());
           }
           else {
                // wrong guess
                guesses++;
           }
        }
        
        /** Runner basically*/
        public void mainScreen (){
            start();
            while (guesses != maxGuesses){
                continueGame();
                checkBodyParts();
            }
            gameOver();
        }
        
        /**Gets the */
        
        
        /**After certain strikes head is called etc.*/
        public void checkBodyParts(){
            if(guesses == 1){
                System.out.println("You now have a head! Wrong guesses left: 5");
            }
            if(guesses == 2){
                System.out.println("You now have a body! Wrong guesses left: 4");
            }
            if(guesses == 3){
                System.out.println("You now have a right arm! Wrong guesses left: 3");
            }
            if(guesses == 4){
                System.out.println("You now have a left arm! Wrong guesses left: 2");
            }
            if(guesses == 5){
                System.out.println("You now have a right leg! Wrong guesses left: 1");
            }
            if(guesses == maxGuesses){
                System.out.println("You lose!");
            }
        }
        
        /**You lose.*/
        public void gameOver(){
            System.out.println("Game Over!");
            System.exit(0);
        }
    }

  6. #6
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Hangman, show letters

    I still need some help with this...

Similar Threads

  1. Convert English letters to Tamil letters
    By srinivasmallabathula in forum New To Java
    Replies: 1
    Last Post: 10-20-2012, 02:39 PM
  2. Hangman
    By Feriscool in forum New To Java
    Replies: 23
    Last Post: 05-17-2011, 06:54 PM
  3. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  4. hangman
    By coltragon in forum New To Java
    Replies: 2
    Last Post: 01-16-2010, 09:56 AM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 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
  •