Random list, SecretPhrase Java Project -
my school project requires me modify last assignment (code below) pull random phrase list of @ least 10 user guess. drawing blank on this. appreciated. understand have add class import text file or list, need modify loop in order randomly select?
import java.util.scanner; // allows user read different value types public class secretphrase { string phrase; // scanner scan = new scanner(system.in); secretphrase(string phrase){ this.phrase = phrase.tolowercase(); } public static void main(string args[]){ secretphrase start = new secretphrase("java great"); // phrase user have identify start.go(); // starts program } void go(){ string guess; string word=""; string[] words = new string[phrase.length()]; // array store charachters arraylist<string> lettersguessed = new arraylist(); for(int i=0;i<phrase.length();i++){ if(phrase.charat(i)== ' '){words[i] = " ";} else{words[i] = "*";} // array uses * hide actual letters } int gcount =0; // records count while(!word.equals(phrase)){ // continues loop word = ""; int lcount = 0; system.out.print("guess letter> "); guess = scan.next(); for(int i=0;i<phrase.length();i++){ // accounts attempts user use more 1 charachter @ time. if((guess.charat(0)+"").equals(phrase.charat(i)+"")&&(lettersguessed.indexof(guess.charat(0)+"")==-1)){ words[i] = ( guess.charat(0))+ ""; lcount++; } } lettersguessed.add(guess.charat(0)+""); // reveals letter in phrase system.out.println("you found " + lcount +" letters"); // prints out total number of times charachter in phrase for(int i=0;i<words.length;i++){ word=word+words[i]; } system.out.println(word); gcount ++; } system.out.println("good job! took " + gcount + " guesses!" ); // prints out result total count } }
in existing code, creating secretphrase object phrase guess:
public static void main(string args[]){ secretphrase start = new secretphrase("java great"); start.go(); // starts program }
you should replace list (either arraylist or linkedlist fine) , populate data (given either file, user input or hard-coded):
arraylist<secretphrase> phrases = new arraylist<secretphrase>(); //reading file: file file = new file("myphrases.txt"); filereader reader = new filereader(file); bufferedreader br = new bufferedreader(reader); string phrase = null; while ((phrase = br.readline()) != null) { phrases.add(new secretphrase(phrase)); }
now either use random on phrases.size() , execute go on it, or if you're looking series of phrases, can create permutation , loop on them. i'm not sure requirements here are.
Comments
Post a Comment