Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def knight_moves(pos): # the function, that returns available moves for chess knight
- global chess_field
- chess_field = []
- for i in range(8):
- chess_field.append(['o'] * 8) # creating empty chess field
- moves = [] # creating returning list
- if ord('A') <= ord(pos[0]) <= ord('H'): # start pos letter is UpperCase
- for i in range(ord('A'), ord('H') + 1):
- for j in range(1, 9):
- if abs(ord(pos[0]) - i) == 2 and abs(int(pos[1]) - j) == 1 or abs(ord(pos[0]) - i) == 1 and abs(
- int(pos[1]) - j) == 2:
- moves.append(f'{chr(i)}{j}') # appending available moves to the returning list
- chess_field[8 - j][i - ord('A')] = 'M' # appending available moves to the field
- chess_field[8 - int(pos[1])][ord(pos[0]) - ord('A')] = 'K' # appending the knight to the field
- else: # start pos letter is LowerCase
- for i in range(ord('a'), ord('h') + 1):
- for j in range(1, 9):
- if abs(ord(pos[0]) - i) == 2 and abs(int(pos[1]) - j) == 1 or abs(ord(pos[0]) - i) == 1 and abs(
- int(pos[1]) - j) == 2:
- moves.append(f'{chr(i)}{j}') # appending available moves to the returning list
- chess_field[8 - j][i - ord('a')] = 'M' # appending available moves to the field
- chess_field[8 - int(pos[1])][ord(pos[0]) - ord('a')] = 'K' # appending the knight to the field
- return moves
- def cool_output(): # the function, that makes a cool output
- for i in range(8):
- print(8 - i, *chess_field[i])
- print(' ', end='')
- for i in range(ord('A'), ord('H') + 1):
- print(chr(i), end=' ')
- # <START>
- print('Available moves:')
- print(*knight_moves('E5')) # enter the start pos here
- print()
- print('Look at them on the chess field:')
- cool_output()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement