Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create Settings Menu in Python - Pygame
Next article icon

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: InPython, 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:

  1. Make a list of all potential bingo numbers (1–25).
  2. For a random order of the numbers, mix up the list.
  3. With the centre square denoted as "free," make a bingo card with a 5x5 grid of numbers.
  4. Make a list of 24 distinct bingo cards with various number combinations.
  5. 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.
  6. 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 :

Python3
importrandombingo_rules={'B':5,'I':5,'N':5,'G':5,'O':5}# Define a function to generate a randomized Bingo balldefget_bingo_ball(used_balls):all_balls=[iforiinrange(1,26)]available_balls=set(all_balls)-used_ballsifavailable_balls:ball=random.choice(list(available_balls))used_balls.add(ball)returnballelse:returnNone# Define a function to check if a Bingo card has a winning combinationdefcheck_bingo(card):rows=cardcols=[[card[j][i]forjinrange(5)]foriinrange(5)]diags=[[card[i][i]foriinrange(5)],[card[i][4-i]foriinrange(5)]]lines=rows+cols+diagsforlineinlines:iflen(set(line))==1andline[0]!=0:returnTruereturnFalsedefgenerate_card():card=[[0]*5foriinrange(5)]used_numbers=set()foriinrange(5):forjinrange(5):ifj==2andi==2:continuewhileTrue:number=random.randint(i*5+1,i*5+5)ifnumbernotinused_numbers:card[i][j]=numberused_numbers.add(number)breakreturncard# Play a game of Bingodefplay_bingo():# Generate two Bingo cardscard1=generate_card()card2=generate_card()# Print out the two Bingo cardsprint("Bingo Card 1:")forrowincard1:print(' '.join([str(n).rjust(2)forninrow]))print()print("Bingo Card 2:")forrowincard2:print(' '.join([str(n).rjust(2)forninrow]))print()# Start the Bingo game loopused_balls=set()whileTrue:# Generate a new Bingo ballball=get_bingo_ball(used_balls)ifballisNone:print("All Bingo balls have been drawn. The game is a tie.")break# Print out the new Bingo ballprint("New Bingo ball: ",ball)# Check if either player has a winning Bingo cardifcheck_bingo(card1):print("Bingo! Player 1 wins!")breakifcheck_bingo(card2):print("Bingo! Player 2 wins!")break# Wait for player input before drawing the next ballinput("Press Enter to draw the next Bingo ball...")# Update the Bingo cards with the new ballforiinrange(5):forjinrange(5):ifcard1[i][j]==ball:card1[i][j]="X"ifcard2[i][j]==ball:card2[i][j]="X"# Print out the updated Bingo cardsprint("Bingo Card 1:")forrowincard1:print(' '.join([str(n).rjust(2)ifisinstance(n,int)elsen.rjust(2)forninrow]))print()print("Bingo Card 2:")forrowincard2:print(' '.join([str(n).rjust(2)ifisinstance(n,int)elsen.rjust(2)forninrow]))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!


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp