Console Hangman Game

Hangman Java Simple Mode (Console Based)

Page 1 of 1

14 Replies - 7290 Views - Last Post: 15 January 2009 - 08:32 PM Rate Topic: ***** 1 Votes

#1 Valkerion   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 265
  • Joined: 13-January 09

Console Hangman Game

Post icon  Posted 13 January 2009 - 07:06 PM

Greetings from Greece.
We got a Hangman assignment for my Class at Java.
I m just a begginer.And the whole input/output thing is done either by console (system.out.println) or with JOptionPane.

So here is my code


import javax.swing.JOptionPane;
import java.util.*;


public class Hangman
{




	public static void main (String a [])
	{



					//////////////////

					String word = "";
					int badGuesses;

					//////////////////





					/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

					String difficulty = JOptionPane.showInputDialog ("Select difficulty : 1 for easy -- 2 for normal -- 3 for hard");
					int	intDifficulty = Integer.parseInt(difficulty);

					/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






					/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

					String easyDictionary [] = { " abstraction ", " ambiguous ", " arithmetic ", " backslash " , " bitmap", " circumstance", " combination", " consequently", " consortium", " decrementing", " dependency", " disambiguate", " dynamic", " encapsulation", " equivalent", " expression", " facilitate", " fragment", " hexadecimal", " implementation", " indistinguishable", " inheritance", " internet", " java", " localization", " microprocessor", " navigation", " optimization", " parameter", " patrick", " pickle", " polymorphic", " rigorously", " simultaneously", " specification", " structure", " lexical", " likewise", " management", " manipulate", " mathematics", " hotjava", " vertex", " unsigned", " traditional" };


					String normalDictionary [] = {"boy", "girl", "mother", "father", "sister", "love", "sky", "wind", "water", "study", "ball",
					"cat", "dog", "puppy", "kitten", "apple", "pear", "lemon", "mango", "peach", "apricot", "chips", "steak", "fries", "cheese",
					"patatoe", "wedge", "heel", "hand", "foot", "arm", "leg", "nose", "face", "mouth", "tongue", "fingers", "toes", "line", "space",
					"phone", "cord", "core", "grass", "trees", "birds", "animals", "lazy", "funny", "king", "queen", "heart", "heat", "cold", "sun",
					"moon", "movie", "theater", "hairy", "big", "small", "large", "huge", "pig", "donkey", "cow", "chicken", "pizza", "bread", "stones",
					"sticks", "leaves", "letters", "alphabet", "soup", "hungry", "tired", "sleepy", "noisy", "caring", "friends", "month", "day", "light"};


					String hardDictionary [] = { "toothbrush", "savings", "bank", "account", "teller", "paper", "pencil", "tea", "coffee", "spirit", "ghost", "can", "melon", "necklace",
					"screen", "baloon", "string", "calendar", "work", "toys", "kids", "school", "class", "campus", "freedom", "liberty", "happiness",
					"university", "message", "marker", "crayon", "eraser", "music", "lyrics", "songs", "ballads", "shapes", "triangle", "circle", "rectangle",
					"square", "oval", "show", "video", "player", "team", "sport", "basketball", "football", "soccer", "softball", "baseball", "tennis",
					"hockey", "lacrosse", "volleyball", "circut", "blade", "scratch", "hit", "home", "house", "safe", "safety", "number", "count", "bear",
					"goose", "lama", "panda", "lion", "tiger", "cheetah", "computer", "crackers", "rice", "fan", "shoes", "book", "story", "princess",
					"prince", "jester", "court", "jury", "judge", "bench", "scandal", "name", "newspaper", "press", "shove", "tear", "cry", "magic", "tricks",
					"cereal", "breakfast", "lunch", "dinner", "main", "course", "fork", "spoon", "knife", "lamp", "desk", "bottle", "highlighter", "cap",
					"medicine", "six", "seven", "flower", "rose", "petal"};

					////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



					///////////////////////////////////////////////

					Scanner letterProvided = new Scanner(System.in);
					String pickedArray [] = {""};
					String letter;

					///////////////////////////////////////////////




				/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

				if (intDifficulty == 1 )
				{

					Collections.shuffle( Arrays.asList ( easyDictionary ));
					word = ( easyDictionary[1] );
					System.out.println( "Easy huh?" );

					
				}

				else if (intDifficulty == 2)
				{
					Collections.shuffle( Arrays.asList (normalDictionary ));
					word = ( normalDictionary[1] );
					System.out.println( "At last!A normal guy." );

					
				}
				else if (intDifficulty == 3)
				{
					Collections.shuffle( Arrays.asList ( hardDictionary ));
					word =( hardDictionary[1] );
					System.out.println ( "You think you are a tough guy?" );

				
				}
				else
				{
					JOptionPane.showMessageDialog( null,"ERROR TYPE #1: Please input 1 , 2  or 3 to select Hangman's difficulty!" , "Error!" ,JOptionPane.ERROR_MESSAGE );
				}

				/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



				///////////////////////////////////////////////

				String [] hidden = new String [word.length()];

				///////////////////////////////////////////////



				///////////////////////////////////////////////

				for ( int i = 0; i < word.length(); i++ )
				{
					hidden[i] = "_";
				}

				///////////////////////////////////////////////



					/////////////////////////////////////////////////////////////
					System.out.print("Please enter a letter in lower case only!");
					letter = letterProvided.nextLine();
					
					
		



		}



}




