Advertisement
Guest User

Codewars: Plants and Zombies

a guest
Jun 22nd, 2023
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | Source Code | 0 0
  1. # Zombie class
  2. class Zombie:
  3.     def __init__(self, health):
  4.         self.health = health
  5.  
  6.     def remove_health(self, amount):
  7.         if amount < self.health:
  8.             self.health -= amount
  9.             return 0
  10.         else:
  11.             rest = amount - self.health
  12.             self.health = 0
  13.             return rest
  14.  
  15. # Shooter class
  16. class Shooter:
  17.     def __init__(self, kind):
  18.         self.kind = "N" if kind.isnumeric() else "S"
  19.         self.shots = int(kind) if kind.isnumeric() else 1
  20.  
  21. # helper function to draw the grid
  22. def draw_grid(grid):
  23.     for row in grid:
  24.         line = ""
  25.         for item in row:
  26.             if isinstance(item, Shooter):
  27.                 line += item.kind + " "
  28.             elif isinstance(item, Zombie):
  29.                 line += str(item.health) + ("" if item.health > 9 else " ")
  30.             else:
  31.                 line += ". "
  32.         print(line)
  33.     print("--------------")
  34.     print()
  35.  
  36. # main game function
  37. def plants_and_zombies(lawn, zombies):
  38.     grid = []
  39.     row_len = len(lawn[0]) - 1
  40.     running = True
  41.     move = 0
  42.  
  43.     # Place the Shooter-Objects in the Grid
  44.     for row in range(len(lawn)):
  45.         new_row = []
  46.         for c in lawn[row]:
  47.             if c == ' ':
  48.                 new_row.append(0)
  49.             else:
  50.                 new_row.append(Shooter(c))
  51.         grid.append(new_row)
  52.  
  53.     while running:  # main game loop
  54.  
  55.         # Move Zombies
  56.         for i, row in enumerate(grid):
  57.             for j, item in enumerate(row):
  58.                 if isinstance(item, Zombie):
  59.                     grid[i][j - 1] = item
  60.                     grid[i][j] = 0
  61.                    
  62.         # Spawn Zombies
  63.         for i, row, hp in zombies:
  64.             if i == move:
  65.                 grid[row][row_len] = Zombie(hp)
  66.  
  67.         # The number shooters shoot in a acluster for every row
  68.         for r, row in enumerate(grid):
  69.             shots_in_row = 0
  70.             for item in row:
  71.                 if isinstance(item, Shooter):
  72.                     if item.kind == "N":
  73.                         shots_in_row += item.shots
  74.             for c, item in enumerate(row):
  75.                 if isinstance(item, Zombie):
  76.                     rest = item.remove_health(shots_in_row)
  77.                     if item.health <= 0:
  78.                         grid[r][c] = 0
  79.                     if rest > 0:
  80.                         shots_in_row = rest
  81.                     else:
  82.                         break
  83.  
  84.         # The super shooters shoot from rigth to left and top to bottom
  85.         for col in range(len(grid[0]) - 1, -1, -1):
  86.             for row in range(len(grid)):
  87.                 if isinstance(grid[row][col], Shooter):
  88.                     if grid[row][col].kind == "S":
  89.                         for dr, dc in [(1, 1), (-1, 1), (0, 1)]:
  90.                             if row + dr > len(grid) or row + dr < 0 or col + dc > len(grid[0]):
  91.                                 continue
  92.                             nrow, ncol = row + dr, col + dc
  93.                             while nrow >= 0 and nrow < len(grid) and ncol < len(grid[0]):
  94.                                 if isinstance(grid[nrow][ncol], Zombie):
  95.                                     grid[nrow][ncol].remove_health(1)
  96.                                     if grid[nrow][ncol].health <= 0:
  97.                                         grid[nrow][ncol] = 0
  98.                                     break
  99.                                 nrow += dr
  100.                                 ncol += dc
  101.                                
  102.         # increase the move counter an check if the game is over
  103.         # draw_grid(grid) helper function
  104.         move += 1
  105.         zombie_count = 0
  106.         for row in grid:
  107.             for item in row:
  108.                 if isinstance(item, Zombie):
  109.                     zombie_count += 1
  110.         if zombie_count <= 0:
  111.             print("None")
  112.             return None
  113.         for i in range(len(grid)):
  114.             if isinstance(grid[i][0], Zombie):
  115.                 print(move)
  116.                 return move
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement