Hangman程序中的一些错误

时间:2011-05-09 01:27:55

标签: java

我必须为一个类构建一个java的hangman程序。一旦你猜了一封信,我所遇到的问题就是改变了这封信。所有它正在做的是我猜的时候会出错。任何帮助将不胜感激,谢谢

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



}


}

我为隐藏的单词获取null:当我尝试猜一个字母时,我收到这些错误

线程“main”java.lang.NullPointerException中的异常     在Words.guess(Hangman2.java:141)     在HangmanSession.play(Hangman2.java:53)     在Hangman2.main(Hangman2.java:11)​​

2 个答案:

答案 0 :(得分:1)

您的fv和pValue都未初始化,请将初始化块更改为

    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 ____
    }

并且,让自己像Eclipse一样体面的IDE,并尝试学习如何设置断点并在调试中捕获异常。

答案 1 :(得分:0)

我看不到您在初始化private String fv;

的位置

并且由于此行

中的原因导致NullPointerException
int index = fv.indexOf(c);
<{1>} public boolean guess(char c)中的

除此之外,还有很多东西需要清理

像空块class Words

一样
{ }

什么都不做,可以删除。