Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Squared {
- static int totalPlayer1 = 0;
- static int totalPlayer2 = 0;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- startGame();
- while (true) {
- System.out.println("Wil je extra getallen? Gebruik 'N' om te stoppen, gebruikt 'J' om door te gaan");
- String input = scanner.nextLine();
- if (input.equals("N".toLowerCase())) {
- changePlayer2();
- System.out.println("Player 2 kreeg: " + totalPlayer2);
- checkEndGame(totalPlayer1, totalPlayer2);
- break;
- } else if (input.equals("J".toLowerCase())) {
- changePlayer1();
- System.out.println("Nieuw totaal: " + totalPlayer1);
- checkMidGame(totalPlayer1, totalPlayer2);
- } else {
- System.out.println("Sorry, dat is geen geldige input");
- }
- }
- }
- // Tried to make it so this could have a variable as the arg, didn't get it to work
- public static double changePlayer1() {
- // Plus 1 so it is atleast 1 and not 0
- double randomInt = (int) (11 * java.lang.Math.random() + 1);
- totalPlayer1 += randomInt;
- return randomInt;
- }
- public static double changePlayer2() {
- // Plus 18 so it is atleast 16 and not 0
- double randomInt = (int) (8 * java.lang.Math.random() + 18);
- totalPlayer2 += randomInt;
- return randomInt;
- }
- public static void startGame() {
- for (int i = 1; i <= 2; i++) {
- double returnedValue = changePlayer1();
- System.out.println("Getal " + i + ": " + returnedValue);
- }
- System.out.println("Totaal: " + totalPlayer1);
- }
- public static void checkMidGame(int scoreOne, int scoreTwo) {
- if (scoreOne == 21) {
- endGame(true);
- } else if (scoreOne > 21) {
- endGame(false);
- }
- }
- public static void checkEndGame(int scoreOne, int scoreTwo) {
- if (scoreTwo <= scoreOne || scoreTwo > 21) {
- endGame(true);
- } else {
- endGame(false);
- }
- }
- public static void endGame(boolean won){
- if (won){
- if (totalPlayer1 == 21){
- System.out.println("Je hebt gewonnen! (Op de 21 gekomen)");
- } else{
- System.out.println("Je hebt gewonnen! (Player 1: " + totalPlayer1 + " vs Player 2: " + totalPlayer2 + ")");
- }
- } else{
- if (totalPlayer1 > 21){
- System.out.println("Je hebt verloren! (Boven de 21 gekomen)");
- } else{
- System.out.println("Je hebt verloren! (Player 1: " + totalPlayer1 + " vs Player 2: " + totalPlayer2 + ")");
- }
- }
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement