Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Scanner;
- public class Exercise2 {
- Scanner sc = new Scanner(System.in);
- int[] card = new int[5];
- int[] drawnBalls = new int[0];
- int turns = 0;
- boolean[] correctCard = {false, false, false, false, false};
- boolean playerHasCard = false;
- public static void main(String[] args) {
- new Exercise2().run();
- }
- int menuSelection = -1;
- public void run(){
- while (menuSelection != 9){
- printMenu();
- menuSelection = askForMenuSelection();
- if(menuSelection == 1){
- card = createBingoCart();
- playerHasCard = true;
- }
- if(menuSelection == 2){
- if(!playerHasCard){
- System.out.println("Voordat je een bal mag trekken moet je eerst een Bingo-kaart maken!");
- } else {
- int ball = drawBingoBall(); // Draw ball
- while (!isUnique(drawnBalls, ball)) { // Check if ball is unique
- ball = drawBingoBall(); // If not, draw a new ball
- }
- // Ball is unique
- drawnBalls = updateDrawnBalls(drawnBalls, ball); // Add the unique ball to the drawn pool
- System.out.println("Er wordt een bal getrokken! De bal heeft het nummer: " + ball + "!");
- correctCard = checkIfCorrect(correctCard, card, ball);
- printBingoCart(card, correctCard);
- turns++;
- }
- }
- if(menuSelection == 3){
- if(!playerHasCard){
- System.out.println("Om je bingo kaart te bekijken moet je er eerst een maken!");
- } else {
- printBingoCart(card, correctCard);
- menuSelection = checkIfBingo(correctCard, turns);
- }
- }
- }
- }
- public void printMenu(){
- System.out.println("" +
- "******************************************\n" +
- "* BINGO\n" +
- "******************************************\n" +
- "* 1) Maak een Bingo-kaart\n" +
- "* 2) Trek een bal\n" +
- "* 3) Controleer Bingo-kaart\n" +
- "* 9) Afsluiten\n" +
- "******************************************");
- }
- public int askForMenuSelection(){
- System.out.print("Maak een keuze: ");
- menuSelection = sc.nextInt();
- while (!(menuSelection == 1 || menuSelection == 2 || menuSelection == 3 || menuSelection == 9)){
- System.out.print("Maak een geldige keuze uit het menu!: ");
- menuSelection = sc.nextInt();
- }
- return menuSelection;
- }
- public int[] createBingoCart(){
- System.out.println("Je bingo kaart bestaat uit 5 getallen tussen de 0 en 20");
- int[] card = new int[5];
- for (int i = 0; i < 5; i++) {
- System.out.print("Geef een waarde voor veld " + (i + 1) + ": ");
- int input = sc.nextInt();
- while (!(input >= 0 && input <=20)){
- System.out.print("Ongeldige waarde, voer een waarde in tussen de 0 en 20: ");
- input = sc.nextInt();
- }
- for (int j = 0; j < card.length; j++) {
- while (card[j] == input){
- System.out.print("Deze waarde staat al in je bingo kaart, kies een nieuwe: ");
- input = sc.nextInt();
- while (!(input >= 0 && input <=20)){
- System.out.print("Ongeldige waarde, voer een waarde in tussen de 0 en 20: ");
- input = sc.nextInt();
- }
- }
- }
- card[i] = input;
- }
- System.out.println("Je bingo kaart bestaat uit de volgende getallen: " + Arrays.toString(card));
- return card;
- }
- public int drawBingoBall(){
- Random rand = new Random();
- return rand.nextInt(20);
- }
- public boolean isUnique(int[] drawnBalls, int ball){
- for (int i = 0; i < drawnBalls.length; i++) {
- if(drawnBalls[i] == ball){ // Check if ball is in array
- return false; // If it is, isUnique = false
- }
- }
- return true; // If it isn't, isUnique = true
- }
- public int[] updateDrawnBalls(int[] drawnBalls, int ball){
- int[] updatedDrawnBalls = new int[drawnBalls.length+12]; // Create new array with 1 extra space
- for (int i = 0; i < drawnBalls.length; i++) { // Copy old array to new array
- int update = drawnBalls[i];
- updatedDrawnBalls[i] = update;
- }
- updatedDrawnBalls[drawnBalls.length] = ball; // Insert thrown ball in last slot of new array
- return updatedDrawnBalls;
- }
- public boolean[] checkIfCorrect(boolean[] correctCard, int[] card, int ball){
- for (int i = 0; i < 5; i++) {
- if(card[i] == ball){
- correctCard[i] = true;
- }
- }
- return correctCard;
- }
- public void printBingoCart(int[] card, boolean[] correctCard){
- System.out.print("Je Bingo-kaart: [ ");
- for (int i = 0; i < 5; i++) {
- if(correctCard[i]){
- System.out.print("(");
- }
- System.out.print(card[i]);
- if(correctCard[i]){
- System.out.print(")");
- }
- if(i != 4){
- System.out.print(", ");
- }
- }
- System.out.print(" ]");
- System.out.println();
- }
- public int checkIfBingo(boolean[] correctCard, int turns){
- boolean bingo = true;
- for (int i = 0; i < 5; i++) {
- if(!correctCard[i]){
- bingo = false;
- }
- }
- if(bingo){
- System.out.println("BINGO! Je hebt je Bingo-kaart vol gekregen in " + turns + " beurten!");
- return 9;
- } else {
- System.out.println("Helaas, je hebt nog geen Bingo.");
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement