Add new Version of ShipBattle
This commit is contained in:
parent
26f2cee30f
commit
8ff9815604
|
@ -3,7 +3,8 @@ from enum import Enum
|
||||||
|
|
||||||
field_size = 10
|
field_size = 10
|
||||||
ship_size = 4
|
ship_size = 4
|
||||||
ship_direction = 1
|
ship_direction = 0
|
||||||
|
field_mode = 0
|
||||||
|
|
||||||
buttons = []
|
buttons = []
|
||||||
|
|
||||||
|
@ -34,19 +35,22 @@ def set_ship(row, col, size, direction):
|
||||||
return
|
return
|
||||||
if col < 0 or col > field_size - 1:
|
if col < 0 or col > field_size - 1:
|
||||||
return
|
return
|
||||||
|
|
||||||
index = row * field_size + col
|
index = row * field_size + col
|
||||||
|
|
||||||
if direction == 0:
|
if direction == 0:
|
||||||
if field_size - row < size:
|
if field_size - row < size:
|
||||||
return
|
return
|
||||||
for r in range(row, row + size):
|
for r in range(row, row + size):
|
||||||
index = r * field_size + col
|
index = r * field_size + col
|
||||||
my_field[index] = "1"
|
my_field[index] = '1'
|
||||||
|
|
||||||
if direction == 1:
|
if direction == 1:
|
||||||
if field_size - col < size:
|
if field_size - col < size:
|
||||||
return
|
return
|
||||||
for c in range (col, col + size):
|
for c in range (col, col + size):
|
||||||
index = row * field_size + c
|
index = row * field_size + c
|
||||||
my_field[index] = "1"
|
my_field[index] = '1'
|
||||||
|
|
||||||
|
|
||||||
def shoot(field, row, col):
|
def shoot(field, row, col):
|
||||||
|
@ -73,12 +77,15 @@ def draw_field(window, field):
|
||||||
for r in range(0, field_size):
|
for r in range(0, field_size):
|
||||||
for c in range(0, field_size):
|
for c in range(0, field_size):
|
||||||
index = r * field_size + c
|
index = r * field_size + c
|
||||||
bg = 'white'
|
|
||||||
if field[index] == '1':
|
btn = Button(window, text='', width=5, height=2)
|
||||||
bg = 'pink'
|
|
||||||
btn = Button(window, text='', bg=bg, width=5, height=2)
|
|
||||||
btn.grid(column=c, row=r)
|
btn.grid(column=c, row=r)
|
||||||
btn.bind('<Button-1>', lambda e, x=r, y=c: button_click(field, x, y))
|
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(field, x, y))
|
||||||
|
btn.bind('<Button-3>', lambda e, x=r, y=c: right_button_click(field, x, y))
|
||||||
|
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, x, y))
|
||||||
|
for i in range(1, 5):
|
||||||
|
window.bind(str(i), lambda size=i: change_ship_size(size))
|
||||||
|
|
||||||
buttons.append(btn)
|
buttons.append(btn)
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,23 +98,22 @@ def colorize(field, buttons):
|
||||||
bg = 'red'
|
bg = 'red'
|
||||||
if field[i] == '0':
|
if field[i] == '0':
|
||||||
bg = 'black'
|
bg = 'black'
|
||||||
|
if field[i] == 'p':
|
||||||
|
bg = 'blue'
|
||||||
buttons[i].configure(bg=bg)
|
buttons[i].configure(bg=bg)
|
||||||
|
|
||||||
|
|
||||||
def check_possible(field, row, col):
|
def check_possible(field, row, col):
|
||||||
# Функция должна возвращать True, если можно поставить сюда корабль,
|
|
||||||
# в противном случае - False
|
|
||||||
|
|
||||||
if ship_direction == 0:
|
if ship_direction == 0:
|
||||||
if field_size - row >= ship_size:
|
if field_size - row >= ship_size:
|
||||||
for r in range(row, row + ship_size + 1):
|
for r in range(row, row + ship_size):
|
||||||
if not check_blocked(field, r, col):
|
if not check_blocked(field, r, col):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if ship_direction == 1:
|
if ship_direction == 1:
|
||||||
if field_size - col >= ship_size:
|
if field_size - col >= ship_size:
|
||||||
for c in range(col, col + ship_size + 1):
|
for c in range(col, col + ship_size):
|
||||||
if not check_blocked(field, row, c):
|
if not check_blocked(field, row, c):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
@ -116,23 +122,67 @@ def check_possible(field, row, col):
|
||||||
|
|
||||||
|
|
||||||
def check_blocked(field, row, col):
|
def check_blocked(field, row, col):
|
||||||
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
|
|
||||||
# либо находятся за пределами поля, либо в них нет корабля/они пустые
|
|
||||||
# Во всех других случаях, функция возвращает False
|
|
||||||
for r in range(row - 1, row + 2):
|
for r in range(row - 1, row + 2):
|
||||||
for c in range(col - 1, col + 2):
|
for c in range(col - 1, col + 2):
|
||||||
if 0 < r < field_size and 0 < c < field_size:
|
if 0 < r < field_size and 0 < c < field_size:
|
||||||
if (field[r * field_size + c]).strip() != '':
|
cell = (field[r * field_size + c]).strip()
|
||||||
|
if cell != '' and cell != 'p':
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def button_click(field, row, col):
|
def left_button_click(field, row, col):
|
||||||
shoot(field, row, col)
|
# if field[row * field_size + col] == 'p':
|
||||||
|
# set_ship(row, col, ship_size, ship_direction)
|
||||||
|
# else:
|
||||||
|
# shoot(field, row, col)
|
||||||
|
|
||||||
|
if field_mode == 0:
|
||||||
|
if check_blocked(field, row, col):
|
||||||
|
set_ship(row, col, ship_size, ship_direction)
|
||||||
|
else:
|
||||||
|
shoot(field, row, col)
|
||||||
|
|
||||||
colorize(field, buttons)
|
colorize(field, buttons)
|
||||||
|
|
||||||
|
|
||||||
|
def right_button_click(field, row, col):
|
||||||
|
global ship_direction
|
||||||
|
if field_mode == 0 and check_possible(field, row, col):
|
||||||
|
ship_direction = (ship_direction + 1) % 2
|
||||||
|
|
||||||
|
# global ship_direction
|
||||||
|
# if field_mode == 0:
|
||||||
|
# if ship_direction == 0:
|
||||||
|
# ship_direction = 1
|
||||||
|
# else:
|
||||||
|
# ship_direction = 0
|
||||||
|
|
||||||
|
|
||||||
|
def button_enter(field, row, col):
|
||||||
|
for i in range(0, len(field)):
|
||||||
|
if field[i] == 'p':
|
||||||
|
field[i] = ' '
|
||||||
|
|
||||||
|
if check_possible (field, row, col):
|
||||||
|
if ship_direction == 0:
|
||||||
|
for r in range(row, row + ship_size):
|
||||||
|
field[r * field_size + col] = 'p'
|
||||||
|
|
||||||
|
if ship_direction == 1:
|
||||||
|
for c in range(col, col + ship_size):
|
||||||
|
field[row * field_size + c] = 'p'
|
||||||
|
|
||||||
|
colorize(field, buttons)
|
||||||
|
|
||||||
|
|
||||||
|
def change_ship_size(new_size):
|
||||||
|
global ship_size
|
||||||
|
if 1 <= new_size <= 4:
|
||||||
|
ship_size = new_size
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
window = Tk()
|
window = Tk()
|
||||||
window.title("Ship Craft!")
|
window.title("Ship Craft!")
|
||||||
|
@ -147,25 +197,25 @@ set_ship(5, 8, 3, 0)
|
||||||
draw_field(window, my_field)
|
draw_field(window, my_field)
|
||||||
colorize(my_field, buttons)
|
colorize(my_field, buttons)
|
||||||
|
|
||||||
#window.mainloop()
|
window.mainloop()
|
||||||
|
|
||||||
for r in range(0, field_size):
|
# for r in range(0, field_size):
|
||||||
blocked_string = ''
|
# blocked_string = ''
|
||||||
ship_string = ''
|
# ship_string = ''
|
||||||
for c in range(0, field_size):
|
# for c in range(0, field_size):
|
||||||
blocked_string += str(check_blocked(my_field, r, c))[0] + ', '
|
# blocked_string += str(check_blocked(my_field, r, c))[0] + ', '
|
||||||
ship_string += my_field[r * field_size + c] + ', '
|
# ship_string += my_field[r * field_size + c] + ', '
|
||||||
|
|
||||||
print(blocked_string[:-2] + ' ' + ship_string[:-2])
|
# print(blocked_string[:-2] + ' ' + ship_string[:-2])
|
||||||
|
|
||||||
print("#########################################################################")
|
# print("#########################################################################")
|
||||||
|
|
||||||
|
|
||||||
for r in range(0, field_size):
|
# for r in range(0, field_size):
|
||||||
possible_string = ''
|
# possible_string = ''
|
||||||
impossible_string = ''
|
# impossible_string = ''
|
||||||
for c in range(0, field_size):
|
# for c in range(0, field_size):
|
||||||
possible_string += str(check_possible(my_field, r, c))[0] + ', '
|
# possible_string += str(check_possible(my_field, r, c))[0] + ', '
|
||||||
impossible_string += my_field[r * field_size + c] + ', '
|
# impossible_string += my_field[r * field_size + c] + ', '
|
||||||
|
|
||||||
print(possible_string[:-2] + ' ' + impossible_string[:-2])
|
# print(possible_string[:-2] + ' ' + impossible_string[:-2])'
|
Loading…
Reference in New Issue