Results 1 to 16 of 16
Thread: Hnagman program
- 05-05-2008, 03:00 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
Hnagman program
Please could any one help to write the code for hangman using while loops, boolean method and text processing..
If the user guesses a letter that is not in the word, the program adds that
letter (in uppercase) to a list of wrong guesses. If the user guesses 6
wrong letters before guessing all the letters of the word, the user loses.
· If the user guesses a letter that is in the word, the program replaces the
corresponding underscore(s) with that letter (in uppercase). If the user
guesses all the letters of the word before making 6 wrong guesses, the
user wins.
· If the user guesses a letter that they have already guessed before, the
program notifies them and asks them to enter another letter (see
example logs of execution for exact wording).
the program output ....
Word: _ _ G
Misses: T
Guess a letter: g
Already guessed! Guess a letter: t
Already guessed! Guess a letter: f
Word: _ _ G
Misses: T,F
Guess a letter: d
Word: D _ G
Misses: T,F
Guess a letter: m
Word: D _ G
Misses: T,F,M
Guess a letter: O
The answer was: DOG
You WIN!
Do you want to play again? Yess!!
- 05-05-2008, 03:25 AM #2
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
could any one help me to write a method for one game
- 05-05-2008, 03:37 AM #3
lol write some yourself first then ask again. Then show us the code and where your stuck
My IP address is 127.0.0.1
- 05-05-2008, 03:49 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
fluffy, what you have tried up to now?
- 05-06-2008, 02:22 AM #5
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
import java.util.*;
public class Hangman {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Let's play Hangman!");
System.out.println();
//String answer = HangmanUtils.getWord();
String answer = "cat";
answer = answer.toUpperCase();
System.out.println("Random word = " + answer);
System.out.println("Random word Length = " + answer.length());
System.out.print("Word: ");
for (int i = 0; i < answer.length(); i++) {
System.out.print("_ ");
}
System.out.println();
System.out.print("Guess a letter: ");
}
}
- 05-06-2008, 02:28 AM #6
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
i have done till here.....how to write the code to put letter in that underscores and all that ..after this i am not able to write the code ....could you please help me....
- 05-06-2008, 02:31 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
Do you want a user to pick the desire word or is the desire word already programmed into the code?
- 05-06-2008, 02:34 AM #8
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
yes i want user to pick the desire word
i just took that word 'Cat' as an example to see how it works...
- 05-06-2008, 02:38 AM #9
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
Where are you having a problem?
This "program" requires a few if-statements and a loop.
Really try to understand this, and tell us where are you stuck.
- 05-06-2008, 02:49 AM #10
heres the steps I would take.
step 1: make a file with words in it. Then put them in a Vector. Then randomly choose one of those words.
step 2: I personally would have a GUI with a letter bank or something along those lines so I wouldn't have to worry about users inputing numbers or symbols.
step 3: Just check if the letter that the user inputs is in the word.
step 4: add a new game option.My IP address is 127.0.0.1
- 05-06-2008, 03:12 AM #11
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
i have to guess a letter if its correct it should go into that position in the word otherwise it goes to misses...how to put that letter in that position only for eample the word CAT i guessed a letter 'a' how to write the code that letter goes to that 2nd position...
- 05-06-2008, 03:14 AM #12
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
i have a file with words
- 05-06-2008, 03:25 AM #13
The easiest way to do this is with a simple GUI that has a vector of JLabels or what ever and each label represents the same spot in your word. then just have it update the labels to whatever the user inputs. By the way if you want to be a serious programmer is to down a uml designer I recommend the piece of shit that is called visual paradigm. It is very good, if you don't mind it being slow as balls.
My IP address is 127.0.0.1
- 05-06-2008, 08:43 PM #14
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
import java.util.*;
public class Hangman {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Let's play Hangman!");
int gameCount;
char response = 'y';
while (response=='y') {
System.out.println();
gameCount = playGame(console);
System.out.print("Do you want to play again? ");
String playAgain = console.next();
playAgain=playAgain.toLowerCase();
response = playAgain.charAt(0);
if (response=='n') {
System.out.println("You played " + gameCount + " game(s). Goodbye!");
break;
}
}
}
public static int playGame(Scanner console) {
String answer = HangmanUtils.getWord();
int gameCount = 0;
answer = answer.toUpperCase();
String word = "";
for (int i = 0; i < answer.length(); i++) {
word += "_ ";
}
String answerword = getAnswerWithSpaces(answer);
String misses="";
System.out.println("Word: " + word);
System.out.println("Misses: " + misses);
while (misses.length() < 6) {
System.out.print("Guess a letter: ");
String letter = console.next();
boolean missesAlreadyContains = contains(misses, letter.toUpperCase());
boolean letterAlreadyContains = contains(word, letter.toUpperCase());
while (missesAlreadyContains == true || letterAlreadyContains == true) {
System.out.print("Already guessed! Guess a letter: ");
letter = console.next();
missesAlreadyContains = contains(misses, letter.toUpperCase());
letterAlreadyContains = contains(word, letter.toUpperCase());
}
boolean letterContains = contains(answer, letter.toUpperCase());
if (letterContains == false) {
misses +=letter.toUpperCase();
}
word = getWord(letter.toUpperCase(), answerword, word);
if (answerword.equals(word.substring(0, word.length()-1))) {
System.out.println("The answer was: " + answer);
System.out.println("You WIN!");
break;
} else {
System.out.println();
System.out.println("Word: " + word);
if (misses.length() > 1) {
printMisses(misses);
if (misses.length()==6) {
System.out.println();
System.out.println("The answer was: " + answer);
System.out.println("You LOSE!");
break;
}
} else {
System.out.println("Misses = " + misses);
}
}
}
gameCount++;
return gameCount;
}
public static boolean contains(String answer, String letter) {
for(int i=0; i< answer.length(); i++) {
if (answer.charAt(i)==letter.charAt(0)) {
return true;
}
}
return false;
}
public static String getWord(String letter, String answer, String word) {
for(int i = 0; i < answer.length(); i++) {
if(answer.charAt(i) == letter.charAt(0)) {
word = word.substring(0, i) + letter + word.substring(i + 1, word.length());
}
}
return word;
}
public static void printMisses(String misses) {
System.out.print("Misses = " + misses.charAt(0));
for(int i =1; i < misses.length(); i++) {
System.out.print(", " + misses.charAt(i));
}
System.out.println();
}
public static String getAnswerWithSpaces(String answer) {
String answerword = "";
answerword += answer.charAt(0);
for (int i = 1; i < answer.length(); i++) {
answerword += " " + answer.charAt(i);
}
return answerword;
}
}
- 05-06-2008, 08:45 PM #15
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
irrespective of the games i played its showing the game count only 1(you played 1 game(s). Goodbye!).how to change that game count
- 05-07-2008, 05:47 PM #16
Member
- Join Date
- May 2008
- Posts
- 22
- Rep Power
- 0
Move this to the top of the class, out of all methods, and add an access modifier (private/protected/public) if you like:
(The absence of all access modifiers means 'package' access.)Java Code:int gameCount = 0;
EDIT: Also, change these:Java Code:gameCount = playGame(console);
to these:Java Code:public static int playGame(Scanner console) {Java Code:playGame(console);
and remove this:Java Code:public static void playGame(Scanner console) {Java Code:int gameCount;
Last edited by Jesdisciple; 05-07-2008 at 05:53 PM.
Similar Threads
-
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
cannot run the program
By amiey in forum New To JavaReplies: 1Last Post: 11-20-2007, 04:13 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
Why does this program not end?
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks