473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enquires on Hangman

2 New Member
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
1 2572
JosAH
11,448 Recognized Expert MVP
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

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

Similar topics

5
15794
by: tigrfire | last post by:
So I'm trying to write a hangman game and the output is coming out a little strange. Here's my code thus far: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> int hangman()
18
9192
ilikepython
by: ilikepython | last post by:
Hi I'm new to Python(3 to 4 days) and I'm working on a hangman game and have been having some problems that really annoy me. Here is part of my script: c = input('Would you like to play hangman? Type y or n ') word = sample(('soccer', 'robbery', 'antibiotics'), 1) word = str(word) spaces =...
4
2590
by: princessfrost | last post by:
Hi! I was wondering if someone could please help me with a hangman program that I have to do. I have some ideas, but really don't know what to do or where to start. My program needs to be: interactive with the user, menu based(like Menu: 1. Play hangman 2. Exit program), have a dictionaryand use a random method(create arrays of Strings or...
47
11441
by: araujo2nd | last post by:
Originally Posted by araujo2nd my name is andre, im from south africa, i was wondering if any1 could help me with a hangman application, im now in grade 11 and have a huge portfolio piece to do by the 15th which i have just started now, i have always struggled with java and would rele appreciate all the help that i can get. Pls could some1 help...
3
2944
by: kaka_hunter | last post by:
#include <iostream> #include <fstream> using namespace std; const int max_tries=7; int earnings=0; int wordnum; void getword () { ifstream fin;
2
4029
by: tesa | last post by:
I am not able to figure out how to make this work. I am trying to create a hangman game. I am in a basic javascripting class. I am to only use very basic code as you can see. I am able to use any online resources to help me. I have added alot of comments for what should be happening however is not happening. I also have a teacher that...
0
1687
by: Madmartigan | last post by:
Hi I'm a newbie to C# and have been instructed to create a Hangman game in SharpDevelop. I don't want the answer to the full code, just some help along the way. I have included my code thus far and at this stage would like to know how I can get the RandomWordManager, which I found on another site, to display a newly generated word as the...
8
3738
by: tidiz | last post by:
Hi, I'm trying to make a hangman game that should look like this: Welcome to Hangman ______ Your guess: c Success! __cc__ Your guess: b
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.