prolog上的点和盒子

时间:2017-06-15 09:01:08

标签: prolog

我想在Prolog上写点和方框,这里的代码不起作用。我想绘制一个方框并在棋盘上移动。使用" play."它给出了错误,我不知道有错误 代码有什么问题?
我需要在2天内提供此代码,请帮助。

:- use_module(minimax).
:- dynamic(score/2).
:- dynamic(turn/1).

% bestMove(+Pos, -NextPos)
% Compute the best Next Position from Position Pos
% with minimax or alpha-beta algorithm.
bestMove(Pos, NextPos) :-
    minimax(Pos, NextPos, _).

% play
% Start the game.
play :-
    nl,
    write('===================='), nl,
          write('= Dots and Boxes Game ='), nl,
          write('===================='), nl, nl,
          write('Human starts the game'), nl,
                EmptyBoard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            show(EmptyBoard), nl,
                % play ....

% show(+Board)
% Show the board to current output.
show([X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12]) :-
    write(' + '), show2(X1),
    write(' + '), show2(X2),
    write(' + '), nl,
        show2(X3), write('     '), show2(X4), write('  '), show2(X5), nl,
        write(' + '), show2(X6),
    write(' + '), show2(X7),
    write(' + '), nl,
        show2(X8), write('     '), show2(X9), write('  '), show2(X10), nl,
        write(' + '), show2(X11),
    write(' + '), show2(X12),
    write(' + '), nl.

% show2(+Term)
% Write the term to current outupt
% Replace 0 by ' '.
show2(X) :-
    X = 0, !,
    write(' ').

show2(X) :-
    write(X).

% set score to 0       
score(p1,0).
score(p2,0).

updatescore :-
  turn(Player),
  score(Player,S),
  S2 is S+1,
  retract(score(Player,S)),
  assert(score(Player,S2)).

turn(p1).
next(p2).

next :-
  turn(p1),
  retract(turn(p1)) ,
  assert(turn(p2)),
  go.

next :-
  turn(p2),
  retract(turn(p2)),
  assert(turn(p1)),
  go.  

go :- win, !,

  turn(p2), % AI move on P2 turn
  write('AI plays '),
  ai, !,

  show(NextBoard),
  turn(Player),
  write(Player), write(' Human Turn'), nl,
  read(X),
  call(X).

win :-
  finished,
  score(p1,X),
  score(p2,Y),
  win(X,Y).

win(X,Y) :-
  X > Y,
  write('Human wins! '),
  writef('score1=%w score2=%w',[X,Y]), !,
  Y > X,
  write('Computer wins! '),
  writef('score1=%w score2=%w',[X,Y]), !,
  X == Y,
  write('It\' a draw! '),
  writef('score=%w',[X]).

1 个答案:

答案 0 :(得分:0)

我解决了游戏问题。问题。它会在播放前出现%符号...并以“。”结束。 Prolog上的问题。现在我有3 * 3板加上。

play :-
    nl,
    write('===================='), nl,
          write('= Dots and Boxes Game ='), nl,
          write('===================='), nl, nl,
          write('Human starts the game'), nl,
                EmptyBoard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            show(EmptyBoard).

% show(+Board)
% Show the board to current output.
show([X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12]) :-
    write(' + '), show2(X1),
    write(' + '), show2(X2),
    write(' + '), nl,
        show2(X3), write('     '), show2(X4), write('  '), show2(X5), nl,
        write(' + '), show2(X6),
    write(' + '), show2(X7),
    write(' + '), nl,
        show2(X8), write('     '), show2(X9), write('  '), show2(X10), nl,
        write(' + '), show2(X11),
    write(' + '), show2(X12),
    write(' + '), nl.

% show2(+Term)
% Write the term to current outupt
% Replace 0 by ' '.
show2(X) :-
    X = 0, !,
    write(' ').

show2(X) :-
    write(X).

% set score to 0       
score(p1,0).
score(p2,0).

updatescore :-
  turn(Player),
  score(Player,S),
  S2 is S+1,
  retract(score(Player,S)),
  assert(score(Player,S2)).

turn(p1).
next(p2).

next :-
  turn(p1),
  retract(turn(p1)) ,
  assert(turn(p2)),
  go.

next :-
  turn(p2),
  retract(turn(p2)),
  assert(turn(p1)),
  go.  

go :- win, !,

  turn(p2), % AI move on P2 turn
  write('AI plays '),
  ai, !,

  show(NextBoard),
  turn(Player),
  write(Player), write(' Human Turn'), nl,
  read(X),
  call(X).

win :-
  finished,
  score(p1,X),
  score(p2,Y),
  win(X,Y).

win(X,Y) :-
  X > Y,
  write('Human wins! '),
  writef('score1=%w score2=%w',[X,Y]), !,
  Y > X,
  write('Computer wins! '),
  writef('score1=%w score2=%w',[X,Y]), !,
  X == Y,
  write('It\' a draw! '),
  writef('score=%w',[X]).