Frog Puzzle Challenge
Edit in new window
bubble
play_circle
Run
stop_circle
Stop
cancel
Clear
text_increase
text_decrease
light_mode
routine
dark_mode
restart_alt
Reset
download_for_offline
Download
help
Help
main.py
+
#Frog Puzzle Challenge - 101computing.net/frog-puzzle-challenge import turtle, time GREEN = 1 RED = -1 EMPTY = 0 SPEED = 0.1 # Choose a value between 0 and 1 myPen = turtle.Turtle() myPen.tracer(0) myPen.speed(0) myPen.hideturtle() screen = turtle.Screen() screen.bgcolor("#0398fc") myPen.pensize(2) myPen.penup() #Draw a frog (with two eyes!) at a given position (x,y coordinates) def drawFrog(color,x,y): myPen.goto(x,y) myPen.fillcolor(color) myPen.begin_fill() myPen.circle(10) myPen.end_fill() myPen.goto(x-8,y+15) myPen.fillcolor("white") myPen.begin_fill() myPen.circle(4) myPen.end_fill() myPen.goto(x+8,y+15) myPen.fillcolor("white") myPen.begin_fill() myPen.circle(4) myPen.end_fill() myPen.goto(x-8,y+17) myPen.fillcolor("black") myPen.begin_fill() myPen.circle(2) myPen.end_fill() myPen.goto(x+8,y+17) myPen.fillcolor("black") myPen.begin_fill() myPen.circle(2) myPen.end_fill() #Draw a ripple effect around a waterlily to show where a frog jumped to def drawRipple(color,x,y): myPen.goto(x,y-5) myPen.pendown() myPen.color(color) myPen.circle(25) myPen.penup() #Write text in the middle of the screen def writeText(color,text): style = ('Courier', 20, 'italic') myPen.color(color) myPen.penup() myPen.goto(0,0) myPen.pendown() myPen.write(text, font=style, align='center') myPen.penup() #Draw the pond, all 13 waterlilies and all the green and red frogs def drawPond(): myPen.clear() for i in range(0,len(waterlilies)): xy=coordinates[i] myPen.goto(xy[0],xy[1]) myPen.fillcolor("green") myPen.begin_fill() myPen.circle(20) myPen.end_fill() frog = waterlilies[i] if frog==GREEN: drawFrog("#00e304",xy[0],xy[1]+10) elif frog==RED: drawFrog("#f51820",xy[0],xy[1]+10) #Check if there is only one red frog left (Puzzle solved?) def checkPond(): total = 0 for value in waterlilies: total += value return total==-1 #True if only the red frog is left, False otherwise #The graph representing all the potential jumps a frog can do pond = {0:[[2,1],[6,3],[10,5]], 1:[[5,3],[11,6],[7,4]], 2:[[0,1],[6,4],[12,7]], 3:[[9,6]], 4:[[8,6]], 5:[[1,3],[7,6],[11,8]], 6:[[0,3],[2,4],[12,9],[10,8]], 7:[[1,4],[5,6],[11,9]], 8:[[4,6]], 9:[[3,6]], 10:[[0,5],[6,8],[12,11]], 11:[[5,8],[1,6],[7,9]], 12:[[10,11],[6,9],[2,7]] } #The initial position of GREEN and RED frogs on the pond. (13 waterlilies) waterlilies = [GREEN,GREEN,EMPTY,EMPTY,GREEN,GREEN,EMPTY,GREEN,GREEN,EMPTY,EMPTY,RED,EMPTY] #The coordinates of all 13 waterlilies... coordinates = [(-150,140),(0,140),(150,140), (-75,70),(75,70), (-150,0),(0,0),(150,0), (-75,-70),(75,-70), (-150,-140),(0,-140),(150,-140)] drawPond() myPen.getscreen().update() time.sleep(3) #A backtracking algorithm based on a recursive function to try to solve this puzzle! def jumpAround(): for key in pond: if waterlilies[key] != 0: #There is a frog on this waterlily! jumps = pond[key] for jump in jumps: if waterlilies[jump[0]]==0 and waterlilies[jump[1]]>0: #It is possibe to jump on this edge of the graph! so let's jump... waterlilies[jump[0]]=waterlilies[key] waterlilies[key] = 0 waterlilies[jump[1]] = 0 drawPond() drawRipple("lightblue",coordinates[jump[0]][0],coordinates[jump[0]][1]) myPen.getscreen().update() time.sleep(1-SPEED) if checkPond(): #We have a solution! steps.append(str(key) + " -> " + str(jump[0]) ) return True else: #Recursive call! if jumpAround(): steps.insert(0,str(key) + " -> " + str(jump[0]) ) return True else: #Cancel move before checking the next possible move waterlilies[key]=waterlilies[jump[0]] waterlilies[jump[0]] = 0 waterlilies[jump[1]] = GREEN drawPond() drawRipple("red",coordinates[key][0],coordinates[key][1]) drawRipple("red",coordinates[jump[1]][0],coordinates[jump[1]][1]) writeText("black","Backtrack!") myPen.getscreen().update() time.sleep(1-SPEED) drawPond() myPen.getscreen().update() time.sleep(1-SPEED) #After trying every potential move, no solution was found! return False #Final output! steps = [] if jumpAround(): print("Puzzle solved!") writeText("black","Puzzle Solved!") print("Steps / Hops: ") print("\n".join(steps)) myPen.getscreen().update() else: print("This puzzle cannot be solved!") writeText("black","This puzzle cannot be solved!") myPen.getscreen().update()
task_alt