Advertisement
NOBLE-_-MAN

School_task

May 15th, 2022
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. def knight_moves(pos):  # the function, that returns available moves for chess knight
  2.     global chess_field
  3.     chess_field = []
  4.     for i in range(8):
  5.         chess_field.append(['o'] * 8)  # creating empty chess field
  6.  
  7.     moves = []  # creating returning list
  8.     if ord('A') <= ord(pos[0]) <= ord('H'):  # start pos letter is UpperCase
  9.         for i in range(ord('A'), ord('H') + 1):
  10.             for j in range(1, 9):
  11.                 if abs(ord(pos[0]) - i) == 2 and abs(int(pos[1]) - j) == 1 or abs(ord(pos[0]) - i) == 1 and abs(
  12.                         int(pos[1]) - j) == 2:
  13.                     moves.append(f'{chr(i)}{j}')  # appending available moves to the returning list
  14.                     chess_field[8 - j][i - ord('A')] = 'M'  # appending available moves to the field
  15.         chess_field[8 - int(pos[1])][ord(pos[0]) - ord('A')] = 'K'  # appending the knight to the field
  16.     else:  # start pos letter is LowerCase
  17.         for i in range(ord('a'), ord('h') + 1):
  18.             for j in range(1, 9):
  19.                 if abs(ord(pos[0]) - i) == 2 and abs(int(pos[1]) - j) == 1 or abs(ord(pos[0]) - i) == 1 and abs(
  20.                         int(pos[1]) - j) == 2:
  21.                     moves.append(f'{chr(i)}{j}')  # appending available moves to the returning list
  22.                     chess_field[8 - j][i - ord('a')] = 'M'  # appending available moves to the field
  23.         chess_field[8 - int(pos[1])][ord(pos[0]) - ord('a')] = 'K'  # appending the knight to the field
  24.  
  25.     return moves
  26.  
  27.  
  28. def cool_output():  # the function, that makes a cool output
  29.     for i in range(8):
  30.         print(8 - i, *chess_field[i])
  31.     print('  ', end='')
  32.     for i in range(ord('A'), ord('H') + 1):
  33.         print(chr(i), end=' ')
  34.  
  35.  
  36. # <START>
  37. print('Available moves:')
  38. print(*knight_moves('E5'))  # enter the start pos here
  39. print()
  40. print('Look at them on the chess field:')
  41. cool_output()
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement