How to create Bingo game using Java
In this tutorial, we will learn how to create Bingo game in Java.
Bingo game is somewhat related to the random number guessing game. In this game, two or more players are given a random 5×5 board filled with numbers from 1 to 25. After that, each player takes a turn to guess one number on the board and all players cross the guessed numbers. When all the numbers in a row, column or diagonal of the board is fully crossed, then it counts as 1 point and when a player totals 5 point then they become the winner.
Program Code: Bingo game in Java
import java.util.Scanner; import java.util.Random; public class Bingo { public static void main(String[] args) { int[][] computer_card = new int[5][5]; int[] computer_list = new int[26]; int[][] player_card = new int[5][5]; int[] player_list = new int[26]; int winner = 0, turn, toss = 0; // turn = 0 for computer and 1 for player // winner = 1 for computer and winner = 2 for player and winner = 3 for draw for (int i = 0; i < 26; i++) { computer_list[i] = player_list[i] = 0; } Random rand = new Random(); toss = rand.nextInt(2) + 1; System.out.println("Enter your choice for toss:\n1. Head\n2. Tail"); Scanner scanner = new Scanner(System.in); turn = scanner.nextInt(); if (toss == turn) { System.out.println("You Win the toss"); turn = 1; } else { System.out.println("Computer Win the toss"); turn = 0; } rand = new Random(); createBingoCard(computer_card, computer_list, rand); rand = new Random(); createBingoCard(player_card, player_list, rand); while (true) { System.out.println("Player card:"); displayCard(player_card, player_list); drawNumber(turn, computer_list, player_list, scanner); if (turn == 1) turn = 0; else { turn = 1; } winner = checkCards(computer_card, player_card, computer_list); if (winner > 0) { break; } } System.out.println("Computer card:"); displayCard(computer_card, computer_list); System.out.println("Player card:"); displayCard(player_card, player_list); if (winner == 1) System.out.println("\nComputer Wins!"); else if (winner == 2) System.out.println("\nPlayer Wins!"); else if (winner == 3) System.out.println("\nMatch Draw"); } public static void createBingoCard(int[][] card, int[] list, Random rand) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { card[i][j] = rand.nextInt(25) + 1; if (list[card[i][j]] == 1) { j--; } else { list[card[i][j]] = 1; } } } } public static void displayCard(int[][] card, int[] list) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (list[card[i][j]] == 1) { if (card[i][j] < 10) { System.out.print("0" + card[i][j] + "\t"); } else { System.out.print(card[i][j] + "\t"); } } else { System.out.print("X\t"); } } System.out.println(); } } public static int drawNumber(int turn, int[] clist, int[] plist, Scanner scanner) { int number; if (turn == 1) { while (true) { System.out.print("Enter your number : "); number = scanner.nextInt(); if (plist[number] == 1 && number > 0 && number < 26) { clist[number] = plist[number] = 0; break; } else { System.out.println("Number already drawn! Please enter valid number !"); } } } else { Random rand = new Random(); while (true) { number = rand.nextInt(25) + 1; if (clist[number] == 1) { clist[number] = plist[number] = 0; break; } } System.out.println("Computer's Drawn Number : " + number + "\n"); } return number; } public static int checkCards(int[][] computer_card, int[][] player_card, int[] list) { int ccount, pcount, cscore, pscore, cld, pld, crd, prd, size = 5; ccount = pcount = cscore = pscore = cld = crd = pld = prd = 0; for (int i = 0; i < 5; i++) { ccount = pcount = 0; for (int j = 0; j < 5; j++) { if (list[computer_card[i][j]] == 0) { ccount++; } if (list[player_card[i][j]] == 0) { pcount++; } if (i == j) { if (list[computer_card[i][j]] == 0) { cld++; } if (list[player_card[i][j]] == 0) { pld++; } } if (i == size - j - 1) { if (list[computer_card[i][j]] == 0) { crd++; } if (list[player_card[i][j]] == 0) { prd++; } } } if (ccount == 5) { cscore++; } if (pcount == 5) { pscore++; } } for (int i = 0; i < 5; i++) { ccount = pcount = 0; for (int j = 0; j < 5; j++) { if (list[computer_card[j][i]] == 0) { ccount++; } if (list[player_card[j][i]] == 0) { pcount++; } } if (ccount == 5) { cscore++; } if (pcount == 5) { pscore++; } } if (cld == 5) { cscore++; } if (pld == 5) { pscore++; } if (crd == 5) { cscore++; } if (pld == 5) { pscore++; } if (cscore >= 5 && pscore >= 5) { return 3; } else if (pscore >= 5) { return 2; } else if (cscore >= 5) { return 1; } else { return 0; } } }
Code Explanation
This Java program is a simple implementation of the game Bingo.
First, we will declare and initialize various variables and arrays to use throughout the game. The main method is where we have implemented the game’s main logic. Then, we create two bingo cards, one for the computer and one for the player.
After that, we call the createBingoCard method twice to randomly generate numbers for each card. After creating the cards, the game will enter a loop where the player’s card is displayed and a number is drawn.
Next, we will call the drawNumber method to handle this process. If it is the player’s turn, then we ask the player to enter a number which is then stored in the number variable. If it is the computer’s turn, a random number is generated.
After a number is drawn, the turn is switched. The checkCards method is called to check if either the computer or the player has won the game. If a winner is found, the loop is broken and the game ends.
Finally, the computer and player cards are displayed, and the winner is announced based on the return value of the checkCards method. If the return value is 1, the computer wins. If it is 2, the player wins else the game is a draw.
Output
Enter your choice for toss: 1. Head 2. Tail 1 Computer Win the toss Player card: 05 22 09 11 19 13 04 03 08 15 14 21 17 18 20 24 23 01 16 07 02 10 06 12 25 Computer's Drawn Number : 15 Player card: 05 22 09 11 19 13 04 03 08 X 14 21 17 18 20 24 23 01 16 07 02 10 06 12 25 Enter your number : 5 Player card: X 22 09 11 19 13 04 03 08 X 14 21 17 18 20 24 23 01 16 07 02 10 06 12 25 Computer's Drawn Number : 16 Player card: X 22 09 11 19 13 04 03 08 X 14 21 17 18 20 24 23 01 X 07 02 10 06 12 25 Enter your number : 4 Player card: X 22 09 11 19 13 X 03 08 X 14 21 17 18 20 24 23 01 X 07 02 10 06 12 25 Computer's Drawn Number : 25 Player card: X 22 09 11 19 13 X 03 08 X 14 21 17 18 20 24 23 01 X 07 02 10 06 12 X Enter your number : 17 Player card: X 22 09 11 19 13 X 03 08 X 14 21 X 18 20 24 23 01 X 07 02 10 06 12 X Computer's Drawn Number : 11 Player card: X 22 09 X 19 13 X 03 08 X 14 21 X 18 20 24 23 01 X 07 02 10 06 12 X Enter your number : 19 Player card: X 22 09 X X 13 X 03 08 X 14 21 X 18 20 24 23 01 X 07 02 10 06 12 X Computer's Drawn Number : 12 Player card: X 22 09 X X 13 X 03 08 X 14 21 X 18 20 24 23 01 X 07 02 10 06 X X Enter your number : 8 Player card: X 22 09 X X 13 X 03 X X 14 21 X 18 20 24 23 01 X 07 02 10 06 X X Computer's Drawn Number : 20 Player card: X 22 09 X X 13 X 03 X X 14 21 X 18 X 24 23 01 X 07 02 10 06 X X Enter your number : 2 Player card: X 22 09 X X 13 X 03 X X 14 21 X 18 X 24 23 01 X 07 X 10 06 X X Computer's Drawn Number : 23 Player card: X 22 09 X X 13 X 03 X X 14 21 X 18 X 24 X 01 X 07 X 10 06 X X Enter your number : 18 Player card: X 22 09 X X 13 X 03 X X 14 21 X X X 24 X 01 X 07 X 10 06 X X Computer's Drawn Number : 9 Player card: X 22 X X X 13 X 03 X X 14 21 X X X 24 X 01 X 07 X 10 06 X X Enter your number : 7 Player card: X 22 X X X 13 X 03 X X 14 21 X X X 24 X 01 X X X 10 06 X X Computer's Drawn Number : 1 Player card: X 22 X X X 13 X 03 X X 14 21 X X X 24 X X X X X 10 06 X X Enter your number : 22 Computer card: 14 X 21 X X X 24 X X X 03 06 X X X X X 13 X X X X X 10 X Player card: X X X X X 13 X 03 X X 14 21 X X X 24 X X X X X 10 06 X X Player Wins!
Also learn: Hangman game in Java
Leave a Reply