Advertisement
Guest User

reversi-game

a guest
Dec 5th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 5.08 KB | None | 0 0
  1. :- use_module(utils).
  2. :- use_module(reversi).
  3.  
  4. % play
  5. % Start the game.
  6. play :-
  7.     nl,
  8.     write('===================='), nl,
  9.     write('=     Reversi    ='), nl,
  10.     write('===================='), nl, nl,
  11.     write('Remember : x starts the game'), nl,
  12.     playerMark.
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14.     % playAskColor
  15.     % Ask the color for the human player and start the game with it.
  16. playerMark:-
  17.       repeat,
  18.       nl, write('Color for human player ? (x or o)'), nl,
  19.       read(Player), nl,
  20.       (Player == o; Player == x),
  21.           %EmptyBoard = [0, 0, 0, 0, 0, 0, 0, 0, 0],
  22.           EmptyBoard =   ([
  23.             ["-", "-", "-", "-", "-", "-", "-", "-"],
  24.             ["-", "-", "-", "-", "-", "-", "-", "-"],
  25.             ["-", "-", "-", "-", "-", "-", "-", "-"],
  26.             ["-", "-", "-", o,   x,   "-", "-", "-"],
  27.             ["-", "-", "-", x,   o,   "-", "-", "-"],
  28.             ["-", "-", "-", "-", "-", "-", "-", "-"],
  29.             ["-", "-", "-", "-", "-", "-", "-", "-"],
  30.             ["-", "-", "-", "-", "-", "-", "-", "-"]
  31.             ]),
  32.  
  33.     show(EmptyBoard), nl,
  34.     play([x, play, EmptyBoard], Player).
  35.  
  36. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  37. % play(+Position, +HumanPlayer)
  38. % If next player to play in position is equal to HumanPlayer -> Human must play
  39. % Ask to human what to do.
  40. play([Player, play, Board], Player) :- !,
  41.     nl, write('Next move ?'), nl,
  42.     read(Pos),
  43.     %read_line_to_codes(user_input,Cs), atom_codes(Pos, Cs), atomic_list_concat(L, ' ', Pos),
  44.     nl,                                  % Ask human where to play
  45.     (
  46.       humanMove([Player, play, Board], [NextPlayer, State, NextBoard], Pos), !, % Verify is move was "possible"
  47.       write("Verify State"),nl,
  48.       verifyState(State,Player,NextPlayer,NextBoard,Player)
  49.       ;
  50.       write('-> Bad Move !'), nl,                % If humanMove fail -> bad move
  51.       play([Player, play, Board], Player)        % Ask again
  52.     ).
  53.  
  54.  
  55. % play(+Position, +HumanPlayer)
  56. % If it is not human who must play -> Computer must play
  57. % Compute the best move for computer with minimax or alpha-beta.
  58. play([Player, play, Board], HumanPlayer) :-
  59.     nl, write('Computer play : '), nl, nl,
  60.     % Compute the best move
  61.     %bestMove([Player, play, Board], [NextPlayer, State, BestSuccBoard]),
  62.     verifyState(State,Player,NextPlayer,BestSuccBoard,HumanPlayer).
  63.  
  64. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  65.  
  66. % When human play
  67. humanMove([Player1, play, Board], [Player2, State, NextBoard], Pos) :-
  68.     nextPlayer(Player1, Player2),
  69.     set1(Pos, Player1, Board, NextBoard),
  70.     write("about to decide"),nl,
  71.     decide(Player1,NextBoard,State).
  72.  
  73. % nextPlayer(X1, X2)
  74. % True if X2 is the next player to play after X1.
  75. nextPlayer(o, x).
  76. nextPlayer(x, o).
  77.  
  78. set1(Pos,Player,Board, NextBoard):-
  79.         atom_string(Pos, PosString),
  80.         sub_string(PosString,0,Lenght,1,FirstChar),
  81.         sub_string(PosString,1,Lenght,0,SecondChar),
  82.         setOnBoard(FirstChar,SecondChar,Player,Board, NextBoard),
  83.         write("Board Antiga: "),nl,
  84.         show(Board),nl,
  85.         write("Board Nova: "),nl,
  86.         show(NextBoard).
  87.  
  88. switch(X,[Val:Goal|Cases]) :-
  89.     ( X=Val ->
  90.       call(Goal)
  91.     ;
  92.       switch(X,Cases)
  93.     ).
  94.  
  95. getIndex(FirstChar,ResIndex):-
  96.       write('getIndex FirstChar: '),write(FirstChar),nl,
  97.       FirstChar = 'a',ResIndex is 0,!,write(FirstChar),write('###'),write(ResIndex);
  98.       FirstChar = 'b',ResIndex is 1,!,write(FirstChar),write('###'),write(ResIndex);
  99.       FirstChar = 'c',ResIndex is 2,!,write(FirstChar),write('###'),write(ResIndex);
  100.       FirstChar = 'd',ResIndex is 3,!,write(FirstChar),write('###'),write(ResIndex);
  101.       FirstChar = 'e',ResIndex is 4,!,write(FirstChar),write('###'),write(ResIndex);
  102.       FirstChar = 'f',ResIndex is 5,!,write(FirstChar),write('###'),write(ResIndex);
  103.       FirstChar = 'g',ResIndex is 6,!,write(FirstChar),write('###'),write(ResIndex);
  104.       FirstChar = 'h',ResIndex is 7,!,write(FirstChar),write('###'),write(ResIndex).
  105.  
  106. apagaElementoN([_|H],1,H):- !.
  107. apagaElementoN([G|H],N,[G|L]):-
  108.     N > 1,
  109.     Nn is N - 1,
  110.     !,
  111.     apagaElementoN(H,Nn,L).
  112.  
  113.  
  114. setOnBoard(FirstChar,SecondChar,Player,Board,BoardFinal):-
  115.       getIndex(FirstChar,FirstCharIndex),
  116.       nl,nl,write("FirstCharIndex:  "),write(FirstCharIndex),nl,nl,
  117.       %FirstCharIndex is 1,
  118.       atom_number(SecondChar, SecondCharInteger),
  119.       write('ABS POS:  '),write(FirstCharIndex),write(' : '),write(SecondCharInteger),nl,
  120.       validaMovimento(FirstCharIndex,SecondCharInteger,Player,Board),
  121.  
  122.       NLinha is (SecondCharInteger+1), NColuna is (FirstCharIndex+1),
  123.  
  124.       nth1(NLinha, Board, LinhaAux),
  125.       apagaElementoN(LinhaAux, NColuna, NovaLinha),
  126.       nth1(NColuna, LinhaFinal, Player, NovaLinha),
  127.       apagaElementoN(Board, NLinha, BoardAux),
  128.       nth1(NLinha, BoardFinal, LinhaFinal, BoardAux), !.
  129.  
  130. validaMovimento(X,Y,Player,Board).
  131.  
  132. decide(Player,Board,State):-
  133.   verifyFullBoard(Player,Board,State).
  134.  
  135. verifyFullBoard(Player, Board,State).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement