Create Bingo Game using Python

In this tutorial, we will learn how to create Bingo game in Python step by step.

Bingo game is something related to the random number guessing game. 2 Players are given a random 5×5 board filled with 1 to 25 numbers. Each player takes turns to guess 1 number and all players cross the guessed numbers on their board. When a row, column, or diagonal is fully crossed it counts as 1 when a player totals 5 then the player is the winner.

 

Let’s create a Board class in Python

import random

class Board:
    def __init__(self):
        self.position = {}
        self.playBoard = [
            [0,0,0,0,0],
            [0,0,0,0,0],
            [0,0,0,0,0],
            [0,0,0,0,0],
            [0,0,0,0,0],
        ]
        self.bingo = {
            "row" : [0,0,0,0,0],
            "col" : [0,0,0,0,0],
            "diagonal" : [0,0]
        }

        self.createBoard()

    def createBoard(self):
        choices = [i for i in range(1,26)]
        for i in range(5):
            for j in range(5):
                choice = random.choice(choices)
                self.playBoard[i][j] = choice
                choices.pop(choices.index(choice))
                self.position[choice] = (i,j)
    
    def updateBoard(self, val):
        x,y = self.position[val]
        self.playBoard[x][y] = 'X'
        self.updateBingo(x,y)
    
    def updateBingo(self, x, y):
        self.bingo["row"][x] += 1
        self.bingo["col"][y] += 1
        if x==y==2:
            self.bingo["diagonal"][0] += 1
            self.bingo["diagonal"][1] += 1
        elif x==y:
            self.bingo["diagonal"][0] += 1
        elif x+y == 4:
            self.bingo["diagonal"][1] += 1
    
    def checkBingo(self):
        return 5 in self.bingo["row"] or 5 in self.bingo["col"] or 5 in self.bingo["diagonal"]

createBoard() is used to creates the game board and stores the position of each cell in the dictionary to access each cell easily.

updateBoard() is used to update the game board when the player guesses a number.

updateBingo() is used to update a dictionary to update bingo.

checkBingo() is used to check player got bingo or not.

Also read: Blackjack console game using Python
Python program to create a dice game using Turtle

Let’s create a Player class for Bingo game in Python

class Player(Board):
    def __init__(self, name):
        self.name = name
        self.board = Board()
    
    def updatePlayerBoard(self, val):
        self.board.updateBoard(val)
    
    def checkBingo(self):
        return self.board.checkBingo()

updatePlayerBoard() is used to update player board.

checkBingo() is used to checks if the player got bingo or not.

Game class

Game class is to implement game functionalities.

class Game:
    def displayBoard(self, player1, player2):
        board1 = player1.board.playBoard
        board2 = player2.board.playBoard
        size = 20
        p1len = len(player1.name)
        print(player1.name+" "*(size-p1len+1)+player2.name)
        for i in range(5):
            for j in board1[i]:
                if j=='X':
                    print(f" {j}",end=" ")
                elif j>9:
                    print(j,end=" ")
                else:
                    print(f"0{j}",end=" ")
            print("      ",end="")
            for j in board2[i]:
                if j=='X':
                    print(f" {j}",end=" ")
                elif j>9:
                    print(j,end=" ")
                else:
                    print(f"0{j}",end=" ")
            print()
        print()

displayBoard() is used to display the player’s board.

Game implementation: Bingo

creating a game base and 2 players

game = Game()
player1 = Player(name="player1")
player2 = Player(name="player2")

logic for gameplay

game.displayBoard(player1, player2)

while True:
    val = int(input(f"{player1.name}'s turn : "))
    player1.updatePlayerBoard(val)
    player2.updatePlayerBoard(val)
    game.displayBoard(player1,player2)

    if player1.checkBingo() and player2.checkBingo():
        print("DRAW")
        break
    if player1.checkBingo():
        print(f"{player1.name} WON")
        break
    if player2.checkBingo():
        print(f"{player2.name} WON")
        break
    

    val = int(input(f"{player1.name}'s turn : "))
    player1.updatePlayerBoard(val)
    player2.updatePlayerBoard(val)
    game.displayBoard(player1,player2)

    if player1.checkBingo() and player2.checkBingo():
        print("DRAW")
        break
    if player1.checkBingo():
        print(f"{player1.name} WON")
        break
    if player2.checkBingo():
        print(f"{player2.name} WON")
        break

output

player1              player2
06 11 18 03 01       24 02 04 13 18
16 04 20 12 25       08 16 22 01 12
13 10 24 19 08       20 14 21 11 19
05 23 17 14 22       15 23 07 03 09
15 21 02 07 09       17 10 25 06 05

player1's turn : 6
player1              player2
 X 11 18 03 01       24 02 04 13 18
16 04 20 12 25       08 16 22 01 12
13 10 24 19 08       20 14 21 11 19
05 23 17 14 22       15 23 07 03 09
15 21 02 07 09       17 10 25  X 05

