Advertisement
Nenogzar

02. Pawn Wars

Jun 19th, 2024
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. # https://judge.softuni.org/Contests/Practice/Index/3374#1
  2.  
  3. SIZE = 8
  4.  
  5.  
  6. def initialize_board():
  7.     position_w, position_b = (-1, -1), (-1, -1)
  8.     matrix = []
  9.     for row in range(SIZE):
  10.         line = input().split(" ")
  11.         if "w" in line:
  12.             position_w = (row, line.index("w"))
  13.         if "b" in line:
  14.             position_b = (row, line.index("b"))
  15.         matrix.append(line)
  16.     return matrix, position_w, position_b
  17.  
  18.  
  19. def print_game_over(winner, position):
  20.     print(f"Game over! {winner} win, capture on {chr(97 + position[1])}{SIZE - position[0]}.")
  21.  
  22.  
  23. def print_promotion(winner, column):
  24.     row = '8' if winner == 'White' else '1'
  25.     print(f"Game over! {winner} pawn is promoted to a queen at {chr(97 + column) + row}.")
  26.  
  27.  
  28. def move_pawn(current_r, current_c, direction, matrix):
  29.     new_r = current_r + direction[0]
  30.     new_c = current_c + direction[1]
  31.     if 0 <= new_r < SIZE and 0 <= new_c < SIZE:
  32.         return new_r, new_c
  33.     return current_r, current_c
  34.  
  35.  
  36. def main():
  37.     matrix, position_w, position_b = initialize_board()
  38.     w_start_r, w_start_c = position_w
  39.     b_start_r, b_start_c = position_b
  40.  
  41.     directions_w = [(-1, 0), (-1, -1), (-1, 1)]
  42.     directions_b = [(1, 0), (1, -1), (1, 1)]
  43.  
  44.     if abs(w_start_c - b_start_c) > 1:
  45.         if w_start_r < (SIZE - 1 - b_start_r):
  46.             print_promotion('White', w_start_c)
  47.         elif w_start_r > (SIZE - 1 - b_start_r):
  48.             print_promotion('Black', b_start_c)
  49.     else:
  50.         while True:
  51.             for dr, dc in directions_w[1:]:
  52.                 new_r_w, new_c_w = move_pawn(w_start_r, w_start_c, (dr, dc), matrix)
  53.                 if matrix[new_r_w][new_c_w] == "b":
  54.                     print_game_over('White', (new_r_w, new_c_w))
  55.                     return
  56.  
  57.             matrix[w_start_r][w_start_c] = "-"
  58.             w_start_r, w_start_c = move_pawn(w_start_r, w_start_c, directions_w[0], matrix)
  59.             if w_start_r == 0:
  60.                 print_promotion('White', w_start_c)
  61.                 return
  62.             matrix[w_start_r][w_start_c] = "w"
  63.  
  64.             for dr, dc in directions_b[1:]:
  65.                 new_r_b, new_c_b = move_pawn(b_start_r, b_start_c, (dr, dc), matrix)
  66.                 if matrix[new_r_b][new_c_b] == "w":
  67.                     print_game_over('Black', (new_r_b, new_c_b))
  68.                     return
  69.  
  70.             matrix[b_start_r][b_start_c] = "-"
  71.             b_start_r, b_start_c = move_pawn(b_start_r, b_start_c, directions_b[0], matrix)
  72.             if b_start_r == SIZE - 1:
  73.                 print_promotion('Black', b_start_c)
  74.                 return
  75.             matrix[b_start_r][b_start_c] = "b"
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     main()
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement