загружаю фичи
This commit is contained in:
parent
3db797a56d
commit
2342bea194
|
@ -19,6 +19,23 @@ class ShipField:
|
||||||
self.ship_size = 4
|
self.ship_size = 4
|
||||||
self.ship_direction = 0
|
self.ship_direction = 0
|
||||||
|
|
||||||
|
def action(self, row, col):
|
||||||
|
if self.field_mode == 0:
|
||||||
|
if self.check_possible(row, col):
|
||||||
|
self.set_ship(row, col)
|
||||||
|
|
||||||
|
elif self.field_mode == 1:
|
||||||
|
self.shoot(row, col)
|
||||||
|
|
||||||
|
def target(self, row, col):
|
||||||
|
if self.field_mode == 0:
|
||||||
|
self.clear_marker()
|
||||||
|
|
||||||
|
def clear_marker(self):
|
||||||
|
for i in range(0, len(self.field)):
|
||||||
|
if self.field[i] == "p":
|
||||||
|
self.field[i] = ""
|
||||||
|
|
||||||
def set_ship(self, row, col):
|
def set_ship(self, row, col):
|
||||||
if row < 0 or row > self.field_size:
|
if row < 0 or row > self.field_size:
|
||||||
return
|
return
|
||||||
|
@ -53,14 +70,14 @@ class ShipField:
|
||||||
else:
|
else:
|
||||||
return ShootResult.UNDEFINED
|
return ShootResult.UNDEFINED
|
||||||
|
|
||||||
def check_possible(self, row, col, ship_size, ship_direction):
|
def check_possible(self, row, col, ship_direction):
|
||||||
# Функция должна возвращать True, если можно поставить сюда корабль,
|
# Функция должна возвращать True, если можно поставить сюда корабль,
|
||||||
# в противном случае - False
|
# в противном случае - False
|
||||||
|
|
||||||
if self.ship_direction == 0:
|
if self.ship_direction == 0:
|
||||||
# Здесь мы знаем, что корабль помещается на поле.
|
# Здесь мы знаем, что корабль помещается на поле.
|
||||||
if self.field_size - row >= self.ship_size:
|
if self.field_size - row >= self.ship_size:
|
||||||
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
|
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
|
||||||
for r in range(row, row + self.ship_size):
|
for r in range(row, row + self.ship_size):
|
||||||
if not self.check_blocked(self, r, col):
|
if not self.check_blocked(self, r, col):
|
||||||
return False
|
return False
|
||||||
|
@ -76,8 +93,8 @@ class ShipField:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def check_blocked(self, row, col):
|
def check_blocked(self, row, col):
|
||||||
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
|
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
|
||||||
# либо находятся за пределами поля, либо в них нет корабля/они пустые
|
# либо находятся за пределами поля, либо в них нет корабля/они пустые
|
||||||
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 < self.field_size and 0 <= c < self.field_size:
|
if 0 <= r < self.field_size and 0 <= c < self.field_size:
|
||||||
|
@ -87,10 +104,10 @@ class ShipField:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def set_ship_size(self, value):
|
def set_ship_size(self, value):
|
||||||
if value.isnumeric():
|
if value.isnumeric():
|
||||||
number = int(value)
|
number = int(value)
|
||||||
if 1 <= number <= 4:
|
if 1 <= number <= 4:
|
||||||
self.ship_size = number
|
self.ship_size = number
|
||||||
|
|
||||||
def set_ship_direction(self, value):
|
def set_ship_direction(self, value):
|
||||||
if value.isnumeric() == 0 or value.isnumeric() == 1:
|
if value.isnumeric() == 0 or value.isnumeric() == 1:
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -217,29 +217,22 @@ def button_enter(field, buttons, row, col):
|
||||||
other_field = my_field
|
other_field = my_field
|
||||||
other_buttons = my_buttons
|
other_buttons = my_buttons
|
||||||
|
|
||||||
for i in range(0, len(other_field)):
|
|
||||||
if other_field[i] == "p":
|
|
||||||
other_field[i] = ''
|
|
||||||
|
|
||||||
colorize(other_field, other_buttons)
|
colorize(other_field, other_buttons)
|
||||||
|
|
||||||
if field_mode == 0:
|
|
||||||
for i in range(0, len(field)):
|
|
||||||
if field[i] == "p":
|
|
||||||
field[i] = ''
|
|
||||||
|
|
||||||
if check_possible(field, row, col,ship_size, ship_direction):
|
if check_possible(field, row, col,ship_size, ship_direction):
|
||||||
if ship_direction == 0:
|
if ship_direction == 0:
|
||||||
for r in range(row, row + ship_size):
|
for r in range(row, row + ship_size):
|
||||||
field[r * field_size + col] = "p"
|
field[r * field_size + col] = "p"
|
||||||
|
|
||||||
if ship_direction == 1:
|
if ship_direction == 1:
|
||||||
for c in range(col, col + ship_size):
|
for c in range(col, col + ship_size):
|
||||||
field[row * field_size + c] = "p"
|
field[row * field_size + c] = "p"
|
||||||
|
|
||||||
colorize(field, buttons)
|
colorize(field, buttons)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def check_possible(field, row, col, ship_size, ship_direction):
|
def check_possible(field, row, col, ship_size, ship_direction):
|
||||||
# Функция должна возвращать True, если можно поставить сюда корабль,
|
# Функция должна возвращать True, если можно поставить сюда корабль,
|
||||||
# в противном случае - False
|
# в противном случае - False
|
||||||
|
|
Loading…
Reference in New Issue