Python I need the pseudocode of this program: import pygame, sys from pygame.loc
ID: 3854675 • Letter: P
Question
Python
I need the pseudocode of this program:
import pygame, sys
from pygame.locals import *
import numpy as np
def inRangeAndEmpty(posx,posy,board,N):
return (posx < N and posx >= 0 and posy < N and posy >= 0 and board[posx][posy] == 0)
def getAccessibility(x,y,moves,board,N):
accessibility = 0
for i in range(8):
if inRangeAndEmpty(x+moves[i][0],y+moves[i][1],board,N):
accessibility += 1
return accessibility
def getNextMoves(move,moves,board,N):
positionx = move[0]
positiony = move[1]
accessibility = 8
for i in range(8):
newx = positionx + moves[i][0]
newy = positiony + moves[i][1]
newacc = getAccessibility(newx,newy,moves,board,N)
if inRangeAndEmpty(newx,newy,board,N) and newacc < accessibility:
move[0] = newx
move[1] = newy
accessibility = newacc
return
def graphicTour(N,L_coor):
horse = pygame.image.load("knight.png")
# Initialize window size and title:
pygame.init()
window = pygame.display.set_mode((32*N,32*N))
pygame.display.set_caption("Knight's Tour")
background = pygame.image.load("chess.png")
index = 0
# Text:
font = pygame.font.SysFont("Ubuntu",16)
text = []
surface = []
while True:
# Fill background:
window.blit(background,(0,0))
if index < N*N:
window.blit(horse,(L_coor[index][0]*32,L_coor[index][1]*32))
text.append(font.render(str(index+1),True,(255,255,255)))
surface.append(text[index].get_rect())
surface[index].center = (L_coor[index][0]*32+16,L_coor[index][1]*32+16)
index += 1
else:
window.blit(horse,(L_coor[index-1][0]*32,L_coor[index-1][1]*32))
for x in range(10000000):
pass
# Check events on window:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == 27:
pygame.quit()
sys.exit()
for i in range(index):
window.blit(text[i],surface[i])
# Update window:
pygame.display.update()
def ifSolution(Board,N):
for i in range(N):
for j in range(N):
if Board[i][j] == 0:
return False
return True
# Inicializamos las variables:
N = int(input("Enter N, size of the board (NxN): "))
positionx = int(input("Enter initial x position: "))%N
positiony = int(input("Enter initial y position: "))%N
x = positionx
y = positiony
moveNumber = 2
move = [positionx,positiony]
moves = [[2,1],[2,-1],[1,2],[1,-2],[-1,2],[-1,-2],[-2,1],[-2,-1]]
Board = np.zeros([N,N])
Board[positionx][positiony] = 1
L = []
# Buscamos la solución y aplicamos Wansdorff:
for i in range(N*N):
move[0] = positionx
move[1] = positiony
getNextMoves(move,moves,Board,N)
positionx = move[0]
positiony = move[1]
Board[positionx][positiony] = moveNumber
moveNumber += 1
Board[positionx][positiony] -= 1
# Revisamos si encontramos solución:
sol = ifSolution(Board,N)
if sol:
# Añadimos las posiciones a la lista de coordenadas L:
k = 1
while k <= N*N:
for i in range(N):
for j in range(N):
if Board[i][j] == k:
L.append([i,j])
k += 1
print(Board)
else:
moves = [[2,1],[-2,1],[2,-1],[-2,-1],[1,2],[-1,2],[1,-2],[-1,-2]]
Board = np.zeros([N,N])
positionx = x
positiony = y
Board[positionx][positiony] = 1
L = []
moveNumber = 2
move = [positionx,positiony]
# Buscamos la solución y aplicamos Wansdorff:
for i in range(N*N):
move[0] = positionx
move[1] = positiony
getNextMoves(move,moves,Board,N)
positionx = move[0]
positiony = move[1]
Board[positionx][positiony] = moveNumber
moveNumber += 1
Board[positionx][positiony] -= 1
# Revisamos si encontramos solución:
sol = ifSolution(Board,N)
if sol:
# Añadimos las posiciones a la lista de coordenadas L:
k = 1
while k <= N*N:
for i in range(N):
for j in range(N):
if Board[i][j] == k:
L.append([i,j])
k += 1
print(Board)
if len(L) == 0:
print("Didn't find a solution.")
print("Knights' positions: ", L)
if N <= 32 and sol:
graphicTour(N,L)
Explanation / Answer
Pseudocode
Ask user to enter N
Ask user to enter positionx
Ask user to enter positiony
assign positionx value x
assign positiony value y
set movenumber as 2
set list of values for both move and moves
Initialise board value
set initial borad value is 1
create an empty list of L
for i in range(N*N)
Find the axis to apply wansdorff
Find axis to the next move of it till N number of times.
Check whether the solution is ok
if sol is true then
Initialise k is 1
while k less than or equal to N*N
for i in range(N)
for j in range (N)
if board value is equal to N, b[i][j]
append it(i,j) to L
Increment K value by 1
Assign axis to L and print the result of board
else
reset all values of moves, board, positionx, positiony, L and set movenumber as 2.
Do the same thing again for each value till an N number of times reached
and then check finded solution is ok
if sol is true then
Initialise k is 1
while k less than or equal to N*N
for i in range(N)
for j in range (N)
if board value is equal to N, b[i][j]
append it(i,j) to L
Increment K value by 1
Assign axis to L and print the result of board
If there is no axis point has found then
print didn't find the solution.
To check the solution is ok or not
for i in range(N)
for j in range(N)
if board value is equal to zero, b[i][j]
return false
return true
In order to find axis to the next move,
assgin MOVE of 0 index value to positionx
assgin MOVE of 1 index value to positiony
set accessibility as 8
for i in range(accessibility)
set addiotion of 2 values like positionx+moves[i][0] to newx
set addiotion of 2 values like positiony+moves[i][1] to newy
Find the accessibility for these points and assign it to newacc
if these points are in rang and newacc is less than accessibility
set new points are move points and return newacc
In order to check the points are in range,
check posx is less than N and posx is greater than or equal to zero and posy is less than N and posy is greater than or equal to zero and borad value is equal to zero.
If so
return true
else
return false
In order to check accessibility of the points,
initialize accessibility value as zero
for i in range(8)
if these points are in range and is not empty
add accessibility value by 1
return accessibility value.
if N <= 32 and sol is true
Print the result on window with graphic effects
To display the result on window
load knight image
initialize game
set window size
set caption
load and set background image
set font size
while true
set the background
check events on window
if event is quit
quit the game and exit
elseif event is key down
if event key is 27
quit the game and exit
display the result on window
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.