Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.20 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Exercise2 {
  6.  
  7.     Scanner sc = new Scanner(System.in);
  8.     int[] card = new int[5];
  9.     int[] drawnBalls = new int[0];
  10.     int turns = 0;
  11.     boolean[] correctCard = {false, false, false, false, false};
  12.     boolean playerHasCard = false;
  13.  
  14.     public static void main(String[] args) {
  15.         new Exercise2().run();
  16.     }
  17.  
  18.     int menuSelection = -1;
  19.  
  20.     public void run(){
  21.         while (menuSelection != 9){
  22.             printMenu();
  23.             menuSelection = askForMenuSelection();
  24.             if(menuSelection == 1){
  25.                 card = createBingoCart();
  26.                 playerHasCard = true;
  27.             }
  28.             if(menuSelection == 2){
  29.                 if(!playerHasCard){
  30.                     System.out.println("Voordat je een bal mag trekken moet je eerst een Bingo-kaart maken!");
  31.                 } else {
  32.                     int ball = drawBingoBall(); // Draw ball
  33.                     while (!isUnique(drawnBalls, ball)) { // Check if ball is unique
  34.                         ball = drawBingoBall(); // If not, draw a new ball
  35.                     }
  36.                     // Ball is unique
  37.                     drawnBalls = updateDrawnBalls(drawnBalls, ball); // Add the unique ball to the drawn pool
  38.  
  39.                     System.out.println("Er wordt een bal getrokken! De bal heeft het nummer: " + ball + "!");
  40.  
  41.                     correctCard = checkIfCorrect(correctCard, card, ball);
  42.  
  43.                     printBingoCart(card, correctCard);
  44.  
  45.                     turns++;
  46.                 }
  47.             }
  48.             if(menuSelection == 3){
  49.                 if(!playerHasCard){
  50.                     System.out.println("Om je bingo kaart te bekijken moet je er eerst een maken!");
  51.                 } else {
  52.                     printBingoCart(card, correctCard);
  53.                     menuSelection = checkIfBingo(correctCard, turns);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     public void printMenu(){
  60.         System.out.println("" +
  61.                 "******************************************\n" +
  62.                 "* BINGO\n" +
  63.                 "******************************************\n" +
  64.                 "* 1) Maak een Bingo-kaart\n" +
  65.                 "* 2) Trek een bal\n" +
  66.                 "* 3) Controleer Bingo-kaart\n" +
  67.                 "* 9) Afsluiten\n" +
  68.                 "******************************************");
  69.     }
  70.  
  71.     public int askForMenuSelection(){
  72.         System.out.print("Maak een keuze: ");
  73.         menuSelection = sc.nextInt();
  74.         while (!(menuSelection == 1 || menuSelection == 2 || menuSelection == 3 || menuSelection == 9)){
  75.             System.out.print("Maak een geldige keuze uit het menu!: ");
  76.             menuSelection = sc.nextInt();
  77.         }
  78.         return menuSelection;
  79.     }
  80.  
  81.     public int[] createBingoCart(){
  82.         System.out.println("Je bingo kaart bestaat uit 5 getallen tussen de 0 en 20");
  83.         int[] card = new int[5];
  84.         for (int i = 0; i < 5; i++) {
  85.             System.out.print("Geef een waarde voor veld " + (i + 1) + ": ");
  86.             int input = sc.nextInt();
  87.             while (!(input >= 0 && input <=20)){
  88.                 System.out.print("Ongeldige waarde, voer een waarde in tussen de 0 en 20: ");
  89.                 input = sc.nextInt();
  90.             }
  91.             for (int j = 0; j < card.length; j++) {
  92.                 while (card[j] == input){
  93.                     System.out.print("Deze waarde staat al in je bingo kaart, kies een nieuwe: ");
  94.                     input = sc.nextInt();
  95.                     while (!(input >= 0 && input <=20)){
  96.                         System.out.print("Ongeldige waarde, voer een waarde in tussen de 0 en 20: ");
  97.                         input = sc.nextInt();
  98.                     }
  99.                 }
  100.             }
  101.             card[i] = input;
  102.         }
  103.         System.out.println("Je bingo kaart bestaat uit de volgende getallen: " + Arrays.toString(card));
  104.         return card;
  105.     }
  106.  
  107.     public int drawBingoBall(){
  108.         Random rand = new Random();
  109.         return rand.nextInt(20);
  110.     }
  111.  
  112.     public boolean isUnique(int[] drawnBalls, int ball){
  113.         for (int i = 0; i < drawnBalls.length; i++) {
  114.             if(drawnBalls[i] == ball){ // Check if ball is in array
  115.                 return false; // If it is, isUnique = false
  116.             }
  117.         }
  118.         return true; // If it isn't, isUnique = true
  119.     }
  120.  
  121.     public int[] updateDrawnBalls(int[] drawnBalls, int ball){
  122.         int[] updatedDrawnBalls = new int[drawnBalls.length+12]; // Create new array with 1 extra space
  123.         for (int i = 0; i < drawnBalls.length; i++) { // Copy old array to new array
  124.             int update = drawnBalls[i];
  125.             updatedDrawnBalls[i] = update;
  126.         }
  127.         updatedDrawnBalls[drawnBalls.length] = ball; // Insert thrown ball in last slot of new array
  128.         return updatedDrawnBalls;
  129.     }
  130.  
  131.     public boolean[] checkIfCorrect(boolean[] correctCard, int[] card, int ball){
  132.         for (int i = 0; i < 5; i++) {
  133.             if(card[i] == ball){
  134.                 correctCard[i] = true;
  135.             }
  136.         }
  137.         return correctCard;
  138.     }
  139.  
  140.     public void printBingoCart(int[] card, boolean[] correctCard){
  141.         System.out.print("Je Bingo-kaart: [ ");
  142.         for (int i = 0; i < 5; i++) {
  143.             if(correctCard[i]){
  144.                 System.out.print("(");
  145.             }
  146.             System.out.print(card[i]);
  147.             if(correctCard[i]){
  148.                 System.out.print(")");
  149.             }
  150.             if(i != 4){
  151.                 System.out.print(", ");
  152.             }
  153.         }
  154.         System.out.print(" ]");
  155.         System.out.println();
  156.     }
  157.  
  158.     public int checkIfBingo(boolean[] correctCard, int turns){
  159.         boolean bingo = true;
  160.         for (int i = 0; i < 5; i++) {
  161.             if(!correctCard[i]){
  162.                 bingo = false;
  163.             }
  164.         }
  165.         if(bingo){
  166.             System.out.println("BINGO! Je hebt je Bingo-kaart vol gekregen in " + turns + " beurten!");
  167.             return 9;
  168.         } else {
  169.             System.out.println("Helaas, je hebt nog geen Bingo.");
  170.         }
  171.         return -1;
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement