2015-08-04 6 views
1

Я делаю игру Hangman, в которой первый пользователь вводит слово, которое нужно угадать, а второй пользователь, ну, пытается угадать его.Скрытие выходного текста в Java

В принципе, я хочу знать, есть ли способ скрыть, где первый пользователь вводил слово, чтобы второй пользователь не мог прокручивать вверх и видеть его. Вот мой исходный код ниже.

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 
} 

Кроме того, здесь приведен пример того, какой результат может быть линейным.

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!: 

Спасибо всем!

+0

Хранить в памяти не отображаются или печать в любом месте. что не так с этим подходом? – JBaba

+0

Это не относится непосредственно к вопросу, но вы можете обойти эту проблему с помощью 'JOptionPane.showInputDialog (...)', чтобы получить вход первого пользователя – Breeze

+0

возможный дубликат [Скрыть ввод в командной строке] (http: // stackoverflow.com/questions/10819469/hide-input-on-command-line) – Bibz

ответ

0

Не существует встроенного способа очистки консоли в java. К счастью, существуют различные способы обхода, как описано here.

gl!

0

Способ выполнения в соответствии с просьбой:

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

На удивление это тот же самый код, что и в верхнем ответе http://stackoverflow.com/questions/2979383/java-clear-the-console – Breeze

+0

@ dave да, это так. Есть ли проблема, которую я копировал/вставлял здесь? – Bonatti

+0

Насколько я знаю, мы должны скорее поставить этот вопрос как дубликат, чем скопировать ответ – Breeze

Смежные вопросы