Results 1 to 3 of 3

Thread: hangman

  1. #1
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default hangman

    I'm making a java hangman game for 2 players.
    1 player inserts the word, the other guesses it.

    i thought i was in a good way already :P, but the hardes part is that i have to check if the given letter is in the word.

    this is my code so far:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication5;
    
    /**
     *
     * @author Jesse Tielens
     */
    import java.util.Scanner;
    import java.util.*;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    public class Main {
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           System.out.println("Payer 1, please choose a word that player has to guess");
           Scanner input = new Scanner(System.in);// making an input scanner to get the word
           String str = input.nextLine();
           System.out.println("Player 1 has chosen a word; Player 2. You can now tyr to guess it");
           str.toCharArray();// convert the String to an array(i think i need this later to guess the words)
           Scanner guess = new Scanner(System.in);
           String str1 = guess.nextLine();
           int var3=1;
           if(str1.length() > var3) {
               System.out.println("You entered more tha 2 chaacters, please re-enter:");
               str1=null;
               guess.nextLine(); //checking if string is not more than 1 character!
    
           }
           
           
    
    
           }
        }
    I already converted the word to an array
    with this line of code
    Java Code:
    str.toCharArray();
    i think that is necesarry, because now we have all characters in array seperated from each other.

    how can i check if the given letter is in the array?

    PS: am i in a good way, or doing it the whole wrong way?
    Last edited by coltragon; 01-15-2010 at 07:15 PM.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

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

    Default

    This question pops up ever so often; here's my solution. Read the API documentation for every class/method that is new to you:

    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

  3. #3
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    thanks, i think i gonna apply this things to make another game or something
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

Similar Threads

  1. Hangman Game Help Please
    By 9tjh in forum New To Java
    Replies: 4
    Last Post: 12-04-2009, 03:19 AM
  2. hangman
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2009, 09:06 AM
  3. Hangman GUI help
    By kurt in forum New To Java
    Replies: 5
    Last Post: 05-22-2009, 10:22 AM
  4. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 04:42 AM
  5. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 02:30 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
  •