I got some problems here.
First I need my word to be in an array so to check if the input is correct or not.With an array ill be able to check double letters also! So I just dont know what to do with the input I got from
System.out.print("Please enter a letter in lower case only!");
					letter = letterProvided.nextLine();

Any ideas? =\


Secondly i need a counter about badGuesses (because we have to set a maximum number of wrong letters that someone will give ) , but i cant figure out where i should put the loop and do
badGuesses--;



Thank you a lot for reading my post =)

This post has been edited by Valkerion: 13 January 2009 - 07:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Console Hangman Game

#2 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Console Hangman Game

Posted 13 January 2009 - 08:31 PM

Wow, please avoid all the "//////" in your code as it makes it extremely hard to read, if you need to do a comment write a comment, but it is standard to just continue one line after another, not "///////" then next part of code.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Console Hangman Game

Posted 13 January 2009 - 08:42 PM

And avoid useless blank space... we can only see a few lines of code at a time instead of a full method()
really pain in the heck to read

import javax.swing.JOptionPane;
import java.util.*;


public class Hangman
{
	public static void main (String a [])
	{
		String word = "";
		int badGuesses;

		String difficulty = JOptionPane.showInputDialog ("Select difficulty : 1 for easy -- 2 for normal -- 3 for hard");
		int	intDifficulty = Integer.parseInt(difficulty);
		String easyDictionary [] = { " abstraction ", " ambiguous ", " arithmetic ", " backslash " , " bitmap", " circumstance", " combination", " consequently", " consortium", " decrementing", " dependency", " disambiguate", " dynamic", " encapsulation", " equivalent", " expression", " facilitate", " fragment", " hexadecimal", " implementation", " indistinguishable", " inheritance", " internet", " java", " localization", " microprocessor", " navigation", " optimization", " parameter", " patrick", " pickle", " polymorphic", " rigorously", " simultaneously", " specification", " structure", " lexical", " likewise", " management", " manipulate", " mathematics", " hotjava", " vertex", " unsigned", " traditional" };

		String normalDictionary [] = {"boy", "girl", "mother", "father", "sister", "love", "sky", "wind", "water", "study", "ball",
				"cat", "dog", "puppy", "kitten", "apple", "pear", "lemon", "mango", "peach", "apricot", "chips", "steak", "fries", "cheese",
				"patatoe", "wedge", "heel", "hand", "foot", "arm", "leg", "nose", "face", "mouth", "tongue", "fingers", "toes", "line", "space",
				"phone", "cord", "core", "grass", "trees", "birds", "animals", "lazy", "funny", "king", "queen", "heart", "heat", "cold", "sun",
				"moon", "movie", "theater", "hairy", "big", "small", "large", "huge", "pig", "donkey", "cow", "chicken", "pizza", "bread", "stones",
				"sticks", "leaves", "letters", "alphabet", "soup", "hungry", "tired", "sleepy", "noisy", "caring", "friends", "month", "day", "light"};

		String hardDictionary [] = { "toothbrush", "savings", "bank", "account", "teller", "paper", "pencil", "tea", "coffee", "spirit", "ghost", "can", "melon", "necklace",
				"screen", "baloon", "string", "calendar", "work", "toys", "kids", "school", "class", "campus", "freedom", "liberty", "happiness",
				"university", "message", "marker", "crayon", "eraser", "music", "lyrics", "songs", "ballads", "shapes", "triangle", "circle", "rectangle",
				"square", "oval", "show", "video", "player", "team", "sport", "basketball", "football", "soccer", "softball", "baseball", "tennis",
				"hockey", "lacrosse", "volleyball", "circut", "blade", "scratch", "hit", "home", "house", "safe", "safety", "number", "count", "bear",
				"goose", "lama", "panda", "lion", "tiger", "cheetah", "computer", "crackers", "rice", "fan", "shoes", "book", "story", "princess",
				"prince", "jester", "court", "jury", "judge", "bench", "scandal", "name", "newspaper", "press", "shove", "tear", "cry", "magic", "tricks",
				"cereal", "breakfast", "lunch", "dinner", "main", "course", "fork", "spoon", "knife", "lamp", "desk", "bottle", "highlighter", "cap",
				"medicine", "six", "seven", "flower", "rose", "petal"};

		Scanner letterProvided = new Scanner(System.in);
		String pickedArray [] = {""};
		String letter;

		if (intDifficulty == 1 )
		{
			Collections.shuffle( Arrays.asList ( easyDictionary ));
			word = ( easyDictionary[1] );
			System.out.println( "Easy huh?" );
		}
		else if (intDifficulty == 2)
		{
			Collections.shuffle( Arrays.asList (normalDictionary ));
			word = ( normalDictionary[1] );
			System.out.println( "At last!A normal guy." );
		}
		else if (intDifficulty == 3)
		{
			Collections.shuffle( Arrays.asList ( hardDictionary ));
			word =( hardDictionary[1] );
			System.out.println ( "You think you are a tough guy?" );
		}
		else
		{
			JOptionPane.showMessageDialog( null,"ERROR TYPE #1: Please input 1 , 2  or 3 to select Hangman's difficulty!" , "Error!" ,JOptionPane.ERROR_MESSAGE );
		}

		String [] hidden = new String [word.length()];

		for ( int i = 0; i < word.length(); i++ )
		{
			hidden[i] = "_";
		}

		System.out.print("Please enter a letter in lower case only!");
		letter = letterProvided.nextLine();
	}
}



