473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Enquires on Hangman

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
1 2558
JosAH
11,448 Expert 8TB
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
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> ...
18
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?...
4
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:...
47
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...
3
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
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...
0
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...
8
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.