Data Structures, General
Given a chessboard with one K and one Q, see if the Q can attack the K.
This function is given coordinates for the king and queen on a chessboard. These coordinates are given as a letter A-H for the columns and 1-8 for the row (see below for example):
Queens can move in any direction: horizontally, vertically, or diagonally, as far as possible.
This function returns True if the king is in the line of attack of the queen.
For example, the first set of boards, Under Attack, show the king under attack.
8 . . . . . . . . . . . . . . . . . . . . . . . . 8
7 . . . . . . . . . . . . . . . . . K . . . . . . 7
6 . . . K . . . Q . . . . K . . . . . . . . . . . 6
5 . . . . . . . . . . . . . . . . . . . Q . . . . 5
4 . . . . . . . . . . . . Q . . . . . . . . . . . 4
3 . . . . . . . . . . . . . . . . . . . . . . . . 3
2 . . . . . . . . . . . . . . . . . . . . . . . . 2
1 . . . . . . . . . . . . . . . . . . . . . . . . 1
A B C D E F G H A B C D E F G H A B C D E F G H
K=D6, Q=H6 K=E6, Q=E4 K=B7, Q=D5
The second set of boards, Not Under Attack, do not:
8 . . . . . . . . . . . . . . . . . . . . . . . . 8
7 . . . . . . . . . . . . . . . . . K . . . . . . 7
6 . . . K . . . . . . . . K . . . . . . . . . . . 6
5 . . . . . . . Q . . . . . . . . . . . . . . . . 5
4 . . . . . . . . . . . Q . . . . . . . Q . . . . 4
3 . . . . . . . . . . . . . . . . . . . . . . . . 3
2 . . . . . . . . . . . . . . . . . . . . . . . . 2
1 . . . . . . . . . . . . . . . . . . . . . . . . 1
A B C D E F G H A B C D E F G H A B C D E F G H
K=D6, Q=H5 K=E6, Q=D4 K=B7, Q=D4
Your function should work like follows:
>>> check("D6", "H6")
True
>>> check("E6", "E4")
True
>>> check("B7", "D5")
True
>>> check("A1", "H8")
True
>>> check("A8", "H1")
True
>>> check("D6", "H7")
False
>>> check("E6", "F4")
False
We’ve provided a file, check.py, with a function check:
def check(king, queen):
"""Given a chessboard with one K and one Q, see if the K can attack the Q.
This function is given coordinates for the king and queen on a chessboard.
These coordinates are given as a letter A-H for the columns and 1-8 for the
row, like "D6" and "B7":
"""
Implement this function.