player2's turn : 11
player1              player2
 X  X 18 03 01       24 02 04 13 18
16 04 20 12 25       08 16 22 01 12
13 10 24 19 08       20 14 21  X 19
05 23 17 14 22       15 23 07 03 09
15 21 02 07 09       17 10 25  X 05

player1's turn : 4
player1              player2
 X  X 18 03 01       24 02  X 13 18
16  X 20 12 25       08 16 22 01 12
13 10 24 19 08       20 14 21  X 19
05 23 17 14 22       15 23 07 03 09
15 21 02 07 09       17 10 25  X 05

player2's turn : 24
player1              player2
 X  X 18 03 01        X 02  X 13 18
16  X 20 12 25       08 16 22 01 12
13 10  X 19 08       20 14 21  X 19
05 23 17 14 22       15 23 07 03 09
15 21 02 07 09       17 10 25  X 05

player1's turn : 14
player1              player2
 X  X 18 03 01        X 02  X 13 18
16  X 20 12 25       08 16 22 01 12
13 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23 07 03 09
15 21 02 07 09       17 10 25  X 05

player2's turn : 3
player1              player2
 X  X 18  X 01        X 02  X 13 18
16  X 20 12 25       08 16 22 01 12
13 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23 07  X 09
15 21 02 07 09       17 10 25  X 05

player1's turn : 1
player1              player2
 X  X 18  X  X        X 02  X 13 18
16  X 20 12 25       08 16 22  X 12
13 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23 07  X 09
15 21 02 07 09       17 10 25  X 05

player2's turn : 2
player1              player2
 X  X 18  X  X        X  X  X 13 18
16  X 20 12 25       08 16 22  X 12
13 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23 07  X 09
15 21  X 07 09       17 10 25  X 05

player1's turn : 18
player1              player2
 X  X  X  X  X        X  X  X 13  X
16  X 20 12 25       08 16 22  X 12
13 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23 07  X 09
15 21  X 07 09       17 10 25  X 05

player2's turn : 9
player1              player2
 X  X  X  X  X        X  X  X 13  X
16  X 20 12 25       08 16 22  X 12
13 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23 07  X  X
15 21  X 07  X       17 10 25  X 05

player1's turn : 13
player1              player2
 X  X  X  X  X        X  X  X  X  X
16  X 20 12 25       08 16 22  X 12
 X 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23 07  X  X
15 21  X 07  X       17 10 25  X 05

player2's turn : 7
player1              player2
 X  X  X  X  X        X  X  X  X  X
16  X 20 12 25       08 16 22  X 12
 X 10  X 19 08       20  X 21  X 19
05 23 17  X 22       15 23  X  X  X
15 21  X  X  X       17 10 25  X 05

player1's turn : 23
player1              player2
 X  X  X  X  X        X  X  X  X  X
16  X 20 12 25       08 16 22  X 12
 X 10  X 19 08       20  X 21  X 19
05  X 17  X 22       15  X  X  X  X
15 21  X  X  X       17 10 25  X 05

player2's turn : 15
player1              player2
 X  X  X  X  X        X  X  X  X  X
16  X 20 12 25       08 16 22  X 12
 X 10  X 19 08       20  X 21  X 19
05  X 17  X 22        X  X  X  X  X
 X 21  X  X  X       17 10 25  X 05

player1's turn : 22
player1              player2
 X  X  X  X  X        X  X  X  X  X
16  X 20 12 25       08 16  X  X 12
 X 10  X 19 08       20  X 21  X 19
05  X 17  X  X        X  X  X  X  X
 X 21  X  X  X       17 10 25  X 05

player2's turn : 5
player1              player2
 X  X  X  X  X        X  X  X  X  X
16  X 20 12 25       08 16  X  X 12
 X 10  X 19 08       20  X 21  X 19
 X  X 17  X  X        X  X  X  X  X
 X 21  X  X  X       17 10 25  X  X

player1's turn : 16
player1              player2
 X  X  X  X  X        X  X  X  X  X
 X  X 20 12 25       08  X  X  X 12
 X 10  X 19 08       20  X 21  X 19
 X  X 17  X  X        X  X  X  X  X
 X 21  X  X  X       17 10 25  X  X

player2's turn : 10
player1              player2
 X  X  X  X  X        X  X  X  X  X
 X  X 20 12 25       08  X  X  X 12
 X  X  X 19 08       20  X 21  X 19
 X  X 17  X  X        X  X  X  X  X
 X 21  X  X  X       17  X 25  X  X

player1's turn : 21
player1              player2
 X  X  X  X  X        X  X  X  X  X
 X  X 20 12 25       08  X  X  X 12
 X  X  X 19 08       20  X  X  X 19
 X  X 17  X  X        X  X  X  X  X
 X  X  X  X  X       17  X 25  X  X

DRAW

 

Leave a Reply

Your email address will not be published. Required fields are marked *