Help! way in over my head.

Hi all, im pretty new to all this and im trying to code a hangman proggie (no gui) and ive totally hit a brick wall with it, any chance someone could give me a pointer what im doing wrong with this? t'would be much appreciated.

[b]mainclass:[/b]

public class hangMain

{

public static void main(String[] args)

{

char choice;

hangman game = new hangman();

do

{

System.out.println("Welcome to HANGMAN 2000!");

System.out.println();

System.out.println("1. Play HANGMAN 2000");

System.out.println("2. Quit.");

System.out.println("Please choose a menu option (1-2): ");

choice = EasyScanner.nextChar();

System.out.println();

switch (choice)

{

case '1': option1(game);

break;

case '2': break;

default:System.out.println("not a menu option.");

}

}

while (choice !='2');

}

public static void option1(hangman gameIn)

{

boolean result = gameIn.runGame(gameIn);

if(!result)

{

System.out.println("Congratulations, winner!");

}

else

{

System.out.println("Sadly, you lost. Better luck next time!");

}

}

}

hangman class:

public class hangman

{

char lastGuess;

char[] letters = new char[4];

char[] triedLetters = new char[5];

int startTries = 5;

int triesLeft;

int counter;

boolean win, lose;

public hangman()

{

lastGuess = ' ';

triesLeft = startTries;

counter = 0;

win = false;

lose = false;

char [] letters = {'_', '_', '_', '_'};

wordBank newWord = new wordBank();

String currentWord = (newWord.getWord());

char wordArray[] = currentWord.toCharArray();

}

public boolean runGame(hangman gameIn)

{

do

{

System.out.println(letters[0] + " " + letters[1] + " " + letters[2] + " " + letters[3]);

System.out.println();

System.out.println("Number of tries remaining: " + triesLeft);

System.out.println();

System.out.println("Please guess a letter: ");

lastGuess = EasyScanner.nextChar();

for (int x = 0; x < currentWord.length(); x++)

{

String tempGuess = String.valueOf(lastGuess);

String tempTarget = String.valueOf(wordArray[x]);

if (tempGuess.equalsIgnoreCase(tempTarget))

{

letters[x] = lastGuess;

}

else counter = counter + 1;

}

if (counter == 4)

{

triesLeft = triesLeft - 1;

}

if ((letters[0] == wordArray[0]) && (letters[1] == wordArray[1]) && (letters[2] == wordArray[2]) && (letters[3] == wordArray[3]))

{

System.out.println();

System.out.println("Well done!");

win = true;

return true;

}

if (triesLeft == 0)

{

lose = true;

return false;

}

}

while ((win == false) || (lose == false) );

}

}

[3077 byte] By [archie-da] at [2007-11-27 3:06:30]
# 1

lastly, the word class:

import java.util.*;

public class wordBank

{

String wordChosen;

Random randomWord = new Random();

public String wordBank()

{

List<String> wordTome = new ArrayList<String> ();

wordTome.add("tart");

wordTome.add("cake");

wordTome.add("Flan");

wordTome.add("loop");

wordChosen = wordTome.get(randomWord.nextInt(4));

}

public String getWord()

{

return wordChosen;

}

}

archie-da at 2007-7-12 3:52:56 > top of Java-index,Other Topics,Java Game Development...
# 2

Well... I think that you're making this much too complex. Some ideas would be...

1) Store the word as a String and break it into a char array when needed with the String command "charArray" or something like that (check javadocs)

2) have a guessing loops which accesses a 26 boolean array which tracks which lettres have been guessed already.

3) When a new guess is made, confirm that the input is a letter, then resume with checking for that letter in the char array of the String.

4) "Highlight" those letters by unblanking them from the array.

...

String word = "potato";

char charArray[] = new char[256];

word.getChars(0,word.length,charArray,0);

for ( int i = 0; i<word.length; i++ )

{

if (guessedAlready(charArray[i])) //checks if letter has been guessed

System.out.print(charArray[i]); //letter

else

System.out.print("_"); //blank

}

...

Start with those ideas, the rest should come from there... good luck>

ArikArikArika at 2007-7-12 3:52:56 > top of Java-index,Other Topics,Java Game Development...