java - Stuck in Hangman Game -
i learning java programming , required make basic hangman game, no graphic interface, main , fewer methods possible.
so here thing :
i did method allows me choose between different topics, each topic 5 words or group of words on it, , randomly chooses 1 of them , assigns string variable "word", 1 i'll working with.
then there method creates string "hiddenword", replacing characters ' * ' , keeps spaces between words.
the problem dont know how input char "letter" and, replace letter in hidden word, resolving hidden word step step.
i provide code, in spanish i'll translate of key words (did noticed awesome english skills uh?)
hideword
method ( no probs here, think , maybe spacing between words should done in other way) :
public string hideword(string word) { string hiddenword =""; for(int i=0;i<word.length();i++) { if(word.charat(i)==' ') { hiddenword+=' '; } else { hiddenword+='*'; } } return hiddenword; }
i've deleted uninteresting , easy parts main
method knowingly.
main () { ahorcado objeto=new ahorcado(); //example word, since full code long string word = "hello world"; int life; string hiddenword=objeto.hideword(word); system.out.println("game started\n"); system.out.println(palabraoculta); //hello world -> ***** ***** char letter; boolean resolved=false; { system.out.println("input letter"); letter=objeto.teclado.next().charat(0); //intentionally left blank because every try did weird pyramid thing or crash for(int i=0;i<word.length();i++) { if(letter==word.charat(i)) { } } if(hiddenword.equals(word)) { resolved=true; } objeto.life--; } while(objeto.vidas>=0&&resolved==false); }
well, that's it, sorry horrible grammar , poor java, provide more parts of code if needed.
what should once have found letter in word, replace * character in index found letter in original word :
for(int i=0;i<word.length();i++) { if(letter==word.charat(i)) { char[] wordcharsarray = hiddenword.tochararray(); wordcharsarray[i] = letter; hiddenword= string.valueof(wordcharsarray); } }
i not sure there might more efficient way replace character in string should work you.
Comments
Post a Comment