100 lines
3.5 KiB
Python
100 lines
3.5 KiB
Python
|
from main import ShootResult
|
||
|
|
||
|
|
||
|
class ShipField:
|
||
|
|
||
|
field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
||
|
|
||
|
field_size = 10
|
||
|
field_mode = 0
|
||
|
ship_size = 4
|
||
|
ship_direction = 0
|
||
|
|
||
|
def __init__(self):
|
||
|
pass
|
||
|
|
||
|
def set_ship(self, row, col):
|
||
|
if row < 0 or row > self.field_size:
|
||
|
return
|
||
|
if col < 0 or col > self.field_size:
|
||
|
return
|
||
|
index = row * self.field_size + col
|
||
|
if self.ship_direction == 0:
|
||
|
if self.field_size - row < self.ship_size:
|
||
|
return
|
||
|
for r in range(row, row + self.ship_size):
|
||
|
index = r * self.field_size + col
|
||
|
self.field[index] = "1"
|
||
|
if self.ship_direction == 1:
|
||
|
if self.field_size - col < self.ship_size:
|
||
|
return
|
||
|
for c in range(col, col + self.ship_size):
|
||
|
index = row * self.field_size + c
|
||
|
self.field[index] = "1"
|
||
|
|
||
|
def shoot(self, row, col):
|
||
|
if row < 0 or row > self.field_size - 1:
|
||
|
return ShootResult.UNDEFINED
|
||
|
if col < 0 or col > self.field_size - 1:
|
||
|
return ShootResult.UNDEFINED
|
||
|
index = row * self.field_size + col
|
||
|
if (self.field[index]).strip() == "":
|
||
|
self.field[index] = "0"
|
||
|
return ShootResult.EMPTY
|
||
|
elif (self.field[index]).strip() == "1":
|
||
|
self.field[index] = "\\"
|
||
|
return ShootResult.DAMAGED
|
||
|
else:
|
||
|
return ShootResult.UNDEFINED
|
||
|
|
||
|
def check_possible(self, row, col, ship_direction):
|
||
|
if ship_direction == 0:
|
||
|
if self.field_size - row >= self.ship_size:
|
||
|
for r in range(row, row + self.ship_size):
|
||
|
if not self.check_blocked(self.field, r, col):
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
if ship_direction == 1:
|
||
|
if self.field_size - col >= self.ship_size:
|
||
|
for c in range(col, col + self.ship_size):
|
||
|
if not self.check_blocked(self.field, row, c):
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
return False
|
||
|
|
||
|
def check_blocked(self, row, col):
|
||
|
for r in range(row - 1, row + 2):
|
||
|
for c in range(col - 1, col + 2):
|
||
|
if 0 <= r < self.field_size and 0 <= c < self.field_size:
|
||
|
cell = (self.field[r * self.field_size + c]).strip()
|
||
|
if cell != '' and cell != 'p':
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
def set_ship_size(self, value):
|
||
|
if value.isnumeric():
|
||
|
nummer = int(value)
|
||
|
if 1 <= nummer <= 4:
|
||
|
self.ship_size = nummer
|
||
|
|
||
|
def set_ship_direction(self, value):
|
||
|
if value.isnumeric() == 1 or value.isnumeric() == 0:
|
||
|
self.ship_direction = value
|
||
|
|
||
|
def toggle_field_mode(self):
|
||
|
if self.field_mode == 1:
|
||
|
self.field_mode = 0
|
||
|
else:
|
||
|
self.field_mode = 0
|