Create Bingo Game Using Python
A card with a grid of numbers on it is used to play the popular dice game of bingo. Players check off numbers on their cards when they are selected at random by a caller, competing to be the first to mark off all of their numbers in a particular order. We'll examine how to utilise Python to create a simple bingo game in this lesson.
The following ideas must be understood in order to design a bingo game:
- Lists: A list is a group of items that can contain any sort of data.
- Random module: In Python, random numbers are produced using the random package.
- Loops: Loops are used to repeatedly run a block of code until a predetermined condition is satisfied.
- Functions: A set of statements that carry out a particular activity are grouped together into functions.
Steps:
- Make a list of all potential bingo numbers (1–25).
- For a random order of the numbers, mix up the list.
- With the centre square denoted as "free," make a bingo card with a 5x5 grid of numbers.
- Make a list of 24 distinct bingo cards with various number combinations.
- Write a function that draws a number at random from the shuffled list of bingo numbers and checks to see whether any of the numbers on the players' bingo cards match the selected number.
- When a player has checked off all the numbers in a certain sequence, keep drawing numbers (e.g., a straight line, a diagonal, or all four corners).
Source Code :
import random
bingo_rules = {
'B': 5,
'I': 5,
'N': 5,
'G': 5,
'O': 5
}
# Define a function to generate a randomized Bingo ball
def get_bingo_ball(used_balls):
all_balls = [i for i in range(1, 26)]
available_balls = set(all_balls) - used_balls
if available_balls:
ball = random.choice(list(available_balls))
used_balls.add(ball)
return ball
else:
return None
# Define a function to check if a Bingo card has a winning combination
def check_bingo(card):
rows = card
cols = [[card[j][i] for j in range(5)] for i in range(5)]
diags = [[card[i][i] for i in range(5)], [card[i][4-i] for i in range(5)]]
lines = rows + cols + diags
for line in lines:
if len(set(line)) == 1 and line[0] != 0:
return True
return False
def generate_card():
card = [[0]*5 for i in range(5)]
used_numbers = set()
for i in range(5):
for j in range(5):
if j == 2 and i == 2:
continue
while True:
number = random.randint(i*5+1, i*5+5)
if number not in used_numbers:
card[i][j] = number
used_numbers.add(number)
break
return card
# Play a game of Bingo
def play_bingo():
# Generate two Bingo cards
card1 = generate_card()
card2 = generate_card()
# Print out the two Bingo cards
print("Bingo Card 1:")
for row in card1:
print(' '.join([str(n).rjust(2) for n in row]))
print()
print("Bingo Card 2:")
for row in card2:
print(' '.join([str(n).rjust(2) for n in row]))
print()
# Start the Bingo game loop
used_balls = set()
while True:
# Generate a new Bingo ball
ball = get_bingo_ball(used_balls)
if ball is None:
print("All Bingo balls have been drawn. The game is a tie.")
break
# Print out the new Bingo ball
print("New Bingo ball: ", ball)
# Check if either player has a winning Bingo card
if check_bingo(card1):
print("Bingo! Player 1 wins!")
break
if check_bingo(card2):
print("Bingo! Player 2 wins!")
break
# Wait for player input before drawing the next ball
input("Press Enter to draw the next Bingo ball...")
# Update the Bingo cards with the new ball
for i in range(5):
for j in range(5):
if card1[i][j] == ball:
card1[i][j] = "X"
if card2[i][j] == ball:
card2[i][j] = "X"
# Print out the updated Bingo cards
print("Bingo Card 1:")
for row in card1:
print(' '.join([str(n).rjust(2) if isinstance(
n, int) else n.rjust(2) for n in row]))
print()
print("Bingo Card 2:")
for row in card2:
print(' '.join([str(n).rjust(2) if isinstance(
n, int) else n.rjust(2) for n in row]))
print()
play_bingo()
Output:
Bingo Card 1:
1 3 5 4 2
8 7 6 9 10
15 13 0 11 12
16 19 20 17 18
23 21 25 22 24
Bingo Card 2:
3 2 4 5 1
10 7 9 6 8
15 11 0 12 13
19 18 20 16 17
22 21 25 24 23
New Bingo ball: 10
Press Enter to draw the next Bingo ball...
Bingo Card 1:
1 3 5 4 2
8 7 6 9 X
15 13 0 11 12
16 19 20 17 18
23 21 25 22 24
Bingo Card 2:
3 2 4 5 1
X 7 9 6 8
15 11 0 12 13
19 18 20 16 17
22 21 25 24 23
New Bingo ball: 11
Press Enter to draw the next Bingo ball...
Bingo Card 1:
1 3 5 4 2
8 7 6 9 X
15 13 0 X 12
16 19 20 17 18
23 21 25 22 24
Bingo Card 2:
3 2 4 5 1
X 7 9 6 8
15 X 0 12 13
19 18 20 16 17
22 21 25 24 23
New Bingo ball: 22
Press Enter to draw the next Bingo ball...
Bingo Card 1:
1 3 5 4 2
8 7 6 9 X
15 13 0 X 12
16 19 20 17 18
23 21 25 X 24
Bingo Card 2:
3 2 4 5 1
X 7 9 6 8
15 X 0 12 13
19 18 20 16 17
X 21 25 24 23
New Bingo ball: 1
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X 3 5 4 2
8 7 6 9 X
15 13 0 X 12
16 19 20 17 18
23 21 25 X 24
Bingo Card 2:
3 2 4 5 X
X 7 9 6 8
15 X 0 12 13
19 18 20 16 17
X 21 25 24 23
New Bingo ball: 9
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X 3 5 4 2
8 7 6 X X
15 13 0 X 12
16 19 20 17 18
23 21 25 X 24
Bingo Card 2:
3 2 4 5 X
X 7 X 6 8
15 X 0 12 13
19 18 20 16 17
X 21 25 24 23
New Bingo ball: 8
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X 3 5 4 2
X 7 6 X X
15 13 0 X 12
16 19 20 17 18
23 21 25 X 24
Bingo Card 2:
3 2 4 5 X
X 7 X 6 X
15 X 0 12 13
19 18 20 16 17
X 21 25 24 23
New Bingo ball: 3
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 4 2
X 7 6 X X
15 13 0 X 12
16 19 20 17 18
23 21 25 X 24
Bingo Card 2:
X 2 4 5 X
X 7 X 6 X
15 X 0 12 13
19 18 20 16 17
X 21 25 24 23
New Bingo ball: 4
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X 7 6 X X
15 13 0 X 12
16 19 20 17 18
23 21 25 X 24
Bingo Card 2:
X 2 X 5 X
X 7 X 6 X
15 X 0 12 13
19 18 20 16 17
X 21 25 24 23
New Bingo ball: 18
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X 7 6 X X
15 13 0 X 12
16 19 20 17 X
23 21 25 X 24
Bingo Card 2:
X 2 X 5 X
X 7 X 6 X
15 X 0 12 13
19 X 20 16 17
X 21 25 24 23
New Bingo ball: 13
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X 7 6 X X
15 X 0 X 12
16 19 20 17 X
23 21 25 X 24
Bingo Card 2:
X 2 X 5 X
X 7 X 6 X
15 X 0 12 X
19 X 20 16 17
X 21 25 24 23
New Bingo ball: 20
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X 7 6 X X
15 X 0 X 12
16 19 X 17 X
23 21 25 X 24
Bingo Card 2:
X 2 X 5 X
X 7 X 6 X
15 X 0 12 X
19 X X 16 17
X 21 25 24 23
New Bingo ball: 7
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X X 6 X X
15 X 0 X 12
16 19 X 17 X
23 21 25 X 24
Bingo Card 2:
X 2 X 5 X
X X X 6 X
15 X 0 12 X
19 X X 16 17
X 21 25 24 23
New Bingo ball: 24
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X X 6 X X
15 X 0 X 12
16 19 X 17 X
23 21 25 X X
Bingo Card 2:
X 2 X 5 X
X X X 6 X
15 X 0 12 X
19 X X 16 17
X 21 25 X 23
New Bingo ball: 12
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X X 6 X X
15 X 0 X X
16 19 X 17 X
23 21 25 X X
Bingo Card 2:
X 2 X 5 X
X X X 6 X
15 X 0 X X
19 X X 16 17
X 21 25 X 23
New Bingo ball: 6
Press Enter to draw the next Bingo ball...
Bingo Card 1:
X X 5 X 2
X X X X X
15 X 0 X X
16 19 X 17 X
23 21 25 X X
Bingo Card 2:
X 2 X 5 X
X X X X X
15 X 0 X X
19 X X 16 17
X 21 25 X 23
New Bingo ball: 25
Bingo! Player 1 wins!