Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SeaMap:
- def __init__(self):
- self.field = [list('.' * 10)] * 10
- def shoot(self, row, col, result):
- if result == 'miss':
- self.field[row][col] = '*'
- elif result == 'hit':
- self.field[row][col] = 'x'
- elif result == 'sink':
- self.field[row][col] = 'x'
- for i in range(row - 1, row + 2):
- for j in range(col - 1, col + 2):
- if 0 <= i < 10 and 0 <= j < 10 and self.field[i][j] == '.':
- self.field[i][j] = '*'
- for j in range(len(self.field)): # Если корабль расположен горизонтально
- if self.field[row][j] == 'x':
- col = j
- for i in range(row - 1, row + 2):
- for u in range(col - 1, col + 2):
- if 0 <= i < 10 and 0 <= u < 10:
- if self.field[i][u] == '.':
- self.field[i][u] = '*'
- for v in range(len(self.field)): # Если корабль расположен вертикально
- if self.field[v][col] == 'x':
- row = v
- for v in range(row - 1, row + 2):
- for u in range(col - 1, col + 2):
- if 0 <= v < 10 and 0 <= u < 10:
- if self.field[v][u] == '.':
- self.field[v][u] = '*'
- def cell(self, row, col):
- return self.field[row][col]
- sm = SeaMap()
- sm.shoot(0, 0, 'sink')
- sm.shoot(0, 9, 'sink')
- sm.shoot(9, 0, 'sink')
- sm.shoot(9, 9, 'sink')
- for row in range(10):
- for col in range(10):
- print(sm.cell(row, col), end='')
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement