Reputation: 61
I have created this game of hangman for one of my Java course assignments.However there are a number of problems with my code:
import java.util.Scanner;
import java.util.Random;
public class test {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Random random = new Random();
String wordList[] = {"AARDVARK", "BANANA", "ORBITAL", "LANDSCAPE","BEACHES", "SCIENCE", "PRODUCTION"};
boolean play = true;
while(play = true){
System.out.println("You are playing game of hangman. Can you guess the word? ");
char [] selectedWord = wordList[random.nextInt(wordList.length)].toCharArray(); // java -> j,a,v,a
char [] playerGuess = new char[selectedWord.length]; // "* * * * * * * *"
for(int i=0; i<playerGuess.length; i++){ // Assign empty dashes at start "* * * * * * * *"
playerGuess[i] = '*';
}
System.out.println(selectedWord);
boolean correctGuess = true;
int numberOfGuesses = 0;
while(correctGuess = true ){
addSpace(playerGuess);
System.out.println("Enter your next choice: ");
char letterInput = input.nextLine().charAt(0);
numberOfGuesses = numberOfGuesses + 1 ;
if(letterInput == '*'){
correctGuess = true;
play = false;
} else{
for(int i=0; i<selectedWord.length; i++){
if(selectedWord[i] == letterInput){
playerGuess[i] = letterInput;
}
}
if(checkGuess(playerGuess)){
correctGuess = true;
System.out.println("You guessed the word! You guessed " + numberOfGuesses + " times.");
}
}
}
if (playerGuess.equals(selectedWord))
break;
System.out.println("Do you want to play again? (yes/no) ");
String playAgain = input.nextLine();
if(playAgain.equals("no")){
play = false;
}
}
System.out.println("Game Over!");
}
public static boolean checkGuess(char[] word){
boolean guessed = true;
for(int i=0; i < word.length; i++){
if(word[i] == '*'){
guessed = false;
}
}
return guessed;
}
public static void addSpace(char [] word){
for(int i=0; i < word.length; i++){ // Assign empty dashes at start "* * * * * * * *"
System.out.print(word[i] + " ");
}
System.out.println();
}
}
Upvotes: 0
Views: 38
Reputation: 40047
You need to change
while (play = true) {
while(correctGuess = true ){
to
while(play == true) {
while (correctGuess == true) {
or better
while(play) {
while(correctGuess) {
In general, when you do logical comparisons you are simply evaluating if the expression is true or false. So if you already have a true
or false
value, just use it as such.
Here is how you would choose a word from an array. It will start over when the last word is reached.
static int nextWord = 0;
static String wordList[] = {"AARDVARK", "BANANA", "ORBITAL", "LANDSCAPE","BEACHES", "SCIENCE", "PRODUCTION"};
public static String getWord() {
if (nextWord == wordList.length) {
nextWord = 0;
}
return wordList[nextWord++];
}
Usage
String word = getWord();
However, you would be better off learning to use instance methods and not rely on static methods too much. They have their use of course, but Java is an Object Oriented Language so it would be best to learn it.
Upvotes: 3