java - help with hangman sorting -
i need sort my letters guessed alphabetically. know need use array.sort cant figure out how. need make program asks if want play again after game on , i've tried make work cant seem right. please help. thank you
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 used letter " + ch ); bool = false; } if( bool ) { if( secretword.guess( ch ) ) { system.out.println( "you have found letter " + ch ); } else { wrongguesscount--; } if( wrongguesscount < 1 ) lose(); if( secretword.found() ) win(); } } } public void win() { system.out.println( "congratulations " + player + ", win!" ); system.exit( 0 ); } public void lose() { system.out.println( "sorry " + player + ", 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()); (int = 0; < 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 hangman"); system.out.print( "player, enter 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 = 0; < counter; i++ ) { if( lbox[i] == c ) return true; } lbox[counter] = c; counter++; return false; } public void print() { system.out.print( "letterbox: " ); for( int = 0; < counter; i++ ) { system.out.print( lbox[i] ); } system.out.println( " " ); }
to resume program , play game shouldn't call in win()
or lose()
: system.exit( 0 );
instead employ loop , ask user wants do, e.g. play again
or exit
. play again
reset game's state , start new run.
to sort char array, call arrays.sort(lbox)
. however, might put unitialized front. instead might want use sortedset<character>
provides contains
method , sort characters natural order (note use of class character
not primitive here, although through auto(un)boxing should able pass primitives well, not in declaration).
Comments
Post a Comment