帮助进行刽子手排序

时间:2011-05-11 09:19:35

标签: java sorting

我需要按字母顺序排列我的字母。我知道我需要使用array.sort但我无法弄清楚如何。我还需要做到这一点,以便程序询问你是否想在比赛结束后再次玩,我试图让它工作,但我似乎无法做到正确。请帮忙。谢谢

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


public class DRMproject2 
{

    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 wrongGuessCount = 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: " + wrongGuessCount + "\nguess letter: " );
    }

    public void play() 
    {
        boolean bool = true;
        while( true ) 
        {
            bool = true;
            printState();
            char ch = player.guess();
            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 {
                    wrongGuessCount--;
                }
                if( wrongGuessCount < 1 )
                    lose();
                if( secretWord.found() )
                    win();
            }
        }
    }

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

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

}

class Words 
{
    private String fv;
    private StringBuffer pv;
    private int found = 0;
    {
        String Words[] = new String[23];

        Words[0] = "carbon";
        Words[1] = "dictionary";
        Words[2] = "restaurant";
        Words[3] = "television";
        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);
        String[] displayLetters = new String[Words[randomWord].length()];
        fv = Words[randomWord]; 
        pv = new StringBuffer(fv.length()); 
        for (int i = 0; i < displayLetters.length; i++)
        {
            displayLetters[i] = "_";
            pv.append('_'); 
        }

    } 

    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 );
    pv.setCharAt( idx, fv.charAt( idx ) );
    int counter = 1;
    while( idx != -1 ) {
        int index = fv.indexOf( c, idx + 1 );
        idx = index;
        if( idx != -1 ) {
            counter++;
            pv.setCharAt( idx, fv.charAt( idx ) );
        }
    }
    return counter;
}

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

}


class Player 
{
    private String name = " ";
    public void askName() 
    {
        System.out.println("Welcome to Hangman");
        System.out.print( "Player, enter your name: " );
        name = input();
    }

    public char guess()
    {
        return input().charAt( 0 );
    }

    private String input() 
    {
        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( "LetterBox: " );
        for( int i = 0; i < counter; i++ ) 
        {
            System.out.print( lbox[i] );
        }
        System.out.println( " " );
    }

3 个答案:

答案 0 :(得分:1)

要恢复该计划并播放其他游戏,请勿在{{1​​}}或win()中将其调用: lose()

而是使用循环并询问用户他想做什么,例如System.exit( 0 );play again。 让exit重置游戏的状态并开始新的游戏。

要对char数组进行排序,只需调用play again即可。但是,这可能会将整体化放在前面。相反,您可能希望使用已经提供Arrays.sort(lbox)方法的SortedSet<Character>并按字符顺序对字符进行排序(注意使用类contains而不是基元,尽管通过自动(un)装箱你也应该能够传递基元,而不是在声明中。)

答案 1 :(得分:0)

不确定数组排序的含义,但是使用array.sort会对数组进行排序,这个链接可能会对它有所了解。

http://www.rgagnon.com/javadetails/java-0343.html

至于再次播放,您需要更改您的win方法,询问用户是否希望再次播放检查输入,然后如果他们希望再次播放,您应该能够再次调用play()。

public void win() {
 System.out.println( "Congratulations " + player +  ", you win!" );
 ask to play again
 get input
 check input
 if want to play again
   play()
 else 
   exit    
}

答案 2 :(得分:0)

这是你的信箱impl

class LetterBox {

    private SortedSet<Byte> data = new TreeSet<Byte>();

    public int getSize() {
        return data.size();
    }

    public void add(char c) {
        data.add(Byte.valueOf((byte) c));

    }

    public boolean contains(char c) {
        return data.contains(Byte.valueOf((byte) c));
    }

    public void print() {
        System.out.print("LetterBox: ");
        for (Byte _char : data) {
            System.out.print(((char) _char.byteValue()));
        }
        System.out.println(" ");
    }


}