Hanwei Z.

asked • 06/07/20

I am having difficulty on why my program cannot be outputted. Can you please tell me where I did wrong. My code is pasted below.

import hsa.Console;

import java.awt.*;

import java.io.*;

import java.util.*;


public class HangMan

{

static Console c;

public static void main (String []args) throws IOException

{ // Main Method.

c = new Console();

// Declaration of Variables for the main method.

String line; // Get first line in the file.

String text = ""; // Read the text in the file.

Boolean replay = true;

try

{

BufferedReader input = new BufferedReader(new FileReader("hangMan.txt"));

line = input.readLine();

while (line != null)

{

if (line.indexOf("'") == -1)

{

text += line + " , ";

}

line = input.readLine();

}

input.close();

}

catch (Exception e)

{

c.println("There was a problem reading the file");

}

}

public static void playGame()

{

// Declaration of Variables

String word = "";

String hiddenWord = ""; // The hidden word that the user try to guess.

int bodyParts = 6; // The number of body parts

String letter;

String guesses = "";

Boolean finished = false;

int lengthWord; // The length of the hidden word.

String tempHidden = "";

word = word.toLowerCase();

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

{

hiddenWord += '_';

}

while (finished == false)

{

c.println("Number of body parts" + bodyParts);

c.println("Guesses" + guesses);

c.println(hiddenWord);

c.println("Enter a letter: ");

letter = c.readLine();

for (int x = 0; x < word.length(); x++) // Loop the length of the word

{

if (letter.charAt(0) == word.charAt(x))

{

tempHidden += word.charAt(x);

}

else

{

tempHidden +=hiddenWord.charAt(x);

}

}

String splitWord[] = word.split("");

for (int z = 0; z < splitWord.length; z++)

{

if (letter.equalsIgnoreCase(splitWord[z]))

{

break;

}

else if (z == splitWord.length -1)

{

bodyParts--;

break;

}

}

hiddenWord = tempHidden;

tempHidden = "";

if (hiddenWord.equalsIgnoreCase(word))

{

finished = true;

c.println("Well done! You guessed the word!");

}

if (bodyParts == 0)

{

finished = true;

c.println("Well then! All body parts have vanished! The correct word was:" + word);

}

String split[] = guesses.split(" ");

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

{

if (letter.equalsIgnoreCase(split[i]))

{

c.println("Sorry, that letter has already been entered");

}

else if (i == split.length -1)

{

guesses += letter + " ";

}

}

}

}

public static void playAgain()

{

String playAgain;

Boolean replay = true;

c.println("Do you want to play again? (Y/N): ");

playAgain = c.readLine();

if (playAgain.equalsIgnoreCase("Y"))

{

c.println("New Game" + "\n");

}

else if (playAgain.equalsIgnoreCase("N"))

{

replay = false;

}

else

{

c.println("Inacurrate choice");

System.exit(0);

}

}

}

1 Expert Answer

By:

Patrick B. answered • 06/07/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Hanwei Z.

Yeah sure. Here is my program requirements: For the selected game, my program must provide the following features: • Player vs Computer format. • A game must consist of multiple (max 10) rounds of play. • The result of each round must be displayed to the player. • A player must be able to end the game they are playing at any time. • When a game ends the game stats of each player should be shown and the winner of game should be declared. • At the end of the game, the player must be able to start a new game or stop playing. Your program should implement: • At least one command method and one query method. • At least one array (example: tracking player/computer scores) • At least one file (example: high scores, won/lost record) • Data validation • Selection and repetition structures I have chosen to code the game hangman. Please note that I am a beginner of coding. I don't know how to use classes and private methods.
Report

06/08/20

Patrick B.

lol.... the computer already knows the word, so it will guess correctly every time....
Report

06/08/20

Patrick B.

when you say QUERY, that means there is a database involved; are you sure it is a database or just a flat text file?
Report

06/08/20

Patrick B.

I have uploaded some source code for you... The filename is Hangman java.txt found in the RESOUCES section on the TOOLKIT menu under this link... It contains THREE (3) files merged into one. Copy them into THREE separate files... UI_Utils.java, Hangman.java, Words.txt; make sure they are in the same folder... compile them and then run java HangMan at the command prompt.. ===================================== It is a rudimentary hangman game, but works as expected... email me if you have any questions after you study the code it should get you started
Report

06/08/20

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.