-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (154 loc) · 7.61 KB
/
Copy pathmain.py
File metadata and controls
175 lines (154 loc) · 7.61 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import main_game
import start_screen
import bulls_and_hits as bh
import threading as thread
import time
from bulls_and_hits import BH
def change_window(zero):
""" The function is initializing the main window of the game
:parameters: zero: if the random number is with or without zero
:returns: the function do not return any value
"""
data_base.create_game(number_of_digits=first_window.number_of_digits.curselection()[0] + 4,zero=zero)
if len(first_window.number_of_digits.curselection()) != 0:
display.number_of_digits = first_window.number_of_digits.curselection()[0] + 4
for i in range(int(first_window.number_of_computers.get())):
data_base.games[f"Game {data_base.number_of_games}"]["Computers"].append(data_base.add_computer())
number = int(first_window.number_of_computers.get())
first_window.destroy_window()
display.window.deiconify()
stats.create_button(number_of_game=data_base.number_of_games, data_base=data_base)
display.display(number_of_computers=number)
def back_to_main():
""" The function is going back to the first window -display the main menu and hides the game window from the screen
:parameters: non
:returns: the function do not return any value
"""
display.restart_game()
display.window.withdraw()
first_window.window.deiconify()
def game_logs():
""" The function is changing the main window in a case 1 player won.
(enables start, back and stats button and writing who won)
:parameters: non
:returns: the function do not return any value
"""
if len(thread.enumerate()) <= 2:
display.main_window.bottom.info_layer.start_btn["state"] = "normal"
display.main_window.bottom.info_layer.back_to_btn["state"] = "normal"
display.main_window.bottom.info_layer.stats_btn["state"] = "normal"
number_of_games = f"We played #{data_base.games[f'Game {data_base.number_of_games}']['Games Played']} games"
text=data_base.get_winner_loser()
display.main_window.bottom.info_layer.label_computer.configure(text=f"{text}\n" + number_of_games)
else:
pass
def game_loop(number, number_of_digits):
""" The function is starting the game, the threads are running this function in parallel
:parameters: number: the number of the computer (player)
:number_of_digits: digits number of the random number
:returns: the function do not return any value
"""
lock = thread.Lock()
zero = data_base.games[f"Game {data_base.number_of_games}"]['Zero']
game_backend: BH = bh.BH(number_of_digits=number_of_digits, zero=zero)
game_backend.create_list()
game_backend.choose_random()
start_number = game_backend.start_number
display.main_window.top.computer_layers[number - 1].number_label.configure(text=f"The number is: {start_number}")
lock.acquire()
data_base.add_game(
computer_number=number-1,
number=game_backend.start_number,
table_size=len(game_backend.number_list))
game_number = data_base.games[f'Game {data_base.number_of_games}']['Games Played']
lock.release()
counter = 1
while True:
lock.acquire()
game_backend.temp_number = game_backend.start_number
game_backend.create_guess()
data_base.games.get(f'Game {data_base.number_of_games}').get('Computers')[number-1]['Games'][f'{game_number}']\
['guess'].append(game_backend.guess)
game_backend.find_bulls_hits()
display.main_window.top.add_info(
table_size=len(game_backend.number_list),
layer=number,
number_bulls=game_backend.number_bulls,
number_hits=game_backend.number_hits,
number_guess=counter,
guessed_number=game_backend.guess
)
if game_backend.number_bulls == game_backend.number_of_digits:
break
counter += 1
game_backend.temp_number = game_backend.guess
game_backend.nh = game_backend.number_hits
game_backend.nb = game_backend.number_bulls
game_backend.reduce_table()
data_base.games.get(f"Game {data_base.number_of_games}").get("Computers")[number-1]['Games'][f'{game_number}']\
["table size"].append(len(game_backend.number_list))
lock.release()
time.sleep(0.95)
game_logs()
def restart_windows(array):
""" The function is restarting the window for a new game
:parameters: non
:returns: the function do not return any value
"""
[frame.destroy() for i, frame in filter(lambda x: x[0] >= 2, enumerate(array))]
#
def start_game_loops():
""" The function is initializing the game, when the user press start on the first window.
initials the user choices - number of computers(players), number of digits, with or without zero.
initials the threads- one thread for each computer
:parameters: non
:returns: the function do not return any value
"""
data_base.games[f"Game {data_base.number_of_games}"]['Games Played'] += 1
if data_base.number_of_games >= 1:
for layer in display.main_window.top.computer_layers:
restart_windows(layer.frame.winfo_children())
if len(thread.enumerate()) < 2:
for number in range(int(first_window.number_of_computers.get())):
thread.Thread(target=game_loop, args=[number+1, int(display.number_of_digits)]).start()
display.main_window.bottom.info_layer.start_btn["state"] = "disabled"
display.main_window.bottom.info_layer.back_to_btn["state"] = "disabled"
display.main_window.bottom.info_layer.stats_btn["state"] = "disabled"
def stats_open():
""" The function is opening the statistics window and hiding the ather windows
:parameters: self: the function gets self as a parameter
data_base: where we store all the data for the statistics
:returns: the function do not return any value
"""
if len(thread.enumerate()) < 2:
if data_base.number_of_games != 0:
display.window.withdraw()
stats.window.deiconify()
stats.init_frames()
stats.window.mainloop()
else:
pass
def close_all():
""" The function is closing all the windows, end of game
:parameters: non
:returns: the function do not return any value
"""
if len(thread.enumerate()) < 2:
display.window.destroy()
first_window.window.destroy()
stats.window.destroy()
if __name__ == "__main__":
first_window = start_screen.StartScreen()
data_base = bh.BullsHitsDB()
display = main_game.DisplayGame()
display.main_window.bottom.info_layer.stats_btn.config(command=stats_open)
stats = main_game.StatsWindow()
stats.back_btn.config(command=lambda: main_game.stats_and_game(stats_window=stats.window, game_window=display.window))
display.main_window.bottom.info_layer.start_btn.config(command=start_game_loops) # Start button on Display Game
first_window.start_btn_zero.config(command=lambda: change_window(zero=True))
first_window.start_btn_no_zero.config(command=lambda: change_window(zero=False))
display.main_window.bottom.info_layer.back_to_btn.config(command=back_to_main)
first_window.window.protocol("WM_DELETE_WINDOW", close_all)
display.window.protocol("WM_DELETE_WINDOW", close_all)
stats.window.protocol("WM_DELETE_WINDOW", close_all)
first_window.create_start() # First window to showcase game