Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a Prolog program equivalent to the Python program below that implements a solver for the Peg Puzzle: https://images-na.ssl-images-amazon.com/images/I/418L40lLY0L._SY355_.jpg # from,over,to : describes moves moves
Write a Prolog program equivalent to the Python program below that implements a solver for the Peg Puzzle:
https://images-na.ssl-images-amazon.com/images/I/418L40lLY0L._SY355_.jpg # from,over,to : describes moves moves = ( (0,1,3), (0,2,5), (1,3,6), (1,4,8), (2,4,7), (2,5,9), (3,6,10), (3,7,12), (4,7,11), (4,8,13), (5,8,12), (5,9,14), (3,4,5), (6,7,8), (7,8,9), (10,11,12), (11,12,13), (12,13,14) ) # generator for moves and their oposites def step() : for m in moves : f,o,t = m yield m yield t,o,f # builds cells, 1 if full 0 if empty # returns as a pair a count k of the full ones and the cells def init(i) : cells = [1]*15 cells[i] = 0 return (14,cells) # performs, if possible, a move # given the current occupancy of the cells def move(kd,fot) : k,d=kd f,o,t=fot if d[f]==1 and d[o]==1 and d[t]==0 : c=d.copy() c[f]=0 # moved away c[o]=0 # remove jumped over c[t]=1 # landing here after jump return (k-1,c) else : return None # generator that yields all possible solutions # given a cell configuration def solve(kd) : k,d=kd if k<2 : yield (none,kd) else for m in step() kc if r solve(kc) ms,newkd =r (m,ms),newkd # sets initial position with empty at i picks first solution collects path made of moves to a list def puzzle(i) kd =init(i) next(solve(kd)) mlist =[] while ms m,ms =ms mlist.append(m) return kd,mlist,newkd shows the result by printing out successive states show(kd) k,d = kd lines l t,a,b =l tab = *t print(tab,end = ) range(a,b+1) d[i] ==0 c =. 'x ' print(c,end = print('') replay sequence moves, showing state cells replay(ms,kd) #print(kd) d='d.copy()' f,o,t=md[f] >>> go() === 0 === . x x x x x x x x x x x x x x x . x . x x x x x x x x x x x x x x . . x x x . x x x x x x x x x x . x . x . x . x x x x x . x . . x x x . x . x x x x x . x . x x x . . x . . x x x x . . . . x x x . x . . x x x x . x . . . x x . . . . x x x . . . . . x x x . . . . x x x . . . . . x . . x . . . x x x . . . . . x . . x . . x . . x . . . . . . . . . . . x x . x . . . . . . . . . . . . . x x . . . . . . . . . . . . x . . === 1 === ... etc. '''
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started