/*
*
* Hangman.java
* The program asks the user to choose a file that is provided.
* The program will read one line from the file and the player will guess the word.
* and then outputs the line, the word will appear using "_".
* The player will guess letters within the word or guess entire word,
* if the player guesses correctly the "_" will replaced with the letter guessed.
* But, if the player guesses incorrectly the a part of the stickman's body will be added,
* then the user will be asked to guess again. The user can also enter "!" to guess the entire word,
* if the guess correctly they win, but if they guess incorrectly they will be asked to guess again.
* Once it has finished reading the file, the program outputs the number of guesses.
*/
import java.awt.*;
import hsa.Console;
//class name
public class Hangman
{
static Console c;
public static void main (String [] args)
{
c = new Console();
c.println("Hello, and welcome to Hangman!");
PrintWriter output;
String fileName;
//ask user to choose file; file contains words for user to guess
c.println ("The categories are: cartoons.txt, animals.txt, and food.txt. Which category would you like to choose?");
fileName = c.readLine();
// R:\\HNMRY\\ICS3U10\\Assignments\\Sumbit your work for marks here\\Frank, Tracy\\+fileName
try {
/* Sets up a file reader to read the file passed on the command
line one character at a time */
FileReader input = new FileReader(args[0]);
/* Filter FileReader through a Buffered read to read a line at a
time */
BufferedReader bufRead = new BufferedReader(input);
String line; // String that holds current file line
int count = 0; // Line number of count
// Read first line
line = bufRead.readLine();
count++;
{
lineCount++;
output.println (lineCount + " " + line);
line = input.readLine();
}
// Read through file one line at time. Print line # and line
while (line != null){
c.println(count+": "+line);
line = bufRead.readLine();
count++;
}
bufRead.close();
}
catch (FileNotFoundException e)
{
c.println("File does not exist or could not be found.");
c.println("FileNotFoundException: " + e.getMessage());
}
catch (IOException e)
{
c.println("Problem reading file.");
c.println("IOException: " + e.getMessage());
}
final String GUESS_FULL_WORD = "!";
// set integer value for number of letters for the length of the secret word
// set integer value for the number of guesses the user have made. starting at zero.
int numberofletters, numberofguesses;
numberofguesses = 0;
// guessletter indicates the letter that the user is guessing
// guessword indicates the word that the user is guessing after typing "!"
// new screen indicates the change made to the screen
// screen is the game screen that contains all the "_"'s
String guessletter, guessword, newscreen;
String screen = "";
numberofletters = SECRET_WORD.length();
// for every letter there is in the secretword, add a "_" to screen.
for (int numdashes = 1; numdashes <= numberofletters; numdashes ++)
{
screen += "_";
}
//Array to check letters entered by user
final int LOW = 'A'; //smallest possible value
final int HIGH = 'Z'; //highest possible value
int[] letterCounts = new int[HIGH - LOW + 1];
String SECRET_WORD;
char[] guessletter;
int offset; //array index
// set constants for the secret word and also "!" to guess the full word
final String GUESS_FULL_WORD = "!";
//設置整數值加密詞 的長度//設置用戶所做猜測次數的整數值。從零開始。 int numberofletters,numberofguesses; numberofguesses = 0;劊子手程序已完成,但我仍然有一些問題(我不知道有關陣列我用來檢查字母)的字母數
// guessletter指示用戶猜測的字母 // guessword指示用戶在輸入「!」後猜測的單詞。 //新屏幕表示對屏幕所做的更改 //屏幕是包含所有「_」的遊戲屏幕 字符串猜字母,guessword,newscreen; String screen =「」; numberofletters = SECRET_WORD.length();
/* prompt user for a word */
c.print("Enter a letter: ");
guessletter = c.readLine();
// start guessing!
// keep the user guessing (do while loop) until user types in "!" or the game screen is all filled with letters
// add one to numberofguesses to as a counter for the guesses the user makes
// A string lu(Letters Used) is created to hol the letters the user has already guessed.
String lu;
Boolean canPlay = true;
do
{
c.println ("(TYPE (!) TO GUESS THE WHOLE MYSTERY WORD!)");
c.println (screen);
c.println ("Please guess a letter: ");
guessletter = c.readLine();
numberofguesses = numberofguesses + 1;
// change user's guessed letter to upper case.
guessletter = guessletter.toUpperCase();
// make an index for which space the guessed letter is in the secret word.
int indexof = SECRET_WORD.indexOf (guessletter);
// if the guessed letter DOES appear in the secret word
// then replace the guessed letter on the corresponding dash on the newscreen
// the dashes that is in front of the corresponding dash and behind it is not changed to the current newscreen.
// the replace the information of the old screen with the new screen.
if (indexof > -1)
{
newscreen = (screen.substring (0,indexof) + guessletter + screen.substring (indexof+1));
screen = newscreen;
}
}while (GUESS_FULL_WORD.compareTo (guessletter)!= 0 && SECRET_WORD.compareTo (screen) !=0);
// if the screen equals the secret word (the dashes are all replaced with letters)
// then indicate that the user have won.
// also include the secret word and the number of guesses the used have made.
if (screen.equals (SECRET_WORD))
{
c.println ("WOW!! YOU WON!! (:");
c.println ("The scecret word was..." +SECRET_WORD);
c.println ("You've guessed the total of " +numberofguesses+ " times");
}
// if the user enters "!" then
if (guessletter.equals (GUESS_FULL_WORD))
{
c.println (screen);
// allow the user to guess the whole word.
c.println ("What is the secret word? ");
guessword = c.readLine();
// change the user's guess into upper case
guessword = guessword.toUpperCase();
if (numberofguesses==6)
{
canPlay = false;
Lose();
}
else
/*
* Put man here
--
o |
/|\|
/\|
_____
*/
String man[] = new String[7]
man[0] = " --\n |\n |\n |\n_____\n";
man[1] = " --\n o |\n |\n |\n_____\n";
man[2] = " --\n o |\n/ |\n |\n_____\n";
man[3] = " --\n o |\n/| |\n |\n_____\n";
man[4] = " --\n o |\n/|\\|\n |\n_____\n";
man[5] = " --\n o |\n/|\\|\n/ |\n_____\n";
man[6] = " --\n o |\n/|\\|\n/ \\|\n_____\n";
char g1[] = guess.toCharArray();
char w2[] = word.toCharArray();
c.println(man[0]);
for(int x=0;x<g1.length;x++)
{
c.print(g1[x]);
}
c.println();
// inside the if-statement...
// if the user's guess (full word) match with the secret word then
// indicate that the user have won.
// also include the secret word and also the number of guesses the user have made.
if (guessword.compareTo (SECRET_WORD) ==0)
{
c.println ("WOW!! YOU'VE GUESSED IT!! (:");
c.println ("The scecret word was..." +SECRET_WORD);
c.println ("You've guessed the total of " +numberofguesses+ " times");
}
// if the user's guess (full word) does not match with the secret word then
// indicate that the user have lost.
// also include the secret word and also the number of guesses the user have made
else
{
c.println ("OOPS...! YOU LOSE!!):");
c.println ("The scecret word was..." +SECRET_WORD);
c.println ("You've guessed the total of " +numberofguesses+ " times");
}
}
**7 errors found:**
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: not a statement
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: ';' expected
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: not a statement
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: ';' expected
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: not a statement
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: ';' expected
File: H:\My Documents\Dr. Java\Hangman.java [line: 203]
Error: H:\My Documents\Dr. Java\Hangman.java:203: reached end of file while parsing
我很喜歡它在錯誤中如何表達,但他仍然需要問我們。 – Kayla 2011-06-02 23:09:37