Implement counter prolog -
I am developing a board game in PROLOG and to make the interface more entertaining for the user, I decided to add A grid (0-> 4) with the coordinates as shown below:
0 1 2 3 4 --------- 0 | X x x x x 1 X o o o 2 O x o x x3 X x oo4 O x x x
After developing some code, I came up with an issue about the counter for vertical coordinates when I hit the counter index + 1
, Then the status quits immediately after applying the rule given below:
printgrid: -% writes only horizontal grid ('\ t (write), Write (0), write (''), write (1), write (''), write (2), write (''), write (3), write (''), write (4), nl , Type ('\ t'), Write ('------------------'), nl printbauer ([head | tail]): - index = 0, index index + 1,% stops here - ---------- Write (''), write (index), write ('|'), write (''), print rou (head), printbauer (tail). PrintBoard ([]) Print Rou ([Head | Tail]): - Write (Head), Write (''), Print Row (tail) Print Row ([]): - nl
It is known that resetting the counter will be reset to 0 every time, so I do not know exactly how to set 0 to 0 and how to set it there Area known. Any tips of both to solve problems?
You can not change the value of that variable which is already bound You have to use a new variable, for example, I1 I + 1
, or maybe succ (I, I1)
.
One more thing if you want to use any kind of side effects (printing in your case), then between
in all
loop < / P>
? - Total (center (0, 4, x), format ('~ D ~ N', [X])). 0 1 2 3 4 is true
s:? - Forall (between (0, 5, X),
You definitely have nested
(between total (0, 4, y), format ('~ D- ~ D ', [X, Y]), format (' ~ N '). 0-0 0-1 0-2 0-3 0-4 1-0 0-1-1 1-2 1-3 1-4 2-0 2-1 2-2 2-3 2-4 3-0 3- 1 3-2 3-3 3-4 4-0 4-1 4-2 4-3 4-4 5-0 5-1 5-2 5-3 5-4 Truth
The way to calculate the members of a list, then, must use nth0
, for example,
? - total (nth0 (n, [a, b, c, d], e), format ('~ d- ~ a ~ n', [n, e])). 0-A1-B2-C3-D True
Or, if you do not need the element (when you are marking the column:
? - length ([a, b, c 0, last, x), format ('~ d ~ n', [x])) 0 1 2 3 lane = 4, last = 3 Are ingressing
or nth0
:
? - Kulal (nth0 (n, [a, b, c, d], _), format ('~ d ~ n', [n]). 0 1 2 3 true.
There are other predictions that can offer the same, for example, see findall
and maplist
.
But of course you can reconsider yourself If you remember to I1 I + 1
.
Comments
Post a Comment