Advertisement
Guest User

TripleX

a guest
Jan 17th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. import java.util.Scanner; // Import the Scanner class
  2. import java.util.concurrent.ThreadLocalRandom; // needed Random Class part
  3.  
  4. public class TripleX {
  5.  
  6.     static void printIntroduction(int Difficulty) {
  7.         System.out.println("\nDu bist ein Tresorknacker und du raubst gerade einen Tresor mit dem Level " + Difficulty
  8.                 + " aus...");
  9.         System.out.println("Gib den korrekten Tresorcode (mit Leerzeichen getrennt) ein um fortzufahren...\n");
  10.     }
  11.  
  12.     static void sort(int arr[]) {
  13.         for (int i : arr) {
  14.             for (int j = i; j < arr.length; j++) {
  15.                 if (arr[i] < arr[j]) {
  16.                     int buffer = j;
  17.                     arr[i] = arr[j];
  18.                     arr[j] = buffer;
  19.                 }
  20.             }
  21.         }
  22.     }
  23.  
  24.     static boolean playGame(int Difficulty, Scanner myScanner) {
  25.  
  26.         printIntroduction(Difficulty);
  27.  
  28.         // nextInt is normally exclusive of the top value,
  29.         // so add 1 to make it inclusive
  30.         // Generate Code
  31.         int CodeA = ThreadLocalRandom.current().nextInt(0, Difficulty * 2) + 1;
  32.         int CodeB = ThreadLocalRandom.current().nextInt(0, Difficulty * 2) + 1;
  33.         int CodeC = ThreadLocalRandom.current().nextInt(0, Difficulty * 2) + 1;
  34.  
  35.         int arr[] = { CodeA, CodeB, CodeC };
  36.         sort(arr);
  37.  
  38.         final int CodeSum = CodeA + CodeB + CodeC;
  39.         final int CodeProduct = CodeA * CodeB * CodeC;
  40.  
  41.         // Print CodeSum and CodeProduct to the terminal
  42.         System.out.println("+ Der Code ist 3 Zahlen lang");
  43.         System.out.println("+ Der Code ergibt addiert: " + CodeSum);
  44.         System.out.println("+ Multipliziert ergibt der Code: " + CodeProduct + "\n");
  45.         // DEBUG - KEEP AS COMMENT WHEN PLAYING
  46.         System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
  47.  
  48.         // Store player guess
  49.         int GuessA = myScanner.nextInt();
  50.         int GuessB = myScanner.nextInt();
  51.         int GuessC = myScanner.nextInt();
  52.  
  53.         int GuessSum = GuessA + GuessB + GuessC;
  54.         int GuessProduct = GuessA * GuessB * GuessC;
  55.  
  56.         // Check if the players guess is correct
  57.         if (GuessSum == CodeSum && GuessProduct == CodeProduct) {
  58.             if (Difficulty == 5) {
  59.                 return true;
  60.             }
  61.  
  62.             System.out.println("\n\n*** Sehr gut! Du hast den Tresor geknackt! Mach weiter! ***");
  63.             return true;
  64.         } else {
  65.             System.out.println("*** Du hast den falschen Code eingegeben! Sei vorsichtig! Versuch es nochmal! ***");
  66.             return false;
  67.         }
  68.     }
  69.  
  70.     public static void main(String[] args) {
  71.  
  72.         Scanner myScanner = new Scanner(System.in); // Create a Scanner object
  73.  
  74.         int LevelDifficulty = 1;
  75.         final int MaxDifficulty = Integer.valueOf(args[0]);
  76.  
  77.         while (LevelDifficulty <= MaxDifficulty) // Loop game until all levels completed
  78.         {
  79.             boolean bLevelComplete = playGame(LevelDifficulty, myScanner);
  80.  
  81.             if (bLevelComplete) {
  82.                 ++LevelDifficulty;
  83.             }
  84.         }
  85.         System.out.println(
  86.                 "\n*** Grossartige Arbeit! Du hast alle Tresore geknackt! Du bist der beste Einbrecher aller Zeiten ***\n");
  87.         System.out.println("\n Druecke Enter zum Beenden des Spiels...");
  88.  
  89.         myScanner.close(); // Closing the Scanner
  90.         return;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement