Untitled
unknown
plain_text
2 years ago
3.4 kB
2
Indexable
Never
package labs; import java.io.File; import java.util.Arrays; import java.util.Scanner; public class PerformanceTask { public static String[] words; public static void main(String[] args) throws Exception { Scanner inKey = new Scanner(System.in); int misses = 0; String word = ""; int computer = 0; boolean start = true; String wordGame = ""; //array of possible words String[] secretWord = { "secret", "abruptly", "lengths", "subway", "fuchsia", "luxury", "frazzled", "frizzled", "fuchsia", "subway", "swivel", "syndrome", "thriftless", "affix", "matrix" }; boolean begin = true; //the start of code while (begin) { int playAgain = 0; int badGuesses = 7; String guesses = ""; //the beginning of the code while (start) { System.out.println("Time to play HANGMAN!!\n" + "Can you save him?!?"); //computer generates the word computer = (int) (Math.random() * 15 + 1); wordGame = secretWord[computer]; //enters blanks for how long the word is String[] blanks = new String[wordGame.length()]; for (int i = 0; i < blanks.length; i++) { blanks[i] = "_"; } System.out.println(""); start = false; String incorrect = ""; String letters2 = ""; //the entering part where you enter your answers while (badGuesses > 0) { //shows everything you need to know to play the game System.out.println("Bad Guesses: " + incorrect); System.out.println("Guesses Left: " + badGuesses); System.out.println(Arrays.toString(blanks)); System.out.println("Choose a letter: "); String letters = inKey.next().toLowerCase(); boolean correct = false; boolean sameLetter = false; //checks if letter is already guessed for (int o = 0; o < guesses.length(); o++) { if (guesses.substring(o, o + 1).equals(letters)) { sameLetter = true; } } if (sameLetter) { System.out.println("You already guessed this"); continue; } guesses += letters; //if letter isn't already guessed find if the letters are in the secret word for (int i = 0; i < wordGame.length(); i++) { if (wordGame.substring(i, i + 1).equals(letters)) { blanks[i] = letters; System.out.println("Good guess!"); correct = true; letters2 += letters; } } //if letter is wrong removes and guess and says word is wrong. Also in "Bad Guesses:" shows that you entered wrong word if (!correct) { System.out.println("Nope..."); incorrect += letters; badGuesses = badGuesses - 1; } //you won! int count = 0; for (int i = 0; i < blanks.length; i++) { if (blanks[i].equals("_")) { count++; } } if (count == 0) { System.out.println("Congratulations!! The word is: " + wordGame); badGuesses = 0; } //if all guesses are over and you didn't get the word... you lost else if(badGuesses==0) { System.out.println("Game over mr stickman died The word is: " + wordGame); } } } //wanna play again? if (badGuesses == 0) { System.out.println("Would you like to play again? Enter 1 for yes and 2 for no"); playAgain = inKey.nextInt(); if (playAgain == 1) { badGuesses = 7; start = true; } } } } }