Now you might have an answer

This post has been edited by pbl: 13 January 2009 - 08:45 PM

Was This Post Helpful? 1
  • +
  • -

#4 ProGraM   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Re: Console Hangman Game

Posted 13 January 2009 - 08:42 PM

Here cleaned it up a little so its easier to read

import javax.swing.JOptionPane;
import java.util.*;
public class Hangman
{
	public static void main (String a [])
	{
					String word = "";
					int badGuesses;
					String difficulty = JOptionPane.showInputDialog ("Select difficulty : 1 for easy -- 2 for normal -- 3 for hard");
					int intDifficulty = Integer.parseInt(difficulty);		
					String easyDictionary [] = { " abstraction ", " ambiguous ", " arithmetic ", " backslash " , " bitmap", " circumstance", " combination", " consequently", " consortium", " decrementing", " dependency", " disambiguate", " dynamic", " encapsulation", " equivalent", " expression", " facilitate", " fragment", " hexadecimal", " implementation", " indistinguishable", " inheritance", " internet", " java", " localization", " microprocessor", " navigation", " optimization", " parameter", " patrick", " pickle", " polymorphic", " rigorously", " simultaneously", " specification", " structure", " lexical", " likewise", " management", " manipulate", " mathematics", " hotjava", " vertex", " unsigned", " traditional" };
					String normalDictionary [] = {"boy", "girl", "mother", "father", "sister", "love", "sky", "wind", "water", "study", "ball",  "cat", "dog", "puppy", "kitten", "apple", "pear", "lemon", "mango", "peach", "apricot", "chips", "steak", "fries", "cheese", "patatoe", "wedge", "heel", "hand", "foot", "arm", "leg", "nose", "face", "mouth", "tongue", "fingers", "toes", "line", "space", "phone", "cord", "core", "grass", "trees", "birds", "animals", "lazy", "funny", "king", "queen", "heart", "heat", "cold", "sun",  "moon", "movie", "theater", "hairy", "big", "small", "large", "huge", "pig", "donkey", "cow", "chicken", "pizza", "bread", "stones", "sticks", "leaves", "letters", "alphabet", "soup", "hungry", "tired", "sleepy", "noisy", "caring", "friends", "month", "day", "light"};
					String hardDictionary [] = { "toothbrush", "savings", "bank", "account", "teller", "paper", "pencil", "tea", "coffee", "spirit", "ghost", "can", "melon", "necklace",   "screen", "baloon", "string", "calendar", "work", "toys", "kids", "school", "class", "campus", "freedom", "liberty", "happiness",
 "university", "message", "marker", "crayon", "eraser", "music", "lyrics", "songs", "ballads", "shapes", "triangle", "circle", "rectangle", "square", "oval", "show", "video", "player", "team", "sport", "basketball", "football", "soccer", "softball", "baseball", "tennis", "hockey", "lacrosse", "volleyball", "circut", "blade", "scratch", "hit", "home", "house", "safe", "safety", "number", "count", "bear", "goose", "lama", "panda", "lion", "tiger", "cheetah", "computer", "crackers", "rice", "fan", "shoes", "book", "story", "princess", "prince", "jester", "court", "jury", "judge", "bench", "scandal", "name", "newspaper", "press", "shove", "tear", "cry", "magic", "tricks", "cereal", "breakfast", "lunch", "dinner", "main", "course", "fork", "spoon", "knife", "lamp", "desk", "bottle", "highlighter", "cap", "medicine", "six", "seven", "flower", "rose", "petal"};
					Scanner letterProvided = new Scanner(System.in);
					String pickedArray [] = {""};
					String letter;
				if (intDifficulty == 1 )
				{
					Collections.shuffle( Arrays.asList ( easyDictionary ));
					word = ( easyDictionary[1] );
					System.out.println( "Easy huh?" );	   
				else if (intDifficulty == 2)
				{
					Collections.shuffle( Arrays.asList (normalDictionary ));
					word = ( normalDictionary[1] );
					System.out.println( "At last!A normal guy." );	
				}
				else if (intDifficulty == 3)
				{
					Collections.shuffle( Arrays.asList ( hardDictionary ));
					word =( hardDictionary[1] );
					System.out.println ( "You think you are a tough guy?" );	 
				}
				else
				{
					JOptionPane.showMessageDialog( null,"ERROR TYPE #1: Please input 1 , 2  or 3 to select Hangman's difficulty!" , "Error!" ,JOptionPane.ERROR_MESSAGE );
				}
				String [] hidden = new String [word.length()];
				for ( int i = 0; i < word.length(); i++ )
				{
					hidden[i] = "_";
				}
					System.out.print("Please enter a letter in lower case only!");
				letter = letterProvided.nextLine();						 
		}
}


also for the
 badguesses-- 


you put this after they input a letter and that letter is not in the word...
so you would have the program search through the word the program chose for the user to guess and when it has searched through the word it will take the badguesses and either subtract a counter from it or leave it alone.

so what you want to do is set badguesses as an integer equal to how many bad guesses they get until they lose. so if bad guesses gets to 0 they lose

so lets say they are only alowed to have 6 bad guesses( head, body, 2 arms, 2 legs). you would put

