-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (141 loc) · 4.46 KB
/
main.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
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
176
from random import choice
from tkinter import *
import tkinter.messagebox as mb
from ai import AI
from field import Field
from gui import SetField, PlayerField, EnemyField
from ship import Ship
SIZE = 40
job = None
in_game = False
def set_field():
global job
def on_close():
window.after_cancel(job)
window.destroy()
def update_labels():
global job
ships = field.num_ships
for i in range(1, 5):
labels[i].configure(text=f'{i}-палубных: {ships[i]}')
if ships != sf.available:
job = window.after(100, update_labels)
def start_game():
global in_game
ships = field.num_ships
if ships == sf.available:
in_game = True
on_close()
else:
window.bell()
window = Tk()
window.title('Battle Ship')
canvas = Canvas(window, width=SIZE * 15, height=SIZE * 15, bg='white')
canvas.grid(row=0, column=0, rowspan=4, columnspan=4)
field = Field()
sf = SetField(field, canvas, SIZE * 5 // 2, SIZE * 5 // 2, SIZE)
labels = {}
for i in range(1, 5):
labels[i] = Label(window)
labels[i].grid(row=4, column=i - 1)
button_ok = Button(window, text='Начать игру', command=start_game)
button_ok.grid(row=5, column=0, columnspan=4)
job = window.after(50, update_labels)
window.protocol("WM_DELETE_WINDOW", on_close)
window.mainloop()
if in_game:
return field
def game(player_field, comp_field):
global job
def on_close():
global in_game
in_game = False
window.after_cancel(job)
window.destroy()
def check_player_field(x, y):
if player_field.cells[x][y].ship:
player_field.hit(x, y)
pf.hit(x, y)
if player_field.cells_dict[(x, y)].dead:
return 2
return 1
else:
player_field.miss(x, y)
pf.miss(x, y)
return 0
def check_turn():
global job
if ef.blocked:
if not player_field.alive():
mb.showinfo('Ура!', 'Я победил!')
return
if not comp_field.alive():
mb.showinfo('Поздравляю!', 'Ты победил!')
return
x, y = ai.make_turn()
result = check_player_field(x, y)
if result == 1:
ai.hit(x, y)
elif result == 2:
ai.kill(x, y)
else:
ai.miss(x, y)
ef.unblock()
job = window.after(50, check_turn)
def restart_game():
window.after_cancel(job)
window.destroy()
window = Tk()
window.title('Battle Ship')
canvas = Canvas(window, width=SIZE * 30, height=SIZE * 15, bg='white')
canvas.grid(row=0, column=0, rowspan=4, columnspan=4)
restart_button = Button(window, text='Начать заново', command=restart_game)
quit_button = Button(window, text='Выйти', command=on_close)
restart_button.grid(row=4, column=0, columnspan=2)
quit_button.grid(row=4, column=2, columnspan=2)
pf = PlayerField(player_field, canvas, SIZE * 5 // 2, SIZE * 5 // 2, SIZE)
pf.draw_ships()
ef = EnemyField(comp_field, canvas, SIZE * 15, SIZE * 5 // 2, SIZE)
ai = AI()
job = window.after(50, check_turn)
window.protocol("WM_DELETE_WINDOW", on_close)
window.mainloop()
def generate_test_field():
comp_field = Field()
comp_ships = [
Ship(4, 0, 0, 0),
Ship(3, 0, 2, 0),
Ship(3, 0, 4, 0),
Ship(2, 0, 6, 0),
Ship(2, 0, 8, 0),
Ship(2, 6, 0, 0),
Ship(2, 6, 2, 0),
Ship(1, 4, 4, 0),
Ship(1, 6, 6, 0),
Ship(1, 6, 4, 0),
Ship(1, 6, 8, 0)
]
for ship in comp_ships:
comp_field.create_ship(ship)
return comp_field
def generate_random_field():
cells = [(x, y) for x in range(10) for y in range(10)]
f = Field()
sizes = [4] + [3] * 2 + [2] * 3 + [1] * 4
for size in sizes:
while True:
ship = Ship(size, *choice(cells), choice((0, 1)))
if f.check_cell(ship):
f.create_ship(ship)
break
return f
def main():
while True:
player_field = set_field()
if player_field:
comp_field = generate_random_field()
game(player_field, comp_field)
if not in_game:
break
if __name__ == '__main__':
main()