2

I am making a Hangman game, in which the first user inputs a word to be guessed, and the second user, well, tries to guess it.

Basically I want to know if there is a way to hide where the first user inputted the word, so that the second user can't scroll up and see it. Here is my source code below.

package hangman;
public class Hangman 
{
public static void main(String[] args) 
{
    System.out.println("This game operates only in lowercase!");
    GameBoardClass myGame = new GameBoardClass();
    myGame.askForWord();
    while(myGame.gameActive())
    {
        myGame.guessLetter();
        myGame.checkForWinner();
    }
}
}


package hangman;
import java.util.*;
public class GameBoardClass
{
    private char[] GameBoard;
    private char[] CorrectWord;
    private boolean gameOnGoing = true;
    String mysteryWord;

    public boolean gameActive()
    {
        return gameOnGoing;
    }//checks if game should continue

    public void askForWord()
    {
        System.out.print("Please enter the word to be guessed by the opposing Player!: ");
        Scanner input = new Scanner(System.in);
        mysteryWord = input.nextLine();
        int i = mysteryWord.length();
        GameBoard = new char[i];
            for(int j = 0; j < i; j++)
            {
                char Blank = '_';
                GameBoard[j] = Blank;
                System.out.print(GameBoard[j] + " ");
            }
        CorrectWord = new char[i];
            for(int j = 0; j < i; j++)
            {
                char Blank1 = mysteryWord.charAt(j);
                CorrectWord[j] = Blank1;
            }
    }//end of Asking User for word and printing out blank spaces

    int Guesses = 7;
    public void guessLetter()
    {
        if(gameOnGoing = true)
        {

         int lol = 0;
         System.out.print("Guess a letter for this word!: ");
         Scanner input1 = new Scanner(System.in);
         String chickenNugget = input1.nextLine();
         char guessedLetter = chickenNugget.charAt(0);
            for(int i = 0; i < mysteryWord.length(); i++)
            {
                if(CorrectWord[i] == guessedLetter)
                {
                    GameBoard[i] = guessedLetter;
                    lol = lol + 1;
                }
            }
            if(lol == 0)
            {
                System.out.println("That was an incorrect guess!");
                Guesses = Guesses - 1;
                System.out.println("You have " + Guesses + " remaining.");
            }

            for(int i = 0; i < mysteryWord.length(); i++)
            {
                System.out.print(GameBoard[i] + " ");
            }
        }
    }//ends method asking the user to guess a letter

    public void checkForWinner()
    {
        String checkForWinnerString = "";
        String checkForWinnerString2 = "";
        for(int i = 0; i < mysteryWord.length(); i++)
        {
            checkForWinnerString += GameBoard[i];
            checkForWinnerString2 += CorrectWord[i];
        }
            if(checkForWinnerString.equals(checkForWinnerString2))
            {
                System.out.print("You've won the game!");
                gameOnGoing = false;
            }
        if(Guesses == 0)
        {
            System.out.print("You've lost the game! The word was " + mysteryWord + "\n");
            gameOnGoing = false;
        }
    }//end of checking for winner
}

In addition, here is an example of what the output might be line-by-line.

This game operates only in lowercase!
Please enter the word to be guessed by the opposing Player!: Random
_ _ _ _ _ _ Guess a letter for this word!: a
_ a _ _ _ _ Guess a letter for this word!: n
_ a n _ _ _ Guess a letter for this word!:

Thanks everyone!

Breeze
  • 2,010
  • 2
  • 32
  • 43
Shambaree
  • 35
  • 4
  • Keep in memory do not display or print anywhere. what is wrong with this approach ? – JBaba Aug 04 '15 at 17:51
  • It's not directly addressing the question, but you could get around this problem with `JOptionPane.showInputDialog(...)` to get the input of the first user – Breeze Aug 04 '15 at 17:59
  • possible duplicate of [Hide input on command line](http://stackoverflow.com/questions/10819469/hide-input-on-command-line) – Bibz Aug 04 '15 at 18:42
  • Have a look at a [readPassword](http://stackoverflow.com/questions/10819469/hide-input-on-command-line) example, it might help you. – Bibz Aug 04 '15 at 18:43

2 Answers2

1

There is no inherent way to clear the console in java. Fortunately, there are various ways to work-around it as described here.

gl!

Community
  • 1
  • 1
sharmaap
  • 66
  • 6
0

A way to perform as requested:

public final static void clearConsole()
{
try
{
    final String os = System.getProperty("os.name");

    if (os.contains("Windows"))
    {
        Runtime.getRuntime().exec("cls");
    }
    else // basically, linux
    {
        Runtime.getRuntime().exec("clear");
    }
}
catch (final Exception e)
{
    //  Handle any exceptions.
}
}
Bonatti
  • 2,778
  • 5
  • 23
  • 42
  • surprisingly this is the exact same code as in the top answer of http://stackoverflow.com/questions/2979383/java-clear-the-console – Breeze Aug 04 '15 at 18:59
  • @dave yes it is. Is there a problem I have copy/pasted it here? – Bonatti Aug 04 '15 at 19:03
  • as far as i know we should rather flag the question as duplicate than copy the answer – Breeze Aug 04 '15 at 19:05
  • @Dave my mistake then, but the flag button only states: "For serious problems or moderator attention" is that the flag? or is there some other way? because as my user gains "reputation" my buttons change – Bonatti Aug 04 '15 at 19:08
  • If we have the same options (I assume that) and you didn't already flag it as duplicate, the second last option in the flag menu should be _a duplicate..._ *This question has been asked before and already has an answer.* – Breeze Aug 04 '15 at 19:13