 int badguesses = 6
if(letter != word.length)//not sure if this ifstatement is right
badguesses --;
if(badguesses == 0
System.out.println("You lose");
System.exit(0);



im not sure if that is totally correct but it should look something like that

This post has been edited by ProGraM: 13 January 2009 - 08:56 PM

Was This Post Helpful? 1
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Console Hangman Game

Posted 13 January 2009 - 09:29 PM

Better to use char rather than String
Also I trimmed your words as some have extra space at the beginning and end of them
Also make dictionnary[][] simpler to test
So that is what I end up with... not tested at all

import javax.swing.JOptionPane;
import java.util.*;


public class Hangman
{
	public static void main (String a [])
	{
		String word;
		int badGuesses;


		String dictionaries [][] = {
				{ " abstraction ", " ambiguous ", " arithmetic ", " backslash " , " bitmap", " circumstance", " combination", " consequently", " consortium", 
					" decrementing", " dependency", " disambiguate", " dynamic", " encapsulation", " equivalent", " expression", " facilitate", " fragment", 
					" hexadecimal", " implementation", " indistinguishable", " inheritance", " internet", " java", " localization", " microprocessor", 
					" navigation", " optimization", " parameter", " patrick", " pickle", " polymorphic", " rigorously", " simultaneously", " specification", 
					" structure", " lexical", " likewise", " management", " manipulate", " mathematics", " hotjava", " vertex", " unsigned", " traditional" 
				},

				{"boy", "girl", "mother", "father", "sister", "love", "sky", "wind", "water", "study", "ball",
					"cat", "dog", "puppy", "kitten", "apple", "pear", "lemon", "mango", "peach", "apricot", "chips", "steak", "fries", "cheese",
					"patatoe", "wedge", "heel", "hand", "foot", "arm", "leg", "nose", "face", "mouth", "tongue", "fingers", "toes", "line", "space",
					"phone", "cord", "core", "grass", "trees", "birds", "animals", "lazy", "funny", "king", "queen", "heart", "heat", "cold", "sun",
					"moon", "movie", "theater", "hairy", "big", "small", "large", "huge", "pig", "donkey", "cow", "chicken", "pizza", "bread", "stones",
					"sticks", "leaves", "letters", "alphabet", "soup", "hungry", "tired", "sleepy", "noisy", "caring", "friends", "month", "day", "light"
				},

				{ "toothbrush", "savings", "bank", "account", "teller", "paper", "pencil", "tea", "coffee", "spirit", "ghost", "can", "melon", "necklace",
					"screen", "baloon", "string", "calendar", "work", "toys", "kids", "school", "class", "campus", "freedom", "liberty", "happiness",
					"university", "message", "marker", "crayon", "eraser", "music", "lyrics", "songs", "ballads", "shapes", "triangle", "circle", "rectangle",
					"square", "oval", "show", "video", "player", "team", "sport", "basketball", "football", "soccer", "softball", "baseball", "tennis",
					"hockey", "lacrosse", "volleyball", "circut", "blade", "scratch", "hit", "home", "house", "safe", "safety", "number", "count", "bear",
					"goose", "lama", "panda", "lion", "tiger", "cheetah", "computer", "crackers", "rice", "fan", "shoes", "book", "story", "princess",
					"prince", "jester", "court", "jury", "judge", "bench", "scandal", "name", "newspaper", "press", "shove", "tear", "cry", "magic", "tricks",
					"cereal", "breakfast", "lunch", "dinner", "main", "course", "fork", "spoon", "knife", "lamp", "desk", "bottle", "highlighter", "cap",
					"medicine", "six", "seven", "flower", "rose", "petal"
				}};

		String difficulty = JOptionPane.showInputDialog ("Select difficulty : 1 for easy -- 2 for normal -- 3 for hard");
		int intDifficulty = Integer.parseInt(difficulty);

		if(intDifficulty > dictionaries.length)
		{
			System.out.println("Sorry answer between 1 and 3");
			return;
		}
		String pickedArray[] = dictionaries[intDifficulty];
		String[] difficultyMsg = {"Easy huh?", "At last!A normal guy.", "You think you are a tough guy?"};
		System.out.println(difficultyMsg[intDifficulty]);

		// I added the Trim bacause somme of your word start/end with space like " backslash "
		word = pickedArray[0].trim();
		// make hidden a char array of the word to guess
		char[] hidden = new char[word.length()];
		// and filled it with '_'
		for(int i = 0; i < word.length(); i++)
			hidden[i] = '_';
		// translate the word to guess into an array of char
		char[] digit = word.toCharArray();

		// build a scaner to read user input
		Scanner letterProvided = new Scanner(System.in);

		badGuesses = 0;
		do {
			// print what we have
			System.out.print("So far: ");
			for(int i = 0; i < word.length(); i++)
				System.out.print(hidden[i]);
			// input letter from user
			System.out.print("\nPlease enter a letter: ");
			// put it in lower case
			String letter = letterProvided.nextLine().toLowerCase();
			// extract first char
			char guess = letter.charAt(0);
			// flag if found
			boolean correctGuess = false;
			// scan if it is a letter in the word
			for(int i = 0; i < word.length(); i++)
			{
				// yes it is
				if(guess == digit[i])
				{
					// so replace it by the char
					hidden[i] = guess;
					// and flag not to increment badGuesses counter
					correctGuess = true;
				}
			}
			// if not found increment bad guesses
			if(!correctGuess)
				badGuesses++;
		}	while(badGuesses < 5);	// <-- put here number of guess
	}
}



Hope this helps
Was This Post Helpful? 1
  • +
  • -

#6 Valkerion   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 265
  • Joined: 13-January 09

Re: Console Hangman Game

Posted 14 January 2009 - 06:00 AM

Thank you a lot guys.About the ///// at TextPad it seems OK thats why.I am really sorry!I ll just study a bit your helpful replies and then come back to you! :)
Was This Post Helpful? 0
  • +
  • -

#7 Valkerion   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 265
  • Joined: 13-January 09

Re: Console Hangman Game

Posted 14 January 2009 - 06:07 AM

View Postpbl, on 13 Jan, 2009 - 08:29 PM, said:

Better to use char rather than String
Also I trimmed your words as some have extra space at the beginning and end of them
Also make dictionnary[][] simpler to test
So that is what I end up with... not tested at all

import javax.swing.JOptionPane;
import java.util.*;


public class Hangman
{
	public static void main (String a [])
	{
		String word;
		int badGuesses;


		String dictionaries [][] = {
				{ " abstraction ", " ambiguous ", " arithmetic ", " backslash " , " bitmap", " circumstance", " combination", " consequently", " consortium", 
					" decrementing", " dependency", " disambiguate", " dynamic", " encapsulation", " equivalent", " expression", " facilitate", " fragment", 
					" hexadecimal", " implementation", " indistinguishable", " inheritance", " internet", " java", " localization", " microprocessor", 
					" navigation", " optimization", " parameter", " patrick", " pickle", " polymorphic", " rigorously", " simultaneously", " specification", 
					" structure", " lexical", " likewise", " management", " manipulate", " mathematics", " hotjava", " vertex", " unsigned", " traditional" 
				},

				{"boy", "girl", "mother", "father", "sister", "love", "sky", "wind", "water", "study", "ball",
					"cat", "dog", "puppy", "kitten", "apple", "pear", "lemon", "mango", "peach", "apricot", "chips", "steak", "fries", "cheese",
					"patatoe", "wedge", "heel", "hand", "foot", "arm", "leg", "nose", "face", "mouth", "tongue", "fingers", "toes", "line", "space",
					"phone", "cord", "core", "grass", "trees", "birds", "animals", "lazy", "funny", "king", "queen", "heart", "heat", "cold", "sun",
					"moon", "movie", "theater", "hairy", "big", "small", "large", "huge", "pig", "donkey", "cow", "chicken", "pizza", "bread", "stones",
					"sticks", "leaves", "letters", "alphabet", "soup", "hungry", "tired", "sleepy", "noisy", "caring", "friends", "month", "day", "light"
				},

				{ "toothbrush", "savings", "bank", "account", "teller", "paper", "pencil", "tea", "coffee", "spirit", "ghost", "can", "melon", "necklace",
					"screen", "baloon", "string", "calendar", "work", "toys", "kids", "school", "class", "campus", "freedom", "liberty", "happiness",
					"university", "message", "marker", "crayon", "eraser", "music", "lyrics", "songs", "ballads", "shapes", "triangle", "circle", "rectangle",
					"square", "oval", "show", "video", "player", "team", "sport", "basketball", "football", "soccer", "softball", "baseball", "tennis",
					"hockey", "lacrosse", "volleyball", "circut", "blade", "scratch", "hit", "home", "house", "safe", "safety", "number", "count", "bear",
					"goose", "lama", "panda", "lion", "tiger", "cheetah", "computer", "crackers", "rice", "fan", "shoes", "book", "story", "princess",
					"prince", "jester", "court", "jury", "judge", "bench", "scandal", "name", "newspaper", "press", "shove", "tear", "cry", "magic", "tricks",
					"cereal", "breakfast", "lunch", "dinner", "main", "course", "fork", "spoon", "knife", "lamp", "desk", "bottle", "highlighter", "cap",
					"medicine", "six", "seven", "flower", "rose", "petal"
				}};

		String difficulty = JOptionPane.showInputDialog ("Select difficulty : 1 for easy -- 2 for normal -- 3 for hard");
		int intDifficulty = Integer.parseInt(difficulty);

		if(intDifficulty > dictionaries.length)
		{
			System.out.println("Sorry answer between 1 and 3");
			return;
		}
		String pickedArray[] = dictionaries[intDifficulty];
		String[] difficultyMsg = {"Easy huh?", "At last!A normal guy.", "You think you are a tough guy?"};
		System.out.println(difficultyMsg[intDifficulty]);

		// I added the Trim bacause somme of your word start/end with space like " backslash "
		word = pickedArray[0].trim();
		// make hidden a char array of the word to guess
		char[] hidden = new char[word.length()];
		// and filled it with '_'
		for(int i = 0; i < word.length(); i++)
			hidden[i] = '_';
		// translate the word to guess into an array of char
		char[] digit = word.toCharArray();

		// build a scaner to read user input
		Scanner letterProvided = new Scanner(System.in);

		badGuesses = 0;
		do {
			// print what we have
			System.out.print("So far: ");
			for(int i = 0; i < word.length(); i++)
				System.out.print(hidden[i]);
			// input letter from user
			System.out.print("\nPlease enter a letter: ");
			// put it in lower case
			String letter = letterProvided.nextLine().toLowerCase();
			// extract first char
			char guess = letter.charAt(0);
			// flag if found
			boolean correctGuess = false;
			// scan if it is a letter in the word
			for(int i = 0; i < word.length(); i++)
			{
				// yes it is
				if(guess == digit[i])
				{
					// so replace it by the char
					hidden[i] = guess;
					// and flag not to increment badGuesses counter
					correctGuess = true;
				}
			}
			// if not found increment bad guesses
			if(!correctGuess)
				badGuesses++;
		}	while(badGuesses < 5);	// <-- put here number of guess
	}
}



Hope this helps



I cant see where you do the Random Chooser from the arrays.It has to choose a random word from the chosen difficulty array.Thats why used Shuffle command
Was This Post Helpful? 0
  • +
  • -

#8 Valkerion   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 265
  • Joined: 13-January 09

Re: Console Hangman Game

Posted 14 January 2009 - 06:53 AM

View PostProGraM, on 13 Jan, 2009 - 07:42 PM, said:

also for the
 badguesses-- 


you put this after they input a letter and that letter is not in the word...
so you would have the program search through the word the program chose for the user to guess and when it has searched through the word it will take the badguesses and either subtract a counter from it or leave it alone.

so what you want to do is set badguesses as an integer equal to how many bad guesses they get until they lose. so if bad guesses gets to 0 they lose

so lets say they are only alowed to have 6 bad guesses( head, body, 2 arms, 2 legs). you would put

 int badguesses = 6
if(letter != word.length)//not sure if this ifstatement is right
badguesses --;
if(badguesses == 0
System.out.println("You lose");
System.exit(0);



im not sure if that is totally correct but it should look something like that



Can you explain me why you use this if statement ?
if(letter != word.length)


And about the random i just made this :
 Collections.shuffle( Arrays.asList ( pickedArray ));
					word = pickedArray[ (int) Math.random() * pickedArray.length];



and i filled the gaps that needed some debugging.Now it works properly!Thank you a lot!

Just one more question.Based on the code provided by Mr. Pbl , do you see any way of printing the guessed letters and if the user inputs one of the used letters not to count as a badGuess?

About printing the guessed letters , i think i should do an array of strings , and when the user inputs something immediatly input the characters into that array.And just print it whenever user guesses a new letter.

I will come soon with some ideas.I dont want code now.I just want you to tell me ideas to prompt me think the right way!
(if i need code i ll tell u :P )

This post has been edited by Valkerion: 14 January 2009 - 07:18 AM

Was This Post Helpful? 0
  • +
  • -

#9 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Console Hangman Game

Posted 14 January 2009 - 02:08 PM

Quote

do you see any way of printing the guessed letters and if the user inputs one of the used letters not to count as a badGuess? About printing the guessed letters , i think i should do an array of strings , and when the user inputs something immediatly input the characters into that array.And just print it whenever user guesses a new letter.


Just have another char array which stores all the current guesses, then you can print out these values, and check if the guess has been made already based on this array.
Was This Post Helpful? 0
  • +
  • -

#10 Valkerion   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 265
  • Joined: 13-January 09

Re: Console Hangman Game

Posted 14 January 2009 - 02:30 PM

I see.In my mind the algorithm is like


X a collection of letters
C the user's guess
if X contains C notify user
else add C to X
Was This Post Helpful? 0
  • +
  • -

#11 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Console Hangman Game

Posted 14 January 2009 - 02:33 PM

Yes, thats how to do it
X a collection of letters
C the user's guess
if X contains C notify user
else add C to X **and increment bad guess counter**
Was This Post Helpful? 0
  • +
  • -

#12 Valkerion   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 265
  • Joined: 13-January 09

Re: Console Hangman Game

Posted 14 January 2009 - 02:34 PM

Cheers!i ll come back when i ll do it :)


EDIT : I gotta begin making my code to methods too !

This post has been edited by Valkerion: 14 January 2009 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#13 Valkerion   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 265
  • Joined: 13-January 09

Re: Console Hangman Game

Posted 14 January 2009 - 05:19 PM

Thanks all of you for your time.Its done and rolling.I will hand it out at Friday I guess because I have to write a report that explains what I have done.

Thanks again!Cya in D.I.C.
Was This Post Helpful? 0
  • +
  • -

#14 zoolltrax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-January 09

Re: Console Hangman Game

Posted 15 January 2009 - 08:30 PM

kavliari tsekare to hangman m kai pes m pos s fenete
pira to suffle apo sena alla dont worry einai ok..eida to allakses evales kai random epano gj
Was This Post Helpful? 0
  • +
  • -

#15 Cup_of_java   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 30-October 08

Re: Console Hangman Game

Posted 15 January 2009 - 08:32 PM

english please....oh and zoolltrax check your inbox
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1