开发者

Some error in Hangman program

开发者 https://www.devze.com 2023-03-04 00:21 出处:网络
I have to build a hangman program for java for a class. the problem i\'m having is having the letter change once you guess a letter. all it\'s doing is getting an error when i guess. any help would be

I have to build a hangman program for java for a class. the problem i'm having is having the letter change once you guess a letter. all it's doing is getting an error when i guess. any help would be appreciated, thank you

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class test2 {
    public static void main( String[] args ) {
        HangmanSession hangmanSession = new HangmanSession();
        hangmanSession.play();
    }

}

class HangmanSession {
    private Player player;
    private Words secretWord;
    private LetterBox letterBox;
    priv开发者_运维百科ate int triesNumber = 6;

    public HangmanSession() {
    player = new Player();
    player.askName();
    secretWord = new Words();
    letterBox = new LetterBox();
}

 private void printState() {
    letterBox.print();
    System.out.print( "Hidden word : " );
    secretWord.print();
    System.out.print( "Tries left: " + triesNumber + "<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 the letter " + ch );
            bool = false;
        }
        if( bool ) {
            if( secretWord.guess( ch ) ) {
                System.out.println( "You have found the letter " + ch );
            }
            else {
                triesNumber--;
            }
            if( triesNumber < 1 )
                gameOver();

            if( secretWord.found() )
                congratulations();
        }
    }
}

public void congratulations() {
    System.out.println( "Congratulations " + player + ", you win!" );
    System.exit( 0 );
}

public void gameOver() {
    System.out.println( "Sorry " + player + ", this time you lose!" );
    System.exit( 0 );
}

 }

  class Words {
private String fv;
private StringBuffer pValue;
private int found = 0;
{

    String Words[] = new String[23];

    Words[0] = "carbon";
    Words[1] = "dictionary";
    Words[2] = "restaurant";
    Words[3] = "televison";
    Words[4] = "responsible";
    Words[5] = "technology";
    Words[6] = "computer";
    Words[7] = "communicate";
    Words[8] = "automobile";
    Words[9] = "coffee";
    Words[10] = "federation";
    Words[11] = "exaggerate";
    Words[12] = "cappuccino";
    Words[13] = "macintosh";
    Words[14] = "apple";
    Words[15] = "microsoft";
    Words[16] = "lighter";
    Words[17] = "shark";
    Words[18] = "bunker";
    Words[19] = "argument";
    Words[20] = "playstation";
    Words[21] = "parrot";
    Words[22] = "canine";

    Random random = new Random();
    int randomWord = random.nextInt(22);

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

    String[] displayLetters = new String[Words[randomWord].length()];

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

    {
        displayLetters[i] = "_";
    }
    for (int i=0; i<displayLetters.length; i++)
    {
        System.out.print(displayLetters[i]+" ");
    }
}
{

}   

public boolean found() {
    System.out.println( "Letters found:" + found + "/" + fv.length() );
    return ( found == fv.length() );
}


public boolean guess( char c ) {
    int index = fv.indexOf( c );
    if( index == -1 )
        return false;
    else {
        found = found + findOccurances( c );
        return true;
    }
}

private int findOccurances( char c ) {
    int idx = fv.indexOf( c );
    pValue.setCharAt( idx, fv.charAt( idx ) );
    int counter = 1;
    while( idx != -1 ) {
        int index = fv.indexOf( c, idx + 1 );
        idx = index;
        if( idx != -1 ) {
            counter++;
            pValue.setCharAt( idx, fv.charAt( idx ) );
        }
    }
    return counter;
}

public void print() {
    System.out.println( pValue );
}

}


class Player {
private String name = "";
public void askName() 
{System.out.print( "Player, enter your name:" );


name = receiveInput();

}

public char takeGuess() {
    return receiveInput().charAt( 0 );
}

private String receiveInput() {
    String str = " ";
    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
    try {
        str = br.readLine();
    }
    catch( IOException ex ) {
        ex.printStackTrace();
    }
    return str;
}

public String toString() {
    return name;
}

}


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( "" );



}


}

Im getting null for the hidden word: and i'm getting these errors when i try to guess a letter

Exception in thread "main" java.lang.NullPointerException at Words.guess(Hangman2.java:141) at HangmanSession.play(Hangman2.java:53) at Hangman2.main(Hangman2.java:11)


Both your fv and pValue are not initialized, change your initialization block to this

    fv = Words[randomWord]; // assign the answer to fv
    pValue = new StringBuffer(fv.length()); // init the user result

    for (int i = 0; i < displayLetters.length; i++)
    {
        displayLetters[i] = "_";
        pValue.append('_'); // Init the user result to ____
    }

And, get yourself a decent IDE like eclipse and try to learn how to set break point and catch exception in debugging.


I don't see where you are initializing private String fv;

and the NullPointerException is caused because of that in this line

int index = fv.indexOf(c);

in public boolean guess(char c) of class Words.

Besides that, there is a lot of stuff to be cleaned up

like empty blocks { } and

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

do nothing and could be removed.

0

精彩评论

暂无评论...
验证码 换一张
取 消