Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. class SeaMap:
  2.     def __init__(self):
  3.         self.field = [list('.' * 10)] * 10
  4.  
  5.     def shoot(self, row, col, result):
  6.         if result == 'miss':
  7.             self.field[row][col] = '*'
  8.         elif result == 'hit':
  9.             self.field[row][col] = 'x'
  10.         elif result == 'sink':
  11.             self.field[row][col] = 'x'
  12.             for i in range(row - 1, row + 2):
  13.                 for j in range(col - 1, col + 2):
  14.                     if 0 <= i < 10 and 0 <= j < 10 and self.field[i][j] == '.':
  15.                         self.field[i][j] = '*'
  16.             for j in range(len(self.field)):  # Если корабль расположен горизонтально
  17.                 if self.field[row][j] == 'x':
  18.                     col = j
  19.                     for i in range(row - 1, row + 2):
  20.                         for u in range(col - 1, col + 2):
  21.                             if 0 <= i < 10 and 0 <= u < 10:
  22.                                 if self.field[i][u] == '.':
  23.                                     self.field[i][u] = '*'
  24.             for v in range(len(self.field)): # Если корабль расположен вертикально
  25.                 if self.field[v][col] == 'x':
  26.                     row = v
  27.                     for v in range(row - 1, row + 2):
  28.                         for u in range(col - 1, col + 2):
  29.                             if 0 <= v < 10 and 0 <= u < 10:
  30.                                 if self.field[v][u] == '.':
  31.                                     self.field[v][u] = '*'
  32.  
  33.     def cell(self, row, col):
  34.         return self.field[row][col]
  35.  
  36.  
  37. sm = SeaMap()
  38. sm.shoot(0, 0, 'sink')
  39. sm.shoot(0, 9, 'sink')
  40. sm.shoot(9, 0, 'sink')
  41. sm.shoot(9, 9, 'sink')
  42. for row in range(10):
  43.     for col in range(10):
  44.         print(sm.cell(row, col), end='')
  45.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement