Reputation: 91
I"m currently working a little self project to create a hangman game. I want it so the computer prints the missing letters you have to find as underscores and the letters you have already found in the place where the hidden word would normally be. I'm stuck on the part where I want the computer to replace the underscores with letters every time the user inputs the correct char
.
import java.util.Arrays;
import java.util.Scanner;
public class HangmanGame {
static String hiddenWord = "marcus";
static int triesLeft = 6;
static boolean done = false;
static int length = hiddenWord.length();
static Scanner input = new Scanner(System.in);
final static int maxLength = 20;
static char[] letters = new char[20];
static char[] repeatChecker = new char[20]; //this is to help prevent the user from putting the same char multiple times
static char[] test = new char[20];
static char[] fillWord = new char[length];
static int var;
static int var2;
static char underscore = '_';
public static boolean contains(char[] arr, char i) {
for (char n : arr) {
if (i == n) {
return true;
}
}
return false;
}
public static void stringToArray(String s){
char[] stringArray = s.toCharArray();
for (char output : stringArray){
var2++;
test[var2] = output;
//System.out.println(test);
}
}
public static void createSpaces(int n){
for (int i = 0; i <= n-1; i++){
fillWord[i] = underscore;
}
System.out.println(fillWord);
}
public static void arrayLetters(){
System.out.println("Please enter a letter to guess: ");
char charInput = input.next().charAt(0);
for (int i = 0; i < length; i++){
char wordLetter = hiddenWord.charAt(i);
letters[i] = wordLetter;
if (contains(letters, charInput)){
if (contains(repeatChecker, charInput)){
System.out.println("You already did that word, try again!");
arrayLetters();
}
repeatChecker[var] = charInput;
var++;
fillWord[i] = fillWord[i].replace(charInput);
System.out.println("Nice! There is a " + charInput + " in this word!");
System.out.println(repeatChecker);
System.out.println("You have " + triesLeft + " tries left!\n");
arrayLetters();
}
if (i == length-1){
System.out.println("There is no " + charInput + " in this word!");
triesLeft--;
System.out.println("You have " + triesLeft + " tries left!\n");
if (triesLeft <= 0){
System.out.println("You failed!");
System.exit(0);
}
arrayLetters();
}
}
}
public static void main(String[] args) {
System.out.println("Welcome to my hangman game!");
stringToArray(hiddenWord);
//System.out.println(length);
createSpaces(length);
arrayLetters();
}
}
How can I use the replace()
, or another function, correctly to make it so then my code can replace the underscores with a character every time the user inputs a correct char
that the hiddenWord
contains.
Upvotes: 0
Views: 109
Reputation: 366
You have to write
fillWord[i] = charInput;
And two lines later
System.out.println(fillWord);
You printed your repeatChecker
, not the fillWord
.
Upvotes: 1
Reputation: 545
You can directly do:
fillWord[i] = charInput;
Arrays don't require replace methods anyway.
Upvotes: 0