Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,253 Java Programmers. There are 2,135 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Hangman.java

 
Reply to this topicStart new topic

Hangman.java, I need help with one last part of my program

JiYoonIt
post 9 May, 2008 - 03:33 PM
Post #1


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 1



I have to make a hangman program and I have most of it done. The only part I need is to make a method that asks if the user wants to play again. No matter how i make it it won't stick. Here's my code

CODE

//Plays a game of hangman.  I only got it to play once.

import java.util.*;
public class Hangman{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);  //Scanner utility
        System.out.println("Let's play Hangman!");
        int gameCount = 0;
        oneGame(input);

        }
    
    
// Plays one game.
    public static void oneGame(Scanner input) {
        String answer = HangmanUtils.getWord();
        String blank = "";
        for (int i = 0; i < answer.length(); i++) {
            blank += "_ ";
        }
        String newAnswer = newAnswer(answer);
        String misses="";
        System.out.println("Word: " + blank);
        System.out.println("Misses: " + misses);

//Runs guesses while missed guesses is at or below 6        
        while (misses.length() <= 6) {
            System.out.print("Guess a letter: ");
            String s = input.next();
            boolean missesGuessed = hasLetter(misses, s.toUpperCase());
            boolean letterAlreadyGuessed = hasLetter(blank, s.toUpperCase());

//Test if the current guess was already guessed and filed in missed guesses.            
            while (missesGuessed == true){
                System.out.print("Already guessed! Guess a letter: ");
                s = input.next();
                missesGuessed = hasLetter(misses, s.toUpperCase());
            }

//Test if the current guess was already guessed and filed in correct guesses.            
            while (letterAlreadyGuessed == true) {
                System.out.print("Already guessed! Guess a letter: ");
                s = input.next();
                letterAlreadyGuessed = hasLetter(blank, s.toUpperCase());
            }
            
            boolean letterGuessed = hasLetter(answer, s.toUpperCase());
            if (letterGuessed == false) {
                misses += s.toUpperCase();
            }
            
            blank = compareWord(s.toUpperCase(), newAnswer, blank);

//Stops the game if the word was correctly guessed before 6 misses
            if (newAnswer.equals(blank.substring(0, blank.length()-1))) {
                System.out.println("The answer was: " + answer);
                System.out.println("You WIN!");
                break;
            } else {
                System.out.println();
                System.out.println("Word: " + blank);
                
//If misses has a a char then program prints and at 6 program stops guess intake.                
                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);
                }
            }
        }
    }
    
//Gets correct answer and places spaces in between each char to make comparison easier.
    public static String newAnswer(String answer) {
        String newAnswer = "";
        if (answer.length() > 0){
            newAnswer += answer.charAt(0);
        }
        for (int i = 1; i < answer.length(); i++) {
            newAnswer += " " + answer.charAt(i);
        }
        return newAnswer;
    }

//Prints and keeps track of missed guesses.    
    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();
    }
    
//Tests if the letter has already been guessed.
    public static boolean hasLetter(String answer, String s) {
        for(int i=0; i< answer.length(); i++) {
            if (answer.charAt(i)==s.charAt(0)) {
                return true;
            }
        }
        return false;
    }
    
//Replaces the correctly guessed char at the correct index    
    public static String compareWord(String s, String answer, String blank) {
        for(int i = 0; i < answer.length(); i++) {
            if(answer.charAt(i) == s.charAt(0)) {
                blank = blank.substring(0, i) + s + blank.substring(i + 1, blank.length());
            }

}
return blank;
}
}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


rgfirefly24
post 9 May, 2008 - 05:17 PM
Post #2


D.I.C Head

Group Icon
Joined: 7 Apr, 2008
Posts: 135

You can encompass your code in a while loop. Basically set response = 1 and set up this type thing:

CODE


While (response != 2)
{
           //your game code
         While (response != 1 && response != 2)
         {
                System.out.println("Make a selection");
                System.out.println("============");
                System.out.println("1. Play again");
                System.out.println("2. Quit");
                System.out.println("============");
                Scanner responseinput = new Scanner(System.in);
                response = responseinput.NextInt();
          
                if (response < 1 || response > 2)
                {
                   System.out.println("Please choose either 1 or 2");
                }
             }
}
User is online!Profile CardPM
Go to the top of the page
+Quote Post

sno
post 9 May, 2008 - 05:40 PM
Post #3


New D.I.C Head

*
Joined: 4 May, 2008
Posts: 15

or set up an infinite loop and then tell the user to enter 0 at the end to quit and break the loop.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

baavgai
post 9 May, 2008 - 07:02 PM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,077

Just loop around your one play. Also, what's with all the static? You only need the main static.

Here's an example.

java

import java.util.*;

public class Hangman {
private Scanner input;

public static void main(String[] args) {
Hangman game = new Hangman();
game.play();
}

public Hangman() {
System.out.println("Let's play Hangman!");
this.input = new Scanner(System.in); //Scanner utility
}

public void play() {
int gameCount = 0;
String continuePlay = "Y";
while(continuePlay == "Y") {
gameCount++;
oneGame();
System.out.println("Play Again (Y/N)?");
continuePlay = input.next().toUpperCase();
}
}
//...
}


Hope this helps.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 9 May, 2008 - 07:49 PM
Post #5


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

Should do:

java

while(continuePlay.equals("Y")) {
instead of
while(continuePlay == "Y") {


User is offlineProfile CardPM
Go to the top of the page
+Quote Post

baavgai
post 10 May, 2008 - 02:29 AM
Post #6


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,077

QUOTE(pbl @ 9 May, 2008 - 10:49 PM) *

Should do:

java
while(continuePlay.equals("Y")) {



Yep, you're correct. Though I've never actually managed to get Java to fail on the == for a string instance, it technically could. C# is so much more civil about that kind of thing.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 10 May, 2008 - 01:15 PM
Post #7


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(baavgai @ 10 May, 2008 - 02:29 AM) *

Yep, you're correct. Though I've never actually managed to get Java to fail on the == for a string instance, it technically could.


Baavgai,

If you use the same constant in the same .java file there will be ==

So:

CODE


class AClass {

String x = "abc";

... 2 thousands lines of code

String y = "abc"

}

then

x == y
and x == "abc" and y == "abc"

because the compiler optimizes the use of constants.

even

CODE

AClass instance1, instance2;
    
    instance1.x == instance2.y;
  


But in this case scan.next() return a String build in the object scan that point to a location in memory (built from the InputStream) wich is surely not the same as the one where points your constant "Y" in Class Hangman.

But the Java compiler is brighter than you think

CODE

        String a = "xy";
        String b = "x" + "y";
        System.out.println(a == b);
        
        String c = "y";
        b = "x" + c;
        System.out.println(a == b);


prints:
true
false

This post has been edited by pbl: 10 May, 2008 - 09:00 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

baavgai
post 10 May, 2008 - 05:43 PM
Post #8


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,077

Excellent point. Another process would logically create an different instance of an identical string. Thanks. smile.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 09:22AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month