Prolog(SICStus)中的错误

时间:2012-10-31 21:18:48

标签: prolog prolog-findall

我在prolog程序中遇到问题。 错误在谓词findall内部谓词移动中。任何人都知道我为什么会收到这个错误:

!参数1中的实例化错误> = / 2 !目标:_1123> = 0 %1

代码如下:

moves(Board, Player, BoardList) :-
  change_player(Player, Opposing),
  \+winners(Board, Player, Opposing),
  findall(NewBoard,
          (
            find_piece(Board, Player, 0, 0, XPiece, YPiece),
            get_piece(Board, XPiece, YPiece, Piece),
            validate_move(Board, XPiece, YPiece, XFinal, YFinal, Player,Piece,FinalPiece),
            move(Board, XPiece, YPiece, XFinal, YFinal, Player,Piece,FinalPiece, NewBoard)
          ),
          BoardList).


find_piece([], _, _, _, _, _) :- fail.

find_piece([[piece(_,_,Player)|_]|_], Player, XPiece, YPiece, XPiece, YPiece).

find_piece([[]|Row], Player, _, YCoord, XPiece, YPiece) :-
  NewYCoord is YCoord + 1,
  find_piece(Row, Player, 0, NewYCoord, XPiece, YPiece).

find_piece([[_|Column]|Row], Player, XCoord, YCoord, XPiece, YPiece) :-
  NewXCoord is XCoord + 1,
  find_piece([Column|Row], Player, NewXCoord, YCoord, XPiece, YPiece).

% get_piece/4
% get an piece from board
get_piece(Board, X, Y, Element) :-
  validate_coord(X, Y),
  get_from_board(Board, Y, Row),
  get_from_board(Row, X, Element).

% get_from_board/3
% get something from a certain position of the board
get_from_board([Head|_],0,Head).

get_from_board([Head|Tail],Pos,Elem):-
  Pos \= 0,
  NewPos is Pos - 1,
  get_from_board(Tail,NewPos,Elem).


validate_move(Board,XPiece,YPiece,XPiece,YFinal,Player,Piece,FinalPiece):-
  validate_coord(XPiece,YFinal),
  get_movement_y(YPiece,YFinal),
  piece(_,_,Player)=Piece,
  get_piece(Board,XPiece,YFinal,Elem),
  FinalPiece=Elem,
  piece(_,Value,_)=Piece,
  Value > 0,
  Value < 11,
  \+(piece(_,_,Player)=Elem).


validate_move(Board,XPiece,YPiece,XFinal,YPiece,Player,Piece,FinalPiece):-
  validate_coord(XFinal,YPiece),
  get_movement_x(XPiece,XFinal),
  piece(_,_,Player)=Piece,
  get_piece(Board,XFinal,YPiece,Elem),
  FinalPiece=Elem,
  piece(_,Value,_)=Piece,
  Value > 0,
  Value < 11,
  \+(piece(_,_,Player)=Elem).

% get_movement_y/2
% get and correct the move in y

get_movement_y(YPiece,YDesired):-
  ((YDesired > YPiece )-> (YDesired is YPiece+1)
             ;(YDesired is YPiece -1)).

% get_movement_x/2
% get and correct the move in x
get_movement_x(XPiece,XDesired):-
  ((XDesired > XPiece )-> (XDesired is XPiece+1)
             ;(XDesired is XPiece -1)).

% move/8
% True if NewBoard is the resulting board if the piece belonging to Player
% at (XPiece, YPiece) gets moved to (XFinal, YFinal) in Board
move(Board, XPiece, YPiece, XFinal, YFinal, Player,Piece,piece('   ',12, blk),NewBoard) :-
  get_from_board(Board, YFinal, TempRowFinal),
  set_element_at_index(TempRowFinal, XFinal, Piece, NewRowFinal),
  set_element_at_index(Board, YFinal, NewRowFinal, TempBoard),

  get_from_board(TempBoard, YPiece, TempRowPiece),
  set_element_at_index(TempRowPiece, XPiece, piece('   ',12,blk), NewRowPiece),
  set_element_at_index(TempBoard, YPiece, NewRowPiece, NewBoard).

validate_coord(X, Y) :- 
  X >= 0, 
  X < 10, 
  Y >= 0, 
  Y < 10, 
  \+(forbiden_area(X,Y)).

move(Board, XPiece, YPiece, XFinal, YFinal, Player,Piece,FinalPiece,NewBoard) :-

  clear_screen,
  write('######### CONFRONTO EM CURSO #########'),nl,nl,
  show_battle(Board,XFinal,YFinal,Player,0,0),
  sleep(2),
  clear_screen,
  move_battle(Piece,FinalPiece,PieceToUse),
  get_from_board(Board, YFinal, TempRowFinal),
  set_element_at_index(TempRowFinal, XFinal, PieceToUse, NewRowFinal),
  set_element_at_index(Board, YFinal, NewRowFinal, TempBoard),

  get_from_board(TempBoard, YPiece, TempRowPiece),
  set_element_at_index(TempRowPiece, XPiece, piece('   ',12,blk), NewRowPiece),
  set_element_at_index(TempBoard, YPiece, NewRowPiece, NewBoard).

0 个答案:

没有答案