Results 1 to 3 of 3
Thread: hangman
- 11-13-2009, 10:30 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
hangman
i'm trying to create a simple version of hangman w/out using array/arraylist.
my code is attached works but would like to be able to change value of a string character position w/out having to use a "dummy" string (answer).
my code ask user to enter a word.
then draws blank for each char
when user enters a letter use a loop to find it in the string but...
would like to be able to do something like
word (i) = guess; which obviously you cannot do.
is there a way to directly overwrite the char stored in a certain postion in a string?
- 11-14-2009, 12:27 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Consider using a StringBuilder which is mutable. Ie you can do things like:
Java Code:StringBuilder foo = new StringBuilder("foo"); foo.setCharAt(1, '_'); System.out.println(foo);
- 11-14-2009, 10:06 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
This hangman question is asked so often that I decided to implement it myself and post it as a complete spoonfeeding spoiler; all the work is done by the regular expression engine (no ArrayLists, no loops, no arrays, no character comparisons, no nothing):
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class HangMan { private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); private String correct, used, word; private int turns; private void setup(String w, int turns) { correct= "-"; used= ""; word= w; this.turns= turns; } private String word(String prompt) { String word= this.word.replaceAll("[^"+correct+"]", "-"); System.out.println(prompt+": "+word); return word; } private boolean wins(String word) { boolean wins= word.indexOf('-') < 0; if (wins) System.out.println("You guessed the word"); return wins; } private String guess(char x) { if (used.indexOf(x) >= 0) return word("Character '"+x+"' already used"); used+= x; if (word.indexOf(x) < 0) { turns--; return word("Character '"+x+"' is incorrect"); } correct+= x; return word("Character '"+x+"' is correct"); } private char input() { String s= ""; try { for (; (s= s.trim()).length() == 0; s= br.readLine()) System.out.print("Guess a character: "); } catch (IOException ioe) { System.exit(1); } return s.charAt(0); } public boolean play(String word) { for (setup(word, word.length());; System.out.println("turns: "+turns)) if (wins(guess(input()))) return true; else if (turns == 0) { System.out.println("You lose"); return false; } } public static void main(String[] args) { new HangMan().play("defenestration"); } }
Jos
Similar Threads
-
Hangman GUI help
By kurt in forum New To JavaReplies: 5Last Post: 05-22-2009, 11:22 AM -
Need help with hangman game
By kurt in forum New To JavaReplies: 4Last Post: 04-25-2009, 07:47 PM -
Need help with Hangman!!!
By chinasome in forum New To JavaReplies: 10Last Post: 11-09-2008, 05:42 AM -
Hangman Help!!!
By chinasome in forum New To JavaReplies: 5Last Post: 11-08-2008, 03:30 AM -
Hangman Game
By L23 in forum New To JavaReplies: 8Last Post: 07-03-2008, 02:56 PM
Bookmarks