By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,277 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,277 IT Pros & Developers. It's quick & easy.

Enquires on Hangman

P: 2
Hi to All,

I am doing on a hangman project, Where this is a very simple one, where player would guess the word and if it is correct the letter would be from "-" to the correct letter once all letter is correct they system will appear out good job u clear the game.

I had done up the game and it work however i find mine is rather impractical, looking for better solution for it. please enlighten me in it.

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[]agr)
  2.     {
  3.         String [] fruits ={"apple","pear","watermelon","grape","orange","banana","durian","jackfruit"};
  4.  
  5.         String [] pear ={"p","e","a","r"};
  6.         String answer ="";
  7.         int chooice =(int)(Math.random ()*7);
  8.  
  9.         answer = fruits[chooice];
  10.         if (answer.equals("apple"))
  11.         {
  12.             apple();
  13.         }
  14.         else if (answer.equals("pear"))
  15.         {
  16.                         pear();
  17.                 }
  18. ....
  19.     public static void apple()
  20.     {
  21.         Scanner sc = new Scanner(System.in);
  22.         String word = "";
  23.         String [] apple ={"-","-","-","-","-"};
  24.         int guess =apple.length;
  25.         int correct =0;        
  26.         System.out.println("The word you pick is a Fruits which have "+apple.length+"letter");
  27.         System.out.println("You will have "+ apple.length +"chance to guess the word");
  28.  
  29.         while ( guess>0)
  30.         {    
  31.         for (int b = 0;b<apple.length;b++)
  32.         {
  33.         System.out.print(apple[b]);
  34.         }
  35.         System.out.println();
  36.         System.out.print("Please guess the word\t:");
  37.         word = sc.nextLine();
  38.             if (word.equals("a"))
  39.             {
  40.                 apple[0]= "a";        
  41.                 correct +=1;
  42.             }
  43.  
  44.             else if (word.equals("p"))
  45.             {
  46.                 apple[1]="p";
  47.                 apple[2]="p";
  48.                 correct+=2;
  49.             }
  50. .....
  51.             }
  52.             else
  53.             {
  54.                 System.out.println("You have make an incorrect guess");
  55.             }
  56.             guess--;
  57.             System.out.println("You have left with "+guess+"guess");
  58.  
  59.             if(correct ==apple.length)
  60.             {
  61.                 System.out.println("You have guessed the word correctly");
  62.                 System.out.println("You WIN");
  63.                 break;
  64.             }    
  65.             else if (guess == 0)
  66.             {
  67.                 System.out.println("Sorry you have lost");
  68.                 System.out.println("You have used up all your "+apple.length+" guess");
  69.             }
  70.  
  71.         }
  72.     }
  73. ....
  74. public static void pear()
  75. {
  76. ...[same as the apple but will the "if" will change to match the letter]
  77. }
  78.  
After the above code[part of it] my main question would be will it be possible to have me calling only one method? rather then me having 8 method. As if i want 100 different fruits, then i would need 100 method(which to me is impractical) hence i would like to know what are the possible way to solve this problem?

if you would need to see the whole code in order to help, do tell me i will post it up

P.S. please refer me to other link if they are similar question, as there are really just too much link and i seen some other hangman problem but some are too difficult to understand. [I am currently still new to programming - java]
Oct 17 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
Search for "hangman" (top right of the page); I once showed a complete implementation of the game using regular expressions; it is short without all the kludgy logic and is a complete spoonfeeding giveaway.

kind regards,

Jos
Oct 18 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.