aspose file tools*
The moose likes Beginning Java and the fly likes Hangman problem! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hangman problem!" Watch "Hangman problem!" New topic
Author

Hangman problem!

Epco Elstak
Greenhorn

Joined: Oct 24, 2008
Posts: 1
Hey folks,
I have a serious question here. I made a hangman game see below:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Hangman
{

public static void main(String args[])
{
Hangman epco = new Hangman();
epco.startSpel(args[0]);
}

private int aantalBeurten = 7;
private int aantalFouten = 0;
private StringBuffer teRadenWoord;
private StringBuffer raadWoord;
private boolean nogNietGewonnen = true;
private int aantalGoed = 0;

private char readChar(){

char c = ' '; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try
{
String s = reader.readLine();
c = s.charAt(0);
}
catch (Exception e) {}
return c;

}

public boolean checkWoord(StringBuffer attempt, StringBuffer endResult)
{
boolean notFound = false;
for (int i = 0; i < endResult.length(); i++)
{
if (attempt.charAt(i) != endResult.charAt(i))
{
notFound = true;
}
}
return notFound;
}

public void maakHetRaadWoord(){

raadWoord = new StringBuffer("");
for (int i = 0; i < teRadenWoord.length(); i++)
{
raadWoord.append(".");
}
}
public void startSpel(String woord)
{
teRadenWoord = new StringBuffer(woord);
System.out.println("Help mij! je hebt 8 kansen om te zorgen dat ik niet word opgehangen!");
maakHetRaadWoord();

while (nogNietGewonnen == true && aantalBeurten >= aantalFouten)
{
aantalGoed = 0;
System.out.println("Vul een letter in, zorg dat het een goede is! ");
System.out.println("het woord tot nu toe --> " + raadWoord.toString());
char letter = Character.toLowerCase(readChar());
if (Character.isLetter(letter))
{

System.out.println("Ingevulde letter: " + letter);
for (int i = 0; i < teRadenWoord.length(); i++)
{
if(teRadenWoord.charAt(i) == letter)
{
raadWoord.setCharAt(i, letter);
aantalGoed++;
}

}
nogNietGewonnen = checkWoord(raadWoord, teRadenWoord);

if (aantalGoed == 0)
{
aantalFouten++;
System.out.println("Dit is onjuist, ik hang bijna!");
System.out.println("beurt" +aantalFouten);
}
}
else
{
System.out.println("Dit is geen Letter. Vul een letters in om het woord te raden!");
}
}

if (nogNietGewonnen == false)
{
System.out.println("Je hebt me gered, ik sta bij je in het krijt!");
System.out.println("Het woord is " + teRadenWoord + ".");
}
if (aantalFouten == 8)
{
System.out.println("Je hebt me laten ophangen, ik rekende op je");
System.out.println("Het juiste woord was " + teRadenWoord + ".");
}
}

It's almost done, though I have to do 2 more things, and I can't figure out how to do it.
First I have to add a code that checks if the user used a letter twice, even it's upper case or not (example T/t). And if so the programme needs to say that the user already used that letter.

Second thing is, I have to delete the "char letter = Character.toLowerCase(readChar());" part, and make a stringbuffer of it and use equalsIgnoreCase instead.

I would appreciate any help, I need to post it in 2 days.
thanks in advance!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Epco Elstak:
...First I have to add a code that checks if the user used a letter twice, even it's upper case or not (example T/t). And if so the programme needs to say that the user already used that letter.

Second thing is, I have to delete the "char letter = Character.toLowerCase(readChar());" part, and make a stringbuffer of it and use equalsIgnoreCase instead...

Welcome to JavaRanch!

We're here to help, but we need you to show us exactly where you're stuck. So on the first issue, what ideas do you have so far? On the second issue, what have you tried?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Originally posted by marc weber:

Welcome to JavaRanch!
Agree.

Please use the code button and maintain the usual indentation; that makes your posts much easier to read.
 
 
subject: Hangman problem!