OK, i'm working on a hangman game in cmd prompt. I'm about halfway done, but i have hit a wall regarding the secret word, and the user guess. Now mind you, my professor has not taught the class any array manipulation methods or techniques for finding specific char in an array of strings. My problem arises when the secret word is chosen randomly by the computer, I do not know how to "cover" the secret word in asterisks or whatever char. I imagine an algorithm where i use array.length and match the user input char to individual chars in the array, problem is i have wasted a whole day and i cant come up with the algorithm for that. Please help guys, this is due in a few days. Oh, and related, i also cant figure out how to cover the letters user has guessed already. I print the alphabet horizontally and i dont know how to cover individual letters.

This is my code so far. Thanks is advance.

package hangmangame;

import java.util.Scanner;
import java.util.Random;
import java.io.*;
import java.util.*;
import java.lang.*;


public class HangManGame 
{
    public static void main(String[] args) throws IOException 
    {
        HangManStates states = new HangManStates();
        
        int state1 = 1;
        int state2 = 2;
        int state3 = 3;
        int state4 = 4;
        int state5 = 5;
        int state6 = 6;       
 
        char letter;
        
        String[] copyFrom = {"*"};
        
        String[] newSecretWord = new String[10];
        
        String[] letters = {"a", "b", "c", "d", "e", "f", "g" ,"h", "i" ,"j", "k", "l" ,"m" ,"n" ,"o" ,"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; 
        
        String[] phrase = { "alabama",	"alaska", "arizona", "arkansas",
                            "california", "colorado", "connecticut", "delaware",
                            "florida", "georgia", "hawaii", "idaho",
                            "illinois",	"indiana", "iowa", "kansas",
                            "kentucky",	"louisiana", "maine", "maryland",
                            "massachusetts", "michigan", "minnesota", "mississippi",
                            "missouri",	"montana", "nebraska", "nevada",
                            "new hampshire", "new jersey", "new mexico", "new york",
                            "north carolina", "north dakota", "ohio", "oklahoma",
                            "oregon", "pennsylvania", "rhode island", "south carolina",
                            "south dakota", "tennessee", "texas", "utah",
                            "vermont",	"virginia", "washington", "west virginia",
                            "wisconsin", "wyoming"};
        
         int pos = phrase.length;
 
        String tryAgain;      
        String answer;
        
        Scanner userInput = new Scanner(System.in);

        do{
            int numOfPhrases = phrase.length;    
            Random randomWord = new Random();
            int rand1 = randomWord.nextInt(numOfPhrases);
            String word = phrase[rand1];
            String wordCover = " ";
            //System.arraycopy(copyFrom, 0, word, 0, 7);

             do{
                System.out.print("I am thinking of a phrase.\n");
                states.initialState();
                System.out.print("The current Word is: " + word + " \n");
                System.out.print("The letters you have not guessed are: \n");
                for(int index = 0; index < letters.length; index++)
                {
                    System.out.print(letters[index] + " "); 
                }      
                System.out.print("\nEnter a letter: ");       //USER ENTERS GUESS HERE
                answer = userInput.nextLine();
                    if(!(answer.equals(word)))
                    {
                        System.out.print("Try again\n");
                            continue;
                    }
                    else if(answer.equals(word))
                    {
                        System.out.print("You got it\n");
                            break;
                    }
            }while(!(answer.equals(word)));
             System.out.print("Play again? yes/no\n");
             tryAgain = userInput.nextLine();
             if(tryAgain.equals("no"))
             {
                 System.out.print("thanks for playing\n");
             }
        }while(tryAgain.equals("yes"));      
    }
}

well, you have two Strings:
1. your original word
2. a String the same length as your original one filled with *'s.

you print the second one. whenever you guess a char, you verify with the original String if the char is in there, and if so, check the indexes of where, and at that same index, you change the char in your *'s char to that char.

well, you have two Strings:
1. your original word
2. a String the same length as your original one filled with *'s.

you print the second one. whenever you guess a char, you verify with the original String if the char is in there, and if so, check the indexes of where, and at that same index, you change the char in your *'s char to that char.

you beat me to it... :cool:

anyway to add up to that you could do these:

to create the string of asterisk you may use a loop then concatenate an asterisk to the variable and iterate until the the end of the string length

when comparing the *'s string you could use a String method indexOf()(to get the index position of the letter) with the guess word then replace the * with the same index with the guessed letter

well, you have two Strings:
1. your original word
2. a String the same length as your original one filled with *'s.

you print the second one. whenever you guess a char, you verify with the original String if the char is in there, and if so, check the indexes of where, and at that same index, you change the char in your *'s char to that char.

Awesome. So let me get this straight, I am trying to put the work in here. So im going to declare an int that has the secretWord.length number, and print an array of *'s using that int. Does that make sense? Question, while i did think of verifying the userInput char if it is in the secretWord array, how would i go about doing that. I cant think of a method or a function or algorithm fo that procedure. A little more help please, im implementing code as we speak. Thank you so much.

What is the syntax of indexof()??

How do i add the '*' to the secretWord length array???

package hangmangame;

import java.util.Scanner;
import java.util.Random;
import java.io.*;
import java.util.*;
import java.lang.*;


public class HangManGame 
{
    public static void main(String[] args) throws IOException 
    {
        HangManStates states = new HangManStates();
        
        int state1 = 1;
        int state2 = 2;
        int state3 = 3;
        int state4 = 4;
        int state5 = 5;
        int state6 = 6;       
 
        char letter;
        
        String[] newSecretWord = new String[10];
        
        String[] letters = {"a", "b", "c", "d", "e", "f", "g" ,"h", "i" ,"j", "k", "l" ,"m" ,"n" ,"o" ,"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; 
        
        String[] phrase = { "alabama",	"alaska", "arizona", "arkansas",
                            "california", "colorado", "connecticut", "delaware",
                            "florida", "georgia", "hawaii", "idaho",
                            "illinois",	"indiana", "iowa", "kansas",
                            "kentucky",	"louisiana", "maine", "maryland",
                            "massachusetts", "michigan", "minnesota", "mississippi",
                            "missouri",	"montana", "nebraska", "nevada",
                            "new hampshire", "new jersey", "new mexico", "new york",
                            "north carolina", "north dakota", "ohio", "oklahoma",
                            "oregon", "pennsylvania", "rhode island", "south carolina",
                            "south dakota", "tennessee", "texas", "utah",
                            "vermont",	"virginia", "washington", "west virginia",
                            "wisconsin", "wyoming"};
        
        int pos = phrase.length;
 
        String tryAgain;      
        String answer;
        
        Scanner userInput = new Scanner(System.in);

        do{
            int numOfPhrases = phrase.length;    
            Random randomWord = new Random();
            int rand1 = randomWord.nextInt(numOfPhrases);
            String word = phrase[rand1];
            int coverUp = word.length();        // <<------------
            String[] coverUpArray = new String[coverUp];    //<<--- HERE!!! how do i add the '*' to the int value of the secret word????
            //  HERE!! I created an array with the same length as the secret word.

             do{
                System.out.print("I am thinking of a phrase.\n");
                states.initialState();
                System.out.print("The Secret Word is: " + word + " \n");
                System.out.print("The letters you have not guessed are: \n");
                for(int index = 0; index < letters.length; index++)
                {
                    System.out.print(letters[index] + " "); 
                }      
                System.out.print("\nEnter a letter: ");       //USER ENTERS GUESS HERE
                answer = userInput.nextLine();
                    if(!(answer.equals(word)))
                    {
                        System.out.print("Try again\n");
                            continue;
                    }
                    else if(answer.equals(word))
                    {
                        System.out.print("You got it\n");
                            break;
                    }
            }while(!(answer.equals(word)));
             System.out.print("Play again? yes/no\n");
             tryAgain = userInput.nextLine();
             if(tryAgain.equals("no"))
             {
                 System.out.print("thanks for playing\n");
             }
        }while(tryAgain.equals("yes"));      
    }
}

I got the first part thank you guys im working on checking userInput char with array secretWord. Any hints on possible algorithms would be appreciated. Thank you guys. You're awesome.

Just thought up on an alternative(better in my opinion) solution in checking if a letter input exists.
Loop the string index and compare each character using charAt() method then change the index of the secret word with the letter input, you could use the StringBuilder class setCharAt() method to change the specified index with the letter

If your not familiar on using the StringBuilder class you can check an example in the oracle docs:
http://docs.oracle.com/javase/tutorial/java/data/buffers.html

Thanks boss. Greatly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.