patternjavaMinor
Basic word guessing game
Viewed 0 times
gamewordbasicguessing
Problem
After doing a simple coding challenge regarding strings, I realized that I have forgotten a few things about them. As such, I decided to do a few basic string manipulation practices for myself.
One of them, which I found in my old Java assignments, was a word guessing game. Very small, nothing special. As such, I decided to re-implement it. The game works as follows: You enter the "secret" word, and then you have 10 tries to guess it.
One of them, which I found in my old Java assignments, was a word guessing game. Very small, nothing special. As such, I decided to re-implement it. The game works as follows: You enter the "secret" word, and then you have 10 tries to guess it.
import java.util.Scanner;
public class guess {
public static void main(String[] args) {
int tries = 0;
boolean iterated = false;
String temp = "";
String holder = "";
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your secret word:");
String word = keyboard.nextLine();
do {
System.out.println("Enter your letter guess");
String guess = keyboard.nextLine();
for(int i = 0; i < word.length(); i ++) {
if (guess.equals(Character.toString(word.charAt(i)))) {
if(!iterated)
temp += Character.toString(word.charAt(i));
else {
holder = Character.toString(temp.charAt(i)).replace("-", guess);
temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
}
} else {
if(!iterated) {
temp += "-";
}
}
}
tries++;
iterated = true;
System.out.println(temp);
if(temp.equals(word)) {
System.out.println("You guessed correctly!");
break;
}
}while (tries < 10);
}
}Solution
Don't do too much in one loop
In this, you handle two entirely different purposes. You change the existing string to display any occurrence of the character. And on the first iteration, you fill in the string with dummy characters. This may be slightly more efficient on that first iteration, but it is less efficient on every other iteration. Consider how this code looks without the
How much harder is it to do the initial fill? StackOverflow suggests something like
Or you could make a method like they do:
Or any other variants there.
Which code would you rather read? Those two simple blocks or one large one?
You use repeated calls to
This reduces the two line solution to one even simpler line in the loop.
It also eliminates the need to test that the current index is equal to the guessed character. This reverses the logic, searching for the guess in the string.
I also added an explanatory message for why multicharacter or empty guesses don't work. The original just silently goes to the next guess in that instance.
Iterate with
Your
Summary
It would be more elegant to break this into more methods, but it will work like this.
for(int i = 0; i < word.length(); i ++) {
if (guess.equals(Character.toString(word.charAt(i)))) {
if(!iterated)
temp += Character.toString(word.charAt(i));
else {
holder = Character.toString(temp.charAt(i)).replace("-", guess);
temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
}
} else {
if(!iterated) {
temp += "-";
}
}
}In this, you handle two entirely different purposes. You change the existing string to display any occurrence of the character. And on the first iteration, you fill in the string with dummy characters. This may be slightly more efficient on that first iteration, but it is less efficient on every other iteration. Consider how this code looks without the
!iterated checks. for (int i = 0; i < word.length(); i ++) {
if (guess.equals(Character.toString(word.charAt(i)))) {
holder = Character.toString(temp.charAt(i)).replace("-", guess);
temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
}
}How much harder is it to do the initial fill? StackOverflow suggests something like
String temp;
if (word.length() > 0) {
char[] array = new char[word.length()];
Arrays.fill(array, '-');
temp = new String(array);
} else {
temp = "";
}Or you could make a method like they do:
String temp = getStringWithLengthAndFilledWithCharacter(word.length(), '-');Or any other variants there.
Which code would you rather read? Those two simple blocks or one large one?
StringBuilder to build a stringYou use repeated calls to
substring and string concatenation to build your replacement string. Behind the scenes, Java probably converts this into a StringBuilder. Why not embrace that and do it yourself? if (guess.length() != 1) {
System.out.println("Guesses must each be a single letter.");
continue;
}
char c = guess.charAt(0);
for (int i = word.indexOf(c); i >= 0; i = word.indexOf(c, i + 1)) {
temp.setCharAt(i, c);
}This reduces the two line solution to one even simpler line in the loop.
It also eliminates the need to test that the current index is equal to the guessed character. This reverses the logic, searching for the guess in the string.
I also added an explanatory message for why multicharacter or empty guesses don't work. The original just silently goes to the next guess in that instance.
Iterate with
for loopsYour
do/while loop skips the first check at the expense of a more complicated loop. If you change to a for loop, it's easier to read at the expense of one extra comparison. Summary
StringBuilder temp;
if (word.length() > 0) {
char[] array = new char[word.length()];
Arrays.fill(array, '-');
temp = new StringBuilder(new String(array));
} else {
temp = new StringBuilder();
}
for (int turn = 0; turn = 0; i = word.indexOf(c, i + 1)) {
temp.setCharAt(i, c);
}
System.out.println(temp.toString());
if (word.equals(temp.toString())) {
System.out.println("You guessed correctly!");
break;
}
}It would be more elegant to break this into more methods, but it will work like this.
Code Snippets
for(int i = 0; i < word.length(); i ++) {
if (guess.equals(Character.toString(word.charAt(i)))) {
if(!iterated)
temp += Character.toString(word.charAt(i));
else {
holder = Character.toString(temp.charAt(i)).replace("-", guess);
temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
}
} else {
if(!iterated) {
temp += "-";
}
}
}for (int i = 0; i < word.length(); i ++) {
if (guess.equals(Character.toString(word.charAt(i)))) {
holder = Character.toString(temp.charAt(i)).replace("-", guess);
temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
}
}String temp;
if (word.length() > 0) {
char[] array = new char[word.length()];
Arrays.fill(array, '-');
temp = new String(array);
} else {
temp = "";
}String temp = getStringWithLengthAndFilledWithCharacter(word.length(), '-');if (guess.length() != 1) {
System.out.println("Guesses must each be a single letter.");
continue;
}
char c = guess.charAt(0);
for (int i = word.indexOf(c); i >= 0; i = word.indexOf(c, i + 1)) {
temp.setCharAt(i, c);
}Context
StackExchange Code Review Q#151074, answer score: 3
Revisions (0)
No revisions yet.