-2

Chess knight can move to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. Given two different squares of the chessboard, determine whether a knight can go from the first square to the second one in a single move. The input is four characters: letters from a to h and numbers from 1 to 8, each specifying the column and the row number. First two are for the first square, and the last two for the second square. The program should print True if a knight can go from the first square to the second one in one move. Or print False otherwise.

Example 1 Can the knight go from d4 to c6? Yes! input d 4 c 6 output True Example 2 input d 4 e 2 output True Example 3 There’s no move from f6 to g5 for the chess knight

input f 6 g 5 output False

1 Answers1

1

What have you tried so far?

You can make calculations between the current position and the next one... Considering that knight can only move [1, 2] or [2, 1].

Based on that I recommend you to work with values intead of letters on the board.

def IsMovePossible(player, currentPos, nextPos):

    # You need to replace the currentPos and nextpos for the way you're working on the tiles
    xDif = abs(currentPos[0] - nextPos[0])
    yDif = abs(currentPos[1] - nextPos[1])

    if player == "Knight":
        return (xDif == 2 and yDif == 1) or (xDif == 1 and yDif == 2)

# Considering the chess tiles as array
print(IsMovePossible("Knight", currentPos, nextPos))
Raphael Frei
  • 361
  • 1
  • 10
  • Actually I am confused because 2 of the inputs are letters from_x = input() from_y = int(input()) to_x = input() to_y = int(input()) something like this from_x and to_x meant to be letters and I can't find out how to implement Taxicab geometry's (https://en.wikipedia.org/wiki/Taxicab_geometry) rule with letters – Albina Hakobyan Sep 22 '22 at 20:35
  • What you can try is to convert letters to [ASCII](http://sticksandstones.kstrom.com/appen.html) code and calculate the diference between boths (Will work as they were numbers, the same difference between numbers). How does the input appears? Like A1, D3? This way you can use substring to separate both and calculate the difference – Raphael Frei Sep 22 '22 at 22:33