Reputation: 161
I want my hangman game to be able to replace the blanks " _ " with the right letters in the word.
This is how my program looks like atm:
package hängagubbe;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class hangdamang {
public static void main(String[] args) throws FileNotFoundException {
// Engine
Scanner input = new Scanner(System.in);
Scanner ord = new Scanner(new File("c:/words.txt"));
int gissningar = 0;
String blanks = "";
String guessword;
String rightLetters = "";
String guess;
// Arraylist, här sparas alla ord
ArrayList<String> arswag = new ArrayList<String>();
// while loop, tar ord från words.txt och lägger till arraylisten.
while(ord.hasNext()) {
arswag.add(ord.nextLine());
}
//Väljer ett random ord i arraylisten.
Collections.shuffle(arswag);
String pickWord = arswag.get(0);
System.out.println(pickWord);
int w=pickWord.length();
//Delar det random valda ordet till endast bokstaver till arrayen
String [] splitword = pickWord.split("");
//
for(int i = 1; i <= pickWord.length(); i++){
blanks +="-";
}
//Riktiga spelet:
System.out.println("Välkommen till mitt hänga gubbe spel.");
System.out.println("Längden på ordet du ska gissa är: " + w);
System.out.println(blanks);
do{
System.out.println("\n" + "Skriv en bokstav för att gissa ordet.");
System.out.println("Antal gissningar du gjort:" + gissningar);
gissningar++;
guess = input.next();
for(int y=1; y < splitword.length; y++) {
if (guess.equals(splitword[y])) {
System.out.print(guess );
}
else
System.out.print(" _ ");
}
} while (true);
}
}
As you can see, I made so far that you can guess letters and when the word for example test, and you guess s, it will say this:
_ _ s _
But then when you guess again and do T, it says:
t _ _ t
Can anyone help me fix this?
Upvotes: 0
Views: 962
Reputation: 5364
I had some fun playing with your code, this is what I got. I wrote some notes here and there, hope it gives you a start :)
// NOTE: java naming convntion: capital class names
// complete documentation see here: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
public class Hangdamang {
public static void main(String[] args) throws FileNotFoundException {
// Engine
// NOTE: it is c-style to declare all needed variables in top of the method
// in java you can declare them close to where you need them. Provides better readability at no costs.
Scanner input = new Scanner(System.in);
Scanner ord = new Scanner(new File("words.txt")); // for example zyzzyva.net/wordlists.shtml
int gissningar = 0;
// Arraylist, här sparas alla ord
ArrayList<String> arswag = new ArrayList<String>();
// while loop, tar ord från words.txt och lägger till arraylisten.
while(ord.hasNext()) {
arswag.add(ord.nextLine());
}
//Väljer ett random ord i arraylisten.
Collections.shuffle(arswag);
String pickWord = arswag.get(0).split(" ")[0].toLowerCase();
System.out.println(pickWord);
// hold a list of not yet guessed letters
List<Character> unknownLetters = new LinkedList<Character>(); // NOTE: you could as well use ArrayList
for(Character c = 'a'; c<='z'; c++)
unknownLetters.add(c);
//Riktiga spelet:
System.out.println("Välkommen till mitt hänga gubbe spel.");
System.out.println("Längden på ordet du ska gissa är: " + pickWord.length());
String guess="";
String showWord="";
while(!guess.equalsIgnoreCase(pickWord) && !showWord.equalsIgnoreCase(pickWord)) {
System.out.println(showWord);
System.out.println("\n" + "Skriv en bokstav för att gissa ordet.");
System.out.println("Antal gissningar du gjort: " + gissningar);
gissningar++;
guess = input.next();
if(guess.length()==1) {
unknownLetters.remove(Character.valueOf(guess.charAt(0)));
showWord = pickWord;
for(Character c : unknownLetters)
showWord=showWord.replaceAll(c.toString(), "_");
}
}
System.out.println("You won! Tries needed: "+gissningar);
}
}
Documentation of java naming convention: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
Upvotes: 1
Reputation: 45080
That's because you're not printing the previous guess in the if-else
below. You either print the current guess if its present, or an underscore(_) and thus, the previous guess is not visible.
if (guess.equals(splitword[y])) {
System.out.print(guess);
} else {
System.out.print(" _ ");
}
You need to add an additional check to print the previous guess value, if it already exists. And for that you need an additional array which holds the previous guess or the string with guess filled.
Upvotes: 0