Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I was trying to find the syntax error in my code. Sorry I don\'t have a picture.

ID: 3727487 • Letter: I

Question

I was trying to find the syntax error in my code. Sorry I don't have a picture. I'm doing a 8 puzzle problem for my AI class. Would appreciate it greatly. Thank You.

import random
from collections
import deque namedtuple
from operator
import eq

class Puzzle (namedtuple("PuzzleField", ["board", "width", "zero_at"])):
"""
A class representing an '8 puzzle'.
-'board' should be a list of list with integer entries 0.
e.g. [[A,B,C], [D,E,F],[G,H,I]]
"""
__slots__=()
def __new__(cls, board, width, zero_at=None):
if
zero_at
is
None:
zero_at=board.index(0)
return
super().__new__(cls, board, width, zero_at)
def solved
(self):
return all
def actions(self):
at= self.zero_at
if
at >=self.width:
yield
self.move(at-self.width)
if
at+ self.width yield
self._move(at + self.width)
if
at self.width:
yield
self._move(at-1)
def fhuffle
(self):
"""
Makes a new puzzle with 700 different moves
"""
puzzle = self
for _ in
range(700):
puzzle=random.choice
return
puzzle

def _move(self, to):
"""
Return a new puzzle where 'zero_at' and 'to' tiles have been swapped.
NOTE: all moves should be 'actions' that have been executed
"""
a, b = min(self.zero_at, to), max(self.zero_at, to)
board = list(self.board)
board[a], board[b] = board[b], board[a]
return
Puzzle(tuple(board), self.width, to)
def pprint
(self):
for
i
in
range(0, len(self.board), self.width):
print(self.board[i:i+self.width])

class Node(namedtuple("NodeFields", ["puzzle", "parent"])):
"""
A class representing an Solver node
- 'puzzle' is a Puzzle instance
- 'parent' is the preceding node generated by the solver, if any
- 'action' is the action taken to produce puzzle, if any
"""
__slots__ = ()
def __new__(cls, puzzle, parent=None):
return
super().__new__(cls, puzzle, parent)
@property
def action
(self):
if
self.parent
is None:
return None

diff_to_action = {
+self.puzzle.width: 'D',
-self.puzzle.width: 'U',
+1: 'R',
-1: 'L',
}

return
diff_to_action[self.puzzle.zero_at - self.parent.puzzle.zero_at]
def path
(node):
"""
Reconstruct a path from to the root 'parent'
"""
seen = []
while
node
is not None:
seen.append(node)
node = node.parent
return
reversed(seen)

def solve
(start):
"""
Perform breadth first search and return a path
to the solution, if it exists (otherwise None).
"""
queue = deque([Node(start)])
seen = {start}
if
start.solved():
return
path(Node(start, None))
while
queue:
node = queue.pop()
for
move
in
node.puzzle.actions():
if
move.board
not in
seen:
if
move.solved():
return
path(Node(move, node))
queue.appendleft(Node(move, node))
seen.add(move.board)
def main():
board = A,B,C, D,E,F, G,H,I
puzzle = Puzzle(board, C)
puzzle = puzzle.shuffle()
p = solve(puzzle)
for
node
in
p:
print(node.action)
node.puzzle.pprint()
print()
if
__name__ == "__main__":
main()

Explanation / Answer

import pygame, sys, time
from pygame.locals import *
from py8puzzel import*

puzzle=puzzel()
#puzzle.Solve()

pygame.init()
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
BASICFONT = pygame.font.Font('freesansbold.ttf',50)
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('8 Puzzle')

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE=(255,255,255)
Text=(0,0,0)

blockTOP=0;
blockLEFT=0;
blocks=[]
blockNumber=1

for i in range(3):
for j in range(3):

if blockNumber>8:
blocks.append({'rect':pygame.Rect(blockLEFT,blockTOP,99,99),'color':BLACK,'block':str(0)})
else:
blocks.append({'rect':pygame.Rect(blockLEFT,blockTOP,99,99),'color':GREEN,'block':str(blockNumber)})
blockNumber+=1
blockLEFT+=100
blockTOP+=100
blockLEFT=0

for b in blocks:   
pygame.draw.rect(windowSurface, b['color'], b['rect'])
textSurf = BASICFONT.render(b['block'], True,Text)
textRect = textSurf.get_rect()
textRect.center = b['rect'].left+50,b['rect'].top+50
windowSurface.blit(textSurf, textRect)
pygame.display.update()

numShufles=50
evt=False  
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN and event.button==1:
evt=True
  
while numShufles>0:
puzzle.shufler()
puzzle.PreviousNode.extend(puzzle.StartNode)
block=0
for b in blocks:
b['block']=str(puzzle.StartNode[block])
block+=1
  
if b['block']=='0':
b['color']=BLACK
else:
b['color']=GREEN
pygame.draw.rect(windowSurface, b['color'], b['rect'])
textSurf = BASICFONT.render(b['block'], True,Text)
textRect = textSurf.get_rect()
textRect.center = b['rect'].left+50,b['rect'].top+50
windowSurface.blit(textSurf, textRect)
pygame.display.update()
time.sleep(0.04)
numShufles-=1
  
  
if evt==True:
puzzle.sucessor(puzzle.StartNode)
nxNode=puzzle.getNextNode()
  
block=0
for b in blocks:
b['block']=str(nxNode[block])
block+=1
  
if b['block']=='0':
b['color']=BLACK
else:
b['color']=GREEN
pygame.draw.rect(windowSurface, b['color'], b['rect'])
textSurf = BASICFONT.render(b['block'], True,Text)
textRect = textSurf.get_rect()
textRect.center = b['rect'].left+50,b['rect'].top+50
windowSurface.blit(textSurf, textRect)
pygame.display.update()
time.sleep(0.3)
count=1
  
while nxNode!=puzzle.GoalNode:
#print(self.fronts)
  
count+=1
puzzle.sucessor(nxNode)
nxNode=puzzle.getNextNode()
block=0
for b in blocks:
b['block']=str(nxNode[block])
block+=1
  
if b['block']=='0':
b['color']=BLACK
else:
b['color']=GREEN
pygame.draw.rect(windowSurface, b['color'], b['rect'])
textSurf = BASICFONT.render(b['block'], True,Text)
textRect = textSurf.get_rect()
textRect.center = b['rect'].left+50,b['rect'].top+50
windowSurface.blit(textSurf, textRect)
pygame.display.update()
time.sleep(0.03)
break
  
  
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote