Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Bingo {
- private boolean hasBingo;
- private int[] bingoCard;
- private boolean[] checkedNums;
- private boolean[] numsCalled;
- public static void main(String[] args) {
- new Bingo().run();
- }
- public Bingo() {
- hasBingo = false;
- numsCalled = new boolean[20];
- checkedNums = new boolean[5];
- }
- private void run() {
- while (!hasBingo) {
- printBoard();
- String input = fetchInput();
- if (input.equals("1")) {
- //check for duplicate intialisation
- if (bingoCard != null) {
- System.out.println("Card has already been made!");
- } else {
- bingoCard = createBingoCard();
- printBingoCard();
- }
- } else if (input.equals("2")) {
- //check if bingoCard has been initialised
- if (bingoCard != null) {
- checkCard(callNumber());
- hasBingo = hasBingo();
- printBingoCard();
- } else {
- System.out.println("Create a bingocard before calling a number!");
- }
- } else if (input.equals("3")) {
- System.out.println("Doen we elke keer al, ga ik niet meer omgooien. Doei.");
- } else if (input.equals("4")) {
- System.out.println("Doei.");
- break;
- }
- }
- }
- /**
- * Print the board
- */
- private void printBoard() {
- System.out.println("************************\n*\tBINGO\n************************");
- System.out.println("*\t1) Maak een Bingo-kaart");
- System.out.println("*\t2) Trek een bal");
- System.out.println("*\t3) Controleer een Bingo-kaart");
- System.out.println("*\t9) Afsluiten");
- System.out.println("************************");
- }
- /**
- * Prints the player's bingo card
- */
- private void printBingoCard() {
- String print = "";
- for (int i = 0; i < bingoCard.length; i++) {
- if (checkedNums[i]) {
- print += "(" + bingoCard[i] + ")" + ", ";
- } else {
- print += bingoCard[i] + ", ";
- }
- }
- if (hasBingo) {
- System.out.println("BINGO!!!");
- }
- System.out.println("Your bingo card has the following numbers: [" + print.substring(0, print.length() - 2) + "]");
- }
- /**
- * @return 5 numbers between 1 and 20
- */
- private int[] createBingoCard() {
- int[] ret = new int[5];
- for (int i = 0; i < 5; i++) {
- int num = fetchNumber();
- while (!uniqueBingoCardNum(ret, num)) {
- System.out.println("Number already exists in the bingo card. Please pick another number: ");
- num = fetchNumber();
- }
- ret[i] = num;
- }
- return ret;
- }
- /**
- * Check if the user input is unique in the bingoCard
- *
- * @param card bingoCard
- * @param num number to check
- * @return
- */
- private boolean uniqueBingoCardNum(int[] card, int num) {
- for (int i = 0; i < card.length; i++) {
- if (card[i] == num) {
- return false;
- }
- }
- return true;
- }
- /**
- * Fetches user input
- *
- * @return input
- */
- private String fetchInput() {
- Scanner sc = new Scanner(System.in);
- return sc.next();
- }
- /**
- * Fetches output int from user
- *
- * @return int between 1 and 20
- */
- private int fetchNumber() {
- System.out.println("Enter a number between 1 and 20: ");
- int num = Integer.parseInt(fetchInput());
- while (num < 1 || num > 20) {
- System.out.println("Invalid number! Enter a number between 1 and 20: ");
- num = Integer.parseInt(fetchInput());
- }
- return num;
- }
- /**
- * Calls a number
- *
- * @return
- */
- private int callNumber() {
- int numCalled = (int) (Math.random() * 20) + 1;
- while (!uniqueNumber(numCalled)) {
- numCalled = (int) (Math.random() * 20) + 1;
- }
- numsCalled[numCalled - 1] = true;
- return numCalled;
- }
- /**
- * Check if number has been called before
- *
- * @param num
- * @return true if not called
- */
- private boolean uniqueNumber(int num) {
- if (numsCalled[num - 1]) {
- return false;
- }
- return true;
- }
- /**
- * Check if the last called number exists in the bingoCard
- *
- * @param numCalled last called number
- */
- private void checkCard(int numCalled) {
- for (int i = 0; i < bingoCard.length; i++) {
- if (bingoCard[i] == numCalled) {
- checkedNums[i] = true;
- break;
- }
- }
- }
- /**
- * Check for bingo
- *
- * @return
- */
- private boolean hasBingo() {
- for (int i = 0; i < checkedNums.length; i++) {
- if (!checkedNums[i]) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement