-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbingo.py
78 lines (63 loc) · 2.39 KB
/
bingo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#this is whole code for bingo game
# bingo_logic.py and bingo_function.py are same as this but seperatly written in two files and imported to bingo_logic.py
import random
from tkinter import *
root = Tk()
root.configure(background="green")
root.title("Bingo!!!")
root.geometry("270x270")
# Initialize score
row_score = 0
column_score = 0
diagonal_score = 0
# Initialize flags for completed rows, columns, and diagonals
row_completed = [False] * 5
column_completed = [False] * 5
left_diagonal_completed = False
right_diagonal_completed = False
# function to change button color
def change_color(button):
button.config(bg="green", fg="red")
check_win()
# function to check win
def check_win():
global row_score, column_score, diagonal_score
global row_completed, column_completed, left_diagonal_completed, right_diagonal_completed
# check rows
for i in range(5):
if not row_completed[i] and all(button.cget('bg') == 'green' for button in buttons[i*5:i*5+5]):
print("Row win!")
row_score += 1
row_completed[i] = True
# check columns
for j in range(5):
if not column_completed[j] and all(button.cget('bg') == 'green' for button in buttons[j:25:5]):
print("Column win!")
column_score += 1
column_completed[j] = True
# Check diagonals
if not left_diagonal_completed and all(button.cget('bg') == 'green' for button in buttons[0:25:6]):
print("Left diagonal win!")
diagonal_score += 1
left_diagonal_completed = True
if not right_diagonal_completed and all(button.cget('bg') == 'green' for button in buttons[4:20:4]):
print("Right diagonal win!")
diagonal_score += 1
right_diagonal_completed = True
print(f"Row Score: {row_score}, Column Score: {column_score}, Diagonal Score: {diagonal_score}")
if row_score + column_score + diagonal_score == 5:
print("BINGO!")
# generate random numbers
data = list(range(1, 26))
random.shuffle(data)
# create buttons for the Bingo card
buttons = []
for i in range(5):
for j in range(5):
num = data[i*5 + j]
txt = "{:^2}".format(num)
button = Button(root, text=txt, fg="green")
button.grid(row=i, column=j, padx=5, pady=5)
buttons.append(button)
button.config(command=lambda b=button: change_color(b)) # Pass button as argument to lambda
root.mainloop()