Results 1 to 6 of 6
Thread: Hangman, show letters
- 01-11-2013, 06:16 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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!"); } } }
- 01-11-2013, 07:09 PM #2
Member
- Join Date
- Jan 2013
- Location
- FailLand
- Posts
- 6
- Rep Power
- 0
Re: Hangman, show letters
This guy will definitely help you out with all your string problems:
- 01-12-2013, 04:40 AM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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?
ignore the intermingle of println and panes lolJava 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!"); } }
- 01-12-2013, 06:51 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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); } }
- 01-14-2013, 04:18 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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); } }
- 01-18-2013, 01:03 AM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Similar Threads
-
Convert English letters to Tamil letters
By srinivasmallabathula in forum New To JavaReplies: 1Last Post: 10-20-2012, 02:39 PM -
Hangman
By Feriscool in forum New To JavaReplies: 23Last Post: 05-17-2011, 06:54 PM -
how to show web browser in mobile app or is it possible to show it in textArea
By Basit781 in forum CLDC and MIDPReplies: 3Last Post: 05-27-2010, 10:54 AM -
hangman
By coltragon in forum New To JavaReplies: 2Last Post: 01-16-2010, 09:56 AM -
netbeans 6.0 not show commpunent or show blank page
By fahimaamir in forum NetBeansReplies: 1Last Post: 01-26-2008, 06:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks