Advertisement
Exhabition

Back-up - Weektaak 3

Sep 18th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Squared {
  4.  
  5.     static int totalPlayer1 = 0;
  6.     static int totalPlayer2 = 0;
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         startGame();
  12.  
  13.         while (true) {
  14.             System.out.println("Wil je extra getallen? Gebruik 'N' om te stoppen, gebruikt 'J' om door te gaan");
  15.             String input = scanner.nextLine();
  16.  
  17.             if (input.equals("N".toLowerCase())) {
  18.                 changePlayer2();
  19.                 System.out.println("Player 2 kreeg: " + totalPlayer2);
  20.                 checkEndGame(totalPlayer1, totalPlayer2);
  21.                 break;
  22.             } else if (input.equals("J".toLowerCase())) {
  23.                 changePlayer1();
  24.                 System.out.println("Nieuw totaal: " + totalPlayer1);
  25.                 checkMidGame(totalPlayer1, totalPlayer2);
  26.             } else {
  27.                 System.out.println("Sorry, dat is geen geldige input");
  28.             }
  29.         }
  30.     }
  31.  
  32.     // Tried to make it so this could have a variable as the arg, didn't get it to work
  33.     public static double changePlayer1() {
  34.         // Plus 1 so it is atleast 1 and not 0
  35.         double randomInt = (int) (11 * java.lang.Math.random() + 1);
  36.         totalPlayer1 += randomInt;
  37.  
  38.         return randomInt;
  39.     }
  40.  
  41.     public static double changePlayer2() {
  42.         // Plus 18 so it is atleast 16 and not 0
  43.         double randomInt = (int) (8 * java.lang.Math.random() + 18);
  44.         totalPlayer2 += randomInt;
  45.  
  46.         return randomInt;
  47.     }
  48.  
  49.     public static void startGame() {
  50.         for (int i = 1; i <= 2; i++) {
  51.             double returnedValue = changePlayer1();
  52.  
  53.             System.out.println("Getal " + i + ": " + returnedValue);
  54.         }
  55.         System.out.println("Totaal: " + totalPlayer1);
  56.     }
  57.  
  58.     public static void checkMidGame(int scoreOne, int scoreTwo) {
  59.         if (scoreOne == 21) {
  60.             endGame(true);
  61.         } else if (scoreOne > 21) {
  62.             endGame(false);
  63.         }
  64.     }
  65.  
  66.     public static void checkEndGame(int scoreOne, int scoreTwo) {
  67.         if (scoreTwo <= scoreOne || scoreTwo > 21) {
  68.             endGame(true);
  69.         } else {
  70.             endGame(false);
  71.         }
  72.     }
  73.  
  74.     public static void endGame(boolean won){
  75.         if (won){
  76.             if (totalPlayer1 == 21){
  77.                 System.out.println("Je hebt gewonnen! (Op de 21 gekomen)");
  78.             } else{
  79.                 System.out.println("Je hebt gewonnen! (Player 1: " + totalPlayer1 + " vs Player 2: " + totalPlayer2 + ")");
  80.             }
  81.         } else{
  82.             if (totalPlayer1 > 21){
  83.                 System.out.println("Je hebt verloren! (Boven de 21 gekomen)");
  84.             } else{
  85.                 System.out.println("Je hebt verloren! (Player 1: " + totalPlayer1 + " vs Player 2: " + totalPlayer2 + ")");
  86.             }
  87.         }
  88.         System.exit(0);
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement