Hello good, I have a problem of black and white knights on how to move them in L position, in this code in each function it makes a turn of about 45° until the black and white pieces make a complete turn to the position in which they started , but using the way knights move in chess. This is the code I am using:
chessBoard = [["N", "▢", "N"], ["▢", "▢", "▢"], ["B", "▢", "B"]]
# def printBoard(columnList):
# fila = len(columnList)
# columna = len(columnList[0])
# for i in range(fila):
# for j in range(columna):
# print(" {0} ".format(columnList[i][j]), sep=',', end='')
# print('')
def rotar_matriz(matriz):
rotada = []
for i in range(len(matriz[0])):
rotada.append([])
for j in range(len(matriz)):
rotada[i].append(matriz[len(matriz) - 1 - j][i])
return rotada
print("\n-----------------------\n")
for fila in chessBoard:
for e in fila:
print(e, sep=",", end=" ")
print()
print("\n-----------------------\n")
rotada_1 = rotar_matriz(chessBoard)
for fila in rotada_1:
for e in fila:
print(e, sep=",", end=" ")
print()
print("\n-----------------------\n")
rotada_2 = rotar_matriz(rotada_1)
for fila in rotada_2:
for e in fila:
print(e, sep=",", end=" ")
print()
print("\n-----------------------\n")
rotada_3 = rotar_matriz(rotada_2)
for fila in rotada_3:
for e in fila:
print(e, sep=",", end=" ")
print()
print("\n-----------------------\n")
rotada_4 = rotar_matriz(rotada_3)
for fila in rotada_4:
for e in fila:
print(e, sep=",", end=" ")
print()
print("\n-----------------------\n")
print("\n***********************\n")
# printBoard(chessBoard)
print("\n***********************\n")
From this code you have the first N to move to the right 2 times then go down a 1 that would give in the L position, just as the white below the first black has to go up 2 times then go to the right in 1 that each piece is move in L to the right but don't be too close to the other glued together, like a square you turn it, a square rhombus appears, then you turn it again it's square again, you turn it again, a rhombus comes out, you turn it about 4 to 6 times in L each one without touching until they turn around
Note: This answer has been edited to include the final rotation.
The board is represented as a list of lists, where each square is a character
A function to print the board. nothing complicated
The rotate function, which receives an array and rotates it 90° counterclockwise:
The function
mover
moves a piece frompos1
topos2
. The move has to be legal. It is not really necessary to check that the boxpos2
is unoccupied; the method ensures that it is.The function
intercambia
performs the elementary moves to exchange two piecesshow
produces