Create Bingo Game Using Java
Last Updated :
22 Sep, 2023
Bingo, a game of enduring appeal characterized by chance and anticipation, has consistently captivated players across generations. With its uncomplicated rules and element of unexpected delight, Bingo emerges as an ideal selection for diverse occasions. If your curiosity leads you into the realm of game development, embarking on the creation of a Bingo game using Java stands as a superb introductory step. Within this comprehensive guide, we shall navigate you through the journey, encompassing comprehension of game mechanics and culminating in the realization of a fully functional Bingo game.
Understanding the Bingo Game
Prior to diving into coding, it's beneficial to gain a swift overview of the Bingo game's mechanics. The game unfolds on a 5x5 grid, where each column symbolizes a distinct numerical range. Participants are equipped with Bingo cards, housing a varied collection of numbers drawn from these ranges. A designated caller randomly announces numbers, prompting players to cross off matching numbers on their cards. The ultimate aim is to achieve specific arrangements, such as lines, diagonals, or a full card, before others accomplish the same feat.
Project Initialization
Before embarking on the creation of your Java-based Bingo game, it's crucial to establish your project environment. A development platform such as Eclipse or IntelliJ IDEA is essential for this endeavor. Here's a step-by-step guide to configuring your project:
Initiate a Fresh Java Project: Launch your chosen Integrated Development Environment (IDE) and initiate a new Java project. Opt for a suitable name for your project, such as BingoGame.
Craft a Main Class: Within your project structure, generate a new Java class that will function as the game's starting point. Consider naming this class BingoMain or GFG to reflect its role effectively.
Implementing Bingo's Operational Logic
With a solid foundation in place, it's time to bring the Bingo game's core functionalities to life by translating the provided C++ code into Java. The Java version presented below encompasses crucial actions like number drawing, card marking, and the detection of winning patterns.
Java
import java.util.*;
// Nikunj Sonigara
class BingoCard {
// List to store the Bingo card numbers
List<Integer> numbers = new ArrayList<>();
}
public class GFG {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Bingo!");
// Random number generator
Random random = new Random();
// Create Bingo cards for both players
BingoCard player1Card = createBingoCard(random);
BingoCard player2Card = createBingoCard(random);
// Tracks the current player
int currentPlayer = 1;
// Main game loop
while (true) {
// Get the current
// player's card
BingoCard currentCard = (currentPlayer == 1)
? player1Card
: player2Card;
// Generate a random number between 0 and 25
int drawnNumber
= generateRandomNumber(random, 0, 25);
// Mark the drawn number on both player's cards
drawNumberAndMark(player1Card, player2Card,
drawnNumber);
System.out.println("\nPlayer " + currentPlayer
+ " - Drawn Number: "
+ drawnNumber);
// Display both player's cards
System.out.println("Player 1's Card:");
displayCard(player1Card);
System.out.println(
"--------------------------");
System.out.println("Player 2's Card:");
displayCard(player2Card);
System.out.println(
"--------------------------");
// Check if the current player has achieved
// Bingo
if (hasBingo(currentCard)) {
// Exit the game loop
System.out.println(
"\nPlayer " + currentPlayer
+ " has achieved Bingo! Congratulations!");
break;
}
// Switch to the other player for the next round
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
// Close the scanner
scanner.close();
}
// Generates a random number between min and max
// (inclusive)
private static int
generateRandomNumber(Random random, int min, int max)
{
return min + random.nextInt(max - min + 1);
}
// Creates a new Bingo card and shuffles the numbers
private static BingoCard createBingoCard(Random random)
{
BingoCard card = new BingoCard();
List<Integer> possibleNumbers = new ArrayList<>();
for (int i = 1; i <= 25; ++i) {
possibleNumbers.add(i);
}
Collections.shuffle(possibleNumbers, random);
// Add shuffled numbers to the
// card
card.numbers.addAll(possibleNumbers);
return card;
}
// Marks the drawn number on both player's cards
private static void
drawNumberAndMark(BingoCard player1Card,
BingoCard player2Card, int number)
{
for (int i = 0; i < player1Card.numbers.size();
++i) {
// Mark the number as drawn on
// player 1's card
if (player1Card.numbers.get(i) == number) {
player1Card.numbers.set(i, 0);
}
// Mark the number as drawn on
// player 2's card
if (player2Card.numbers.get(i) == number) {
player2Card.numbers.set(i, 0);
}
}
}
// Checks if the Bingo card has achieved a Bingo pattern
private static boolean hasBingo(BingoCard card)
{
for (int i = 0; i < 5; ++i) {
// Check rows for Bingo
if (card.numbers.get(i * 5) == 0
&& card.numbers.get(i * 5 + 1) == 0
&& card.numbers.get(i * 5 + 2) == 0
&& card.numbers.get(i * 5 + 3) == 0
&& card.numbers.get(i * 5 + 4) == 0) {
return true;
}
// Check columns for Bingo
if (card.numbers.get(i) == 0
&& card.numbers.get(i + 5) == 0
&& card.numbers.get(i + 10) == 0
&& card.numbers.get(i + 15) == 0
&& card.numbers.get(i + 20) == 0) {
return true;
}
}
// Check diagonals for Bingo
if ((card.numbers.get(0) == 0
&& card.numbers.get(6) == 0
&& card.numbers.get(12) == 0
&& card.numbers.get(18) == 0
&& card.numbers.get(24) == 0)
|| (card.numbers.get(4) == 0
&& card.numbers.get(8) == 0
&& card.numbers.get(12) == 0
&& card.numbers.get(16) == 0
&& card.numbers.get(20) == 0)) {
return true;
}
// No Bingo pattern found
return false;
}
// Displays the Bingo card
private static void displayCard(BingoCard card)
{
for (int i = 0; i < card.numbers.size(); ++i) {
// Display numbers or 'X' for
// drawn numbers
System.out.print((card.numbers.get(i) != 0)
? card.numbers.get(i)
: "X");
System.out.print("\t");
// Move to the next line
// after every 5 numbers
if ((i + 1) % 5 == 0) {
System.out.println();
}
}
}
}
Output:
Welcome to Bingo!
Player 1 - Drawn Number: 20
Player 1's Card:
7 23 21 4 5
22 3 15 2 10
19 6 18 9 17
11 16 8 25 X
12 13 14 24 1
--------------------------
Player 2's Card:
3 24 X 15 14
5 6 7 11 10
16 2 8 19 4
1 25 22 23 9
18 17 12 13 21
--------------------------
Player 2 - Drawn Number: 20
Player 1's Card:
7 23 21 4 5
22 3 15 2 10
19 6 18 9 17
11 16 8 25 X
12 13 14 24 1
--------------------------
Player 2's Card:
3 24 X 15 14
5 6 7 11 10
16 2 8 19 4
1 25 22 23 9
18 17 12 13 21
--------------------------
Player 1 - Drawn Number: 22
Player 1's Card:
7 23 21 4 5
X 3 15 2 10
19 6 18 9 17
11 16 8 25 X
12 13 14 24 1
--------------------------
Player 2's Card:
3 24 X 15 14
5 6 7 11 10
16 2 8 19 4
1 25 X 23 9
18 17 12 13 21
--------------------------
Player 2 - Drawn Number: 21
Player 1's Card:
7 23 X 4 5
X 3 15 2 10
19 6 18 9 17
11 16 8 25 X
12 13 14 24 1
--------------------------
Player 2's Card:
3 24 X 15 14
5 6 7 11 10
16 2 8 19 4
1 25 X 23 9
18 17 12 13 X
--------------------------
Player 1 - Drawn Number: 24
Player 1's Card:
7 23 X 4 5
X 3 15 2 10
19 6 18 9 17
11 16 8 25 X
12 13 14 X 1
--------------------------
Player 2's Card:
3 X X 15 14
5 6 7 11 10
16 2 8 19 4
1 25 X 23 9
18 17 12 13 X
--------------------------
Player 2 - Drawn Number: 3
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 18 9 17
11 16 8 25 X
12 13 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 11 10
16 2 8 19 4
1 25 X 23 9
18 17 12 13 X
--------------------------
Player 1 - Drawn Number: 21
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 18 9 17
11 16 8 25 X
12 13 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 11 10
16 2 8 19 4
1 25 X 23 9
18 17 12 13 X
--------------------------
Player 2 - Drawn Number: 11
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 18 9 17
X 16 8 25 X
12 13 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 4
1 25 X 23 9
18 17 12 13 X
--------------------------
Player 1 - Drawn Number: 3
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 18 9 17
X 16 8 25 X
12 13 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 4
1 25 X 23 9
18 17 12 13 X
--------------------------
Player 2 - Drawn Number: 17
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 18 9 X
X 16 8 25 X
12 13 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 4
1 25 X 23 9
18 X 12 13 X
--------------------------
Player 1 - Drawn Number: 18
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 X 9 X
X 16 8 25 X
12 13 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 4
1 25 X 23 9
X X 12 13 X
--------------------------
Player 2 - Drawn Number: 13
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 X 9 X
X 16 8 25 X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 4
1 25 X 23 9
X X 12 X X
--------------------------
Player 1 - Drawn Number: 21
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 X 9 X
X 16 8 25 X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 4
1 25 X 23 9
X X 12 X X
--------------------------
Player 2 - Drawn Number: 13
Player 1's Card:
7 23 X 4 5
X X 15 2 10
19 6 X 9 X
X 16 8 25 X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 4
1 25 X 23 9
X X 12 X X
--------------------------
Player 1 - Drawn Number: 4
Player 1's Card:
7 23 X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 25 X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
1 25 X 23 9
X X 12 X X
--------------------------
Player 2 - Drawn Number: 18
Player 1's Card:
7 23 X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 25 X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
1 25 X 23 9
X X 12 X X
--------------------------
Player 1 - Drawn Number: 25
Player 1's Card:
7 23 X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
1 X X 23 9
X X 12 X X
--------------------------
Player 2 - Drawn Number: 18
Player 1's Card:
7 23 X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
1 X X 23 9
X X 12 X X
--------------------------
Player 1 - Drawn Number: 23
Player 1's Card:
7 X X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
12 X 14 X 1
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
1 X X X 9
X X 12 X X
--------------------------
Player 2 - Drawn Number: 1
Player 1's Card:
7 X X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
12 X 14 X X
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
X X X X 9
X X 12 X X
--------------------------
Player 1 - Drawn Number: 17
Player 1's Card:
7 X X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
12 X 14 X X
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
X X X X 9
X X 12 X X
--------------------------
Player 2 - Drawn Number: 24
Player 1's Card:
7 X X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
12 X 14 X X
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
X X X X 9
X X 12 X X
--------------------------
Player 1 - Drawn Number: 12
Player 1's Card:
7 X X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
X X 14 X X
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
X X X X 9
X X X X X
--------------------------
Player 2 - Drawn Number: 11
Player 1's Card:
7 X X X 5
X X 15 2 10
19 6 X 9 X
X 16 8 X X
X X 14 X X
--------------------------
Player 2's Card:
X X X 15 14
5 6 7 X 10
16 2 8 19 X
X X X X 9
X X X X X
--------------------------
Player 2 has achieved Bingo! Congratulations!
Similar Reads
Creating Frames using Swings in Java
Swing is a part of JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components that allow a high level of customization and provide rich functionalities and is used to create window-based applications. Java s
4 min read
Introduction to Java Swing
Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract Window Toolkit [AWT]. Java Swing offers much-improved functionality over AWT, new components, expanded components features, and excellent event handling with drag-and-drop support.Introduction of Java SwingSwing has ab
8 min read
Create a Math Sprint Game in HTML CSS & JavaScript
The math sprint game presents random math questions with four options. Players click the correct answer, and if correct, it's acknowledged; otherwise, an incorrect answer message is shown. A 20-second timer adds urgency, ending the game. The score (High Score) is displayed, along with buttons to sta
5 min read
How to Create a Banner Using Applet?
In this article, we shall be building a Java Applet that shows a scrolling banner with a message that moves continually from right to left. In this article, you will learn the fundamentals of Java Applet Basics by creating a banner using Applet. Note: java.applet package package has been deprecated
6 min read
Student Grade Calculator using Java Swing
Consider the following scenario. We need to create a GUI based application which calculates grade of students according to marks obtained in 4 subjects. Below are the range of marks for different grades. MarksGrade>90%ABetween 85% - 90%BBetween 80% - 85%CBetween 70% - 80%DBetween 60% - 70%EBetwee
4 min read
How to Draw a Car using Java Applet?
An Applet is a Java program that runs in a web browser. An Applet is a Java class that extends the java.applet.Applet class, which means that to create any Applet, you need to import this class and extend it in your Applet class. The applet does not have a main() method. Applets are designed to embe
2 min read
Introduction to Processing | Java
Processing is an open-source programming language and development environment that is built on top of the Java programming language. It is specifically designed for artists, designers, and other creative professionals who want to create interactive graphics, animations, and other visual applications
6 min read
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1991. It follows the Object-oriented Programming concept and can run programs written on any OS platform. Java is a high-level, object-oriented, secure, robust, platform-independent, multi
15+ min read
Ball Tapping Game in Android
Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
4 min read
Java Programming Course : A Complete Guide
Hey tech Geeks! Welcome back! Thinking to begin learning Java from scratch to pro level! No worries, get ready to complete your learning journey as GeeksforGeeks 'Master Java Programming Course' is here to be your learning partner. Java as being the most object-oriented & network- centric langua
6 min read