Results 1 to 3 of 3

Thread: hangman

  1. #1
    javaMike is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default 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?
    Attached Files Attached Files

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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);

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    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");
    	}
    }
    kind regards,

    Jos

Similar Threads

  1. Hangman GUI help
    By kurt in forum New To Java
    Replies: 5
    Last Post: 05-22-2009, 10:22 AM
  2. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 06:47 PM
  3. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 04:42 AM
  4. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 02:30 AM
  5. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 01:56 PM

Posting Permissions

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