简体   繁体   中英

Hiding output text in Java

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!

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

gl!

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.
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM