Advertisement
go6odn28

2_fishing.py

Jun 18th, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. def next_move(rows, row, col, direction):
  2.     moves = {'up': (-1, 0),
  3.              'down': (1, 0),
  4.              'left': (0, -1),
  5.              'right': (0, 1)}
  6.  
  7.     d_row, d_col = moves[direction]
  8.     row = (row + d_row) % rows
  9.     col = (col + d_col) % rows
  10.     return row, col
  11.  
  12.  
  13. def fill_the_matrix_and_find_ship_position(rows, row, col):
  14.     matrix = []
  15.     for idx in range(rows):
  16.         r = list(input())
  17.         matrix.append(r)
  18.         if 'S' in r:
  19.             row = idx
  20.             col = r.index('S')
  21.     return matrix, row, col
  22.  
  23.  
  24. def whirlpool_check(matrix, row, col):
  25.     return matrix[row][col] == 'W'
  26.  
  27.  
  28. def mark_past_moves(field, ship_row, ship_col, next_row, next_col):
  29.     field[ship_row][ship_col] = '-'
  30.     ship_row, ship_col = next_row, next_col
  31.     field[ship_row][ship_col] = 'S'
  32.     return field, ship_row, ship_col, next_row, next_col
  33.  
  34.  
  35. def fish_check(field, next_row, next_col):
  36.     return field[next_row][next_col].isdigit()
  37.  
  38.  
  39. def print_result(field, fish_amount, quota_goal, fall_in_whirlpool, last_coordinates):
  40.     result = ''
  41.     if fall_in_whirlpool:
  42.         last_row, last_col = last_coordinates[0], last_coordinates[1]
  43.         result += (f'You fell into a whirlpool! '
  44.                    f'The ship sank and you lost the fish you caught. '
  45.                    f'Last coordinates of the ship: [{last_row},{last_col}]\n')
  46.  
  47.     else:
  48.         if fish_amount >= quota_goal:
  49.             result += 'Success! You managed to reach the quota!\n'
  50.         else:
  51.             lack_of_fish = quota_goal - fish_amount
  52.             result += (f"You didn't catch enough fish and didn't reach the quota! "
  53.                        f"You need {lack_of_fish} tons of fish more.\n")
  54.  
  55.         result += f'Amount of fish caught: {fish_amount} tons.\n' if fish_amount > 0 else ''
  56.  
  57.         for row in field:
  58.             string_row = ''.join(row)
  59.             result += f"{string_row}\n"
  60.  
  61.     result.strip()
  62.     return print(result)
  63.  
  64.  
  65. def main():
  66.     fish_amount = 0
  67.     ship_row, ship_col = 0, 0
  68.     fall_in_whirlpool = False
  69.     quota_goal = 20
  70.     last_coordinates = None
  71.     rows = int(input())
  72.     field, ship_row, ship_col = fill_the_matrix_and_find_ship_position(rows, ship_row, ship_col)
  73.  
  74.     while True:
  75.         command = input()
  76.         if command == 'collect the nets':
  77.             break
  78.  
  79.         direction = command
  80.         next_row, next_col = next_move(rows, ship_row, ship_col, direction)
  81.         fall_in_whirlpool = whirlpool_check(field, next_row, next_col)
  82.         if fall_in_whirlpool:
  83.             last_coordinates = next_row, next_col
  84.             break
  85.  
  86.         have_fish = fish_check(field, next_row, next_col)
  87.         if have_fish:
  88.             amount = int(field[next_row][next_col])
  89.             fish_amount += amount
  90.  
  91.         field, ship_row, ship_col, next_row, next_col = (
  92.             mark_past_moves(field, ship_row, ship_col, next_row, next_col))
  93.  
  94.     print_result(field, fish_amount, quota_goal, fall_in_whirlpool, last_coordinates)
  95.  
  96.  
  97. if __name__ == '__main__':
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement