• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hangman problem!

 
Epco Elstak
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49370
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic