Problem Set 5(Rook and Queen)

Rook and Queen
Given the position of a Rook and a queen in a chess board (8X8 board),  write an algorithm and the subsequent Python code to determine the common positions where both rook and queen can be placed in the next move. Rook can move through any number of cells,  either horizontally or vertically. Queens can move through any number of cells,  either horizontally, vertically or diagonally.  Each cell in the chess board may be represented as a 2-tuple (row,col). For example, if the current position of the rook is (3,1) then the next possible position of the rook may be either in the same column {(2,1),(1,1),(4,1),(5,1),(6,1),(7,1),(8,1)} or in the same row {(3,2),(3,3),(3,4),(3,5),(3,6),(3,7),(3,8)}. If the queen is in the position (5,3) then it can be placed in the same row {(5,1),(5,2),(5,4),(5,5),(5,6),(5,7),(5,8)} or same column {(1,3),(2,3),(3,3),(4,3),(6,3),(7,3),(8,3)} or along the diagonal of the current position {(6,4),(7,5),(8,6),(4,2),(5,1),(6,2),(7,1),(4,4),(3,5),(2,6),(1,7)}. Then the common cells for next move are {(3,3), (5,1), (7,1)}.
The output is a set of common board positions where both queen and rook can be placed. The positions must be printed in sorted order, sort it by row. When rows are same,  sort it by column.

(Hint: Use built-in function to sort the values)
Input Format
Row position of rook
Column position of rook
Row position of queen
Column position of queen
Output Format
Common position1
Common position2
NOTE: The following code is huge and you might be tempted to copy it from another source but the code i have written uses only things that have been in class. Other sources might use functions to simplify the code and reducing the size of the code by 30 lines.
Input:
The position of the rook and queen

Processing:
rook_pos = []
while True:
    for i in range(rook[0] + 1, 9):
        temp_pos = (i,rook[1])
        rook_pos.append(temp_pos)
    for i in range(1,rook[0]):
        temp_pos = (i,rook[1])
        rook_pos.append(temp_pos)
    for i in range(rook[1]+1,9):
        temp_pos = (rook[0],i)
        rook_pos.append(temp_pos)
    for i in range(1,rook[1]):
        temp_pos = (rook[0],i)
        rook_pos.append(temp_pos)
    break
queen_pos = []
while True:
    j = queen[1]
    for i in range(queen[0]+1, 9):
        temp_pos = (i, queen[1])
        j += 1
        if j>= 1 and j <= 8:
            temp_diag = (i,j)
            queen_pos.append(temp_diag)
        queen_pos.append(temp_pos)
    diff = queen[0] - 1
    j = queen[1] - diff
    for i in range(1, queen[0]):
        if j >= 1 and j <= 8:
            temp_diag = (i,j)
            queen_pos.append(temp_diag)
        j+=1
        temp_pos = (i, queen[1])
        queen_pos.append(temp_pos)
    j = queen[0]
    for i in range(queen[1]+1,9):
        j -= 1
        if j >= 1 and j <= 8:
            temp_diag = (j, i)
            queen_pos.append(temp_diag)
        temp_pos = (queen[0],i)
        queen_pos.append(temp_pos)
    diff = queen[1] - 1
    j = queen[0] + diff
    for i in range(1,queen[1]):
        if j >= 1 and j <= 8:
            temp_diag = (j, i)
            queen_pos.append(temp_diag)
        j -= 1
        temp_pos = (queen[0],i)
        queen_pos.append(temp_pos)
    break
common_pos = set(rook_pos)&set(queen_pos)
common_pos = list(common_pos)
common_pos.sort()

Output:
Display the common positions of the rook and queen












Algorithm:
Step1. Get the positions of the rook and the queen
Step2. Initialize rook_pos as an empty list
Step3. Repeat till the loop is not broken
Step3.1 Find the horizontal positions possible for rook whose values are greater than the current position and store them in rook_pos
Step3.2 find the horizontal positions possible for rook whose values are less than the current position and store them in  rook_pos
Step3.3 Do the same for the vertical positions
Step3.4 break out of the loop
Step4. Initialize queen_pos as an empty list
Step5. Repeat till the loop is not broken
Step5.1 find the horizontal positions possible for the queen and while doing that also find diagonal positions in one direction and assign these to queen_pos
Step5.2 find the vertical positions possible for the queen and while doing that find the other diagonal positions and store these in queen_pos
Step5.3 break out of the loop.
Step6. Find the common positions and display these positions
Step7. End

Program:
for i in range(2):
    row_pos = int(input())
    col_pos = int(input())
    if i == 0:
        rook = (row_pos,col_pos)
    else:
        queen = (row_pos,col_pos)
rook_pos = []
while True:
    for i in range(rook[0] + 1, 9):
        temp_pos = (i,rook[1])
        rook_pos.append(temp_pos)
    for i in range(1,rook[0]):
        temp_pos = (i,rook[1])
        rook_pos.append(temp_pos)
    for i in range(rook[1]+1,9):
        temp_pos = (rook[0],i)
        rook_pos.append(temp_pos)
    for i in range(1,rook[1]):
        temp_pos = (rook[0],i)
        rook_pos.append(temp_pos)
    break
queen_pos = []
while True:
    j = queen[1]
    for i in range(queen[0]+1, 9):
        temp_pos = (i, queen[1])
        j += 1
        if j>= 1 and j <= 8:
            temp_diag = (i,j)
            queen_pos.append(temp_diag)
        queen_pos.append(temp_pos)
    diff = queen[0] - 1
    j = queen[1] - diff
    for i in range(1, queen[0]):
        if j >= 1 and j <= 8:
            temp_diag = (i,j)
            queen_pos.append(temp_diag)
        j+=1
        temp_pos = (i, queen[1])
        queen_pos.append(temp_pos)
    j = queen[0]
    for i in range(queen[1]+1,9):
        j -= 1
        if j >= 1 and j <= 8:
            temp_diag = (j, i)
            queen_pos.append(temp_diag)
        temp_pos = (queen[0],i)
        queen_pos.append(temp_pos)
    diff = queen[1] - 1
    j = queen[0] + diff
    for i in range(1,queen[1]):
        if j >= 1 and j <= 8:
            temp_diag = (j, i)
            queen_pos.append(temp_diag)
        j -= 1
        temp_pos = (queen[0],i)
        queen_pos.append(temp_pos)
    break
common_pos = set(rook_pos)&set(queen_pos)
common_pos = list(common_pos)
common_pos.sort()
for i in common_pos:
    print(i)

No comments:

Post a Comment

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...