Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hangman problem!

 
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!
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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?
 
Marshal
Posts: 70260
282
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic