当我检查数字变量是否大于另一个时,Prolog会冻结

时间:2016-06-03 11:34:28

标签: prolog

我是Prolog的初学者,我试图解决游戏问题

我有一个谓词,用于检查行或列中是否有多个灯光

这是我的代码

lightenRow(I,J,R):-neighbor_row(I,J,RowN),count_lamp(RowN,R1),R is R1.
lightenCol(I,J,R):-neighbor_col(I,J,ColN),count_lamp(ColN,R1),R is R1.
isLighten(I,J):-lightenRow(I,J,R1),lightenCol(I,J,R2),R is R1+R2,R > 0.

lightenRow谓词使用neighbor_row谓词来获取单元格的邻居 然后我用count_lamp谓词得到灯号 我尝试编写结果R1R2并且工作正常

但是当我添加R > 0时 当我拨打isLighten

时,它会冻结并且不会显示任何输出

如果R大于0

,我想要的是真实

编辑:这是我的完整代码

:- dynamic rows/1.
:- dynamic cols/1.
:- dynamic wall/2.
:- dynamic cell/2.
:- dynamic wall_num/2.
:- dynamic light/2.

size(N,M):-
   integer(N),integer(M),assert(rows(N)),assert(cols(M)).
clear_board():-
   retractall(rows(_)),retractall(cols(_)).

rows_count(N):-rows(X),N is X.
cols_count(M):-cols(X),M is X.

inside(I,J):-rows_count(Rows),cols_count(Cols),I=<Rows,I>=1,J=<Cols,J>=1.

add_wall(I,J):-inside(I,J),assert(wall(I,J)).
add_wall_num(I,J,Value):-inside(I,J),assert(wall_num(I,J,Value)).
add_light(I,J):-inside(I,J),assert(light(I,J)).

add_cell_to_list(I,J,Cells,NewCells):-
   inside(I,J),
   TempList=[cell(I,J)],append(TempList,Cells,Result),NewCells=Result
;  append([],Cells,Result2),NewCells=Result2.

neighbor(I,J,Neighbors):-
   Left is J-1, Right is J+1, Top is I-1,Bottom is I+1,
   add_cell_to_list(I,Left,[],N1),
   add_cell_to_list(I,Right,N1,N2),
   add_cell_to_list(Top,J,N2,N3),
   add_cell_to_list(Bottom,J,N3,N4),
   Neighbors=N4.

neighbor_left(I,J,[]):-wall(I,J);wall_num(I,J,_);not(inside(I,J)).
neighbor_left(I,J,List):-
   NJ is J-1,
   neighbor_left(I,NJ,Rest),
   Temp=[cell(I,J)],
   append(Temp,Rest,Result),List=Result.

neighbor_right(I,J,[]):-wall(I,J);wall_num(I,J,_);not(inside(I,J)).
neighbor_right(I,J,List):-
   NJ is J+1,
   neighbor_right(I,NJ,Rest),
   Temp=[cell(I,J)],
   append(Temp,Rest,Result),List=Result.

neighbor_row(I,J,List):-
   LJ is J-1,
   neighbor_left(I,LJ,Result1),
   neighbor_right(I,J,Result2),
   append(Result1,Result2,Result),
   List=Result.

neighbor_up(I,J,[]):-wall(I,J);wall_num(I,J,_);not(inside(I,J)).
neighbor_up(I,J,List):-
   NI is I-1,
   neighbor_up(NI,J,Rest),
   Temp=[cell(I,J)],
   append(Temp,Rest,Result),
   List=Result.

neighbor_down(I,J,[]):-wall(I,J);wall_num(I,J,_);not(inside(I,J)).
neighbor_down(I,J,List):-
   NI is I+1,
   neighbor_down(NI,J,Rest),
   Temp=[cell(I,J)],
   append(Temp,Rest,Result),
   List=Result.

neighbor_col(I,J,List):-
   UI is I-1,
   neighbor_up(UI,J,Result1),
   neighbor_down(I,J,Result2),
   append(Result1,Result2,Result),
   List=Result.

isLight(I,J):-light(I,J).

count_lamp([],0).
count_lamp([cell(I,J)|Rest],Result):-
   isLight(I,J),count_lamp(Rest,Result2),Result is Result2+1.
count_lamp([cell(I,J)|Rest],Result):-
   not(isLight(I,J)),count_lamp(Rest,Result2),Result is Result2.

lightenRow(I,J,R):-neighbor_row(I,J,RowN),count_lamp(RowN,R1),R is R1.
lightenCol(I,J,R):-neighbor_col(I,J,ColN),count_lamp(ColN,R1),R is R1.
isLighten(I,J):-lightenRow(I,J,R1),lightenCol(I,J,R2),R is R1+R2,R > 0.

0 个答案:

没有答案