Stair-climbing puzzle: Difference between revisions

Fixed Prolog to match requirements
m (minor fix to reporting in Prolog implementation)
(Fixed Prolog to match requirements)
Line 910:
=={{header|Prolog}}==
The robot code is very short
<lang Prolog>step_robotstep_up :- repeat,\+ step, failstep_up, step_up.</lang>
The test program uses a global variable to store the current level and then implements the step behaviour which determines and reports the success of the step and updates the level.
 
This is capped at 20 steps but removing the last line will enable the program to run forever.
<lang Prolog>test_step_robot :-
nb_setval(level, 1),
step_robot.
The test program keeps track of the level in a dynamic predicate.
<lang Prolog>:- dynamic level/1.
setup :-
nb_setvalretractall(level, 1(_)),
assert(level(1)).
step :-
nb_getval(level, (Level),
random_between(0,3,N),
(
Line 928:
succ(NewLevel, Level), format('Fell down to ~d~n', NewLevel)
),
nb_setvalretractall(level, NewLevel(Level)),
assert(level(NewLevel)),
NewLevel >= 20 -> abort.</lang>
N > 0. % Fail if 0 because that is a non step up.</lang>
{{out}}
<pre>
?- setup, between(1,10,_), step_up, fail.
?- test_step_robot.
Climbed up to 2
Climbed up to 3
Line 939 ⟶ 940:
Fell down to 4
Climbed up to 5
Climbed up to 106
Fell down to 95
Climbed up to 6
Climbed up to 7
Climbed up to 8
ClimbedFell updown to 97
Climbed up to 108
false.
Fell down to 9
 
Climbed up to 10
Climbed up to 11
Climbed up to 12
Climbed up to 13
Climbed up to 14
Climbed up to 15
Climbed up to 16
Climbed up to 17
Climbed up to 18
Fell down to 17
Climbed up to 18
Climbed up to 19
Climbed up to 20
% Execution Aborted
?-
</pre>
Anonymous user