# foxes and rabbits - ICON # here's a list of the variables and what they stand for - # LF - number of foxes on the left side of the river # LR - number of rabbits on the left side of the river # RF - number of foxes on the right side of the river # RR - number of rabbits on the right side of the river # B - location of the boat: (-1) is left, (+1) is right # F - number of foxes to send over # R - number of rabbits to send over # NF - new number of foxes to send over # NR - new number of rabbits to send over link graphics procedure main() Window := WOpen("size=500,270") | stop("*** cannot open window") WWrite(Window," Foxes and Rabbits!") every F := 0 to 2 & R := 0 to 2 do { LF := 3 # initialize all variables each time LR := 3 RF := 0 RR := 0 B := -1 if (0 < F+R < 3) then # first move can send at most 2, lst := move(F,R,LF,LR,RF,RR,B) # but in any combination } every l := lst[1 to *lst] do WWrite(Window,l[1]," ",l[2]," ",l[3]) # file := open("output.txt","c") # write(file,lst) WDone(Window) end procedure move(F,R,LF,LR,RF,RR,B) LF +:= F * B # update variables locally LR +:= R * B RF -:= F * B RR -:= R * B B *:= -1 if RF == 3 & RR == 3 then # check if done { lst := [[F,R,B*-1]] return lst } if (LF <= LR | LR == 0) & (RF <= RR | RR == 0) then # if bunnies don't get eaten { if B > 0 then # if heading back to left shore { every NF := 0 to 1 & NR := 0 to 1 do # send at most 1 of each kind if check(F,R,LF,LR,RF,RR,B,NF,NR) & # if move is valid, (NF ~= F | NR ~= R) # and doesn't repeat then { if lst := move(NF,NR,LF,LR,RF,RR,B) then # then get next move { lst:= push(lst,[F,R,B*-1]) return lst # done } } } else # if heading over to right shore { every NF := 2 to 0 by -1 & NR := 2 to 0 by -1 do # try to send greater numbers if check(F,R,LF,LR,RF,RR,B,NF,NR) & # if move is valid (NF ~= F | NR ~= R) # and doesn't repeat then { if lst := move(NF,NR,LF,LR,RF,RR,B) then # then get next move { lst:= push(lst,[F,R,B*-1]) return lst # done } } } } else fail # otherwise function fails end # check for valid move procedure check(F,R,LF,LR,RF,RR,B,NF,NR) if (0 < NF+NR < 3) & # total animals must be between 1 and 2 (0 <= (LF + (NF * B)) <= 3) & # Left Foxes,Left Rabbits, Right Foxes (0 <= (LR + (NR * B)) <= 3) & # and Right Rabbits must all be between (0 <= (RF - (NF * B)) <= 3) & # 0 and 3 for the move to be valid (0 <= (RR - (NR * B)) <= 3) then return # if valid return else fail # otherwise fail end