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 5×5 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!