assignment_4.doc (24.5K)
Number of downloads: 9
My partner and I have been working together through e-mail correspondence to complete the assignment, but I have encountered an error message that I cannot seem to bypass.
When I try to compile the code, it keeps saying "reached end of file while parsing". I have tried to do as it says to correct it, but the message still remains. Does that mean there is something wrong in the code that is doing that? If so, what should I do to correct it?
import java.util.Random;
import java.util.Scanner;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Hangman4 {
public static void main( String[] args ) {
HangmanSession hangmanSession = new HangmanSession();
hangmanSession.play();
}
}
class HangmanSession {
private Player player;
private HiddenKeyword hiddenKeyword;
private LetterBox letterBox;
private int triesNumber = 7;
public HangmanSession() {
hiddenKeyword = new HiddenKeyword();
letterBox = new LetterBox();
}
private void printState() {
letterBox.print();
System.out.print( "Hidden word : " );
hiddenKeyword.print();
System.out.print( "Tries left: " + triesNumber + "\n<guess letter:>" );
}
public void play() {
boolean bool = true;
while( true ) {
bool = true;
printState();
char ch = player.takeGuess();
if( letterBox.contains( ch ) ) {
System.out.println( "Try again, you've already used that letter " + ch );
bool = false;
}
if( bool ) {
if( hiddenKeyword.guess( ch ) ) {
System.out.println( "YAY! you have found letter! " + ch );
}
else {
triesNumber--;
}
if( triesNumber < 1 )
gameOver();
if( hiddenKeyword.found() )
congratulations();
}
} //end of bool
}
public void congratulations() {
System.out.println( "Congratulations " + player + ", you win!" );
System.exit( 0 );
}
public void gameOver() {
System.out.println( "Sorry " + player + ", you lost, but you can try again!" );
System.exit( 0 );
}
}
class HiddenKeyword {
private String fValue;
private StringBuffer pValue;
private int lfoundNumber = 0;
public String words() {
Random r = new Random();
int random = r.nextInt(20)+1;
}
public boolean found() {
System.out.println( "Letters found:" + lfoundNumber + "/" + fValue.length() );
return ( lfoundNumber == fValue.length() );
}
public boolean guess( char c ) {
int index = fValue.indexOf( c );
if( index == -1 )
return false;
else {
lfoundNumber = lfoundNumber + findOccurances( c );
return true;
}
}
private int findOccurances( char c ) {
int idx = fValue.indexOf( c );
pValue.setCharAt( idx, fValue.charAt( idx ) );
int counter = 1;
while( idx != -1 ) {
int idxx = fValue.indexOf( c, idx + 1 );
idx = idxx;
if( idx != -1 ) {
counter++;
pValue.setCharAt( idx, fValue.charAt( idx ) );
}
}
return counter;
}
public void print() {
System.out.println( pValue );
}
class LetterBox {
private char[] lbox = new char[24];
private int counter = 0;
public boolean contains( char c ) {
for( int i = 0; i < counter; i++ ) {
if( lbox[i] == c )
return true;
}
lbox[counter] = c;
counter++;
return false;
}
public void print() {
System.out.print( "\nLetterBox:" );
for( int i = 0; i < counter; i++ ) {
System.out.print( lbox[i] );
}
System.out.println( "" );
}
}
public class hiddenWords {
public static void main(String[] args) {
int words = 20;String wordsString;
switch(random) {
case 1:
return "star";
case 2:
return "hooray";
case 3:
return "dog";
case 4:
return "improvise";
case 5:
return "teacher";
case 6:
return "college";
case 7:
return "seahawks";
case 8:
return "read";
case 9:
return "computer";
case 10:
return "program";
case 11:
return "holiday";
case 12:
return "film";
case 13:
return "music";
case 14:
return "learn";
case 15:
return "library";
case 16:
return "waterfall";
case 17:
return "book";
case 18:
return "semester";
case 19:
return "first";
case 20:
return "vacation";
default:
return"";
}
}
public static void main(String[] args) {
Hangman4 h = new Hangman4();
Scanner input = new Scanner (System.in);
String play = h.words();
int incorrectGuesses = 7;
String playAgain;
System.out.println("Welcome to Hangman!");
do{
System.out.println("Please enter a letter:");
for (int c = 7; incorrectGuesses > c; incorrectGuesses--) {
}
String userInput = input.next();
char Guess = userInput.charAt(0);
char[] playArray = play.toCharArray();
char [] guessplay = new char[playArray.length];
for(int k=0;k<guessplay.length;k++){
guessplay[k] = '_';
}
int counter = 0;
while( counter < play.length() ){
counter++;
System.out.print("_ ");
}
int charCount = play.length();
boolean d = false;
for(int i = 0; i < play.length(); i++) {
char ch = play.charAt(i);
if(ch == (Guess)){
System.out.println("Correct!");
d = true;
}
}
if( d == false){
System.out.println("Nope.You have" + " " +incorrectGuesses + " " + "incorrect guesses left.");
}
if(incorrectGuesses == 0){
System.out.println("Hangman. Game Over.");
Guess++;
}
}while(incorrectGuesses > 0);
}
}
//System.out.println(h.words());
if( d == false){
System.out.println("Nope.You have" + " " +incorrectGuesses + " " + "incorrect guesses left.");
}
else {
if(incorrectGuesses == 1){
System.out.println("Hangman. Game Over.");
Guess++;
}
}
System.out.println("\nDo you want to keep playing (Y/N): " );
playAgain = input.next();
while (playAgain.equalsIgnoreCase("Y"));{
System.out.println("\nGame over. Thank you for playing.\n\n" );
else(new Hangman4());
}
Random random = new Random();
for (int count = 0; count < 19; count++)
Word= random.nextInt(words.length);
String input = Word;
Scanner user_input= new Scanner(System.in);
String User_Guess;
System.out.println("Guess a letter.");
User_Guess = user.input.next();
Console.WriteLine(Char.Parse(User_Guess));
if Character.isLetter(a, b, c, d, e, f, g, h, i , j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,);
{
System.out.println("Correct!");
else {
System.out.println("Sorry");
}

New Topic/Question
Reply



MultiQuote



|