Advertisement
Guest User

stupidshitfuckyouxd

a guest
Oct 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package Zusatzaufgaben;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Zusatzaufgabe1_Hangman {
  6.  
  7.     public static String copyToWord(char[] copy) {
  8.         StringBuilder builder = new StringBuilder();
  9.         for (int i = 0; i < copy.length; i++) {
  10.             builder.append(copy[i]);
  11.         }
  12.         return builder.toString();
  13.     }
  14.     public static void main(String[] args) {
  15.         Scanner scanner = new Scanner(System.in);
  16.         final String[] secretWords = {"Giraffe", "Neck", "Password", "Rating", "Food", "Übung", "Gitlab"};
  17.         final String secretWord = secretWords[(int) (Math.random() * secretWords.length)].toUpperCase();
  18.         final char[] copy = secretWord.toCharArray();
  19.         for (int i = 0; i < copy.length; i++) {
  20.             copy[i] = '_';
  21.         }
  22.         char currentChar;
  23.         int currentCharNumber = 0;
  24.         int tries = 0;
  25.         int maximumtries = 10;
  26.         System.out.println("Welcome to Hangman! To progress in the game you need to enter letters. Your word will look something like this:\n");
  27.  
  28.         while (!copyToWord(copy).equals(secretWord) && tries < maximumtries) {
  29.  
  30.             for(int i = 0; i < copy.length; i++) {
  31.                 System.out.print(copy[i]+ " ");
  32.             }
  33.  
  34.             System.out.println("\nEnter your next character! You have " + (maximumtries-tries) + " tries left!");
  35.             currentChar = scanner.next().charAt(0);
  36.  
  37.             if(Character.toUpperCase(currentChar) == secretWord.charAt(currentCharNumber)) {
  38.  
  39.                 System.out.println("Hooray! Thats the correct character. Good job!\n");
  40.                 copy[currentCharNumber] = currentChar;
  41.                 currentCharNumber++;
  42.  
  43.             } else {
  44.  
  45.                 if(tries+1 < maximumtries)
  46.                     System.out.println("You entered the wrong character. Try again!\n");
  47.                 tries++;
  48.                 continue;
  49.  
  50.             }
  51.  
  52.             tries++;
  53.         }
  54.         if(copyToWord(copy).equals(secretWord))
  55.             System.out.println("Thanks for playing! You beat the game in " + tries + " tries.");
  56.         else
  57.             System.out.println("You didnt beat the game. Better luck next time.");
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement