Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- use_module(utils).
- :- use_module(reversi).
- % play
- % Start the game.
- play :-
- nl,
- write('===================='), nl,
- write('= Reversi ='), nl,
- write('===================='), nl, nl,
- write('Remember : x starts the game'), nl,
- playerMark.
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % playAskColor
- % Ask the color for the human player and start the game with it.
- playerMark:-
- repeat,
- nl, write('Color for human player ? (x or o)'), nl,
- read(Player), nl,
- (Player == o; Player == x),
- %EmptyBoard = [0, 0, 0, 0, 0, 0, 0, 0, 0],
- EmptyBoard = ([
- ["-", "-", "-", "-", "-", "-", "-", "-"],
- ["-", "-", "-", "-", "-", "-", "-", "-"],
- ["-", "-", "-", "-", "-", "-", "-", "-"],
- ["-", "-", "-", o, x, "-", "-", "-"],
- ["-", "-", "-", x, o, "-", "-", "-"],
- ["-", "-", "-", "-", "-", "-", "-", "-"],
- ["-", "-", "-", "-", "-", "-", "-", "-"],
- ["-", "-", "-", "-", "-", "-", "-", "-"]
- ]),
- show(EmptyBoard), nl,
- play([x, play, EmptyBoard], Player).
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % play(+Position, +HumanPlayer)
- % If next player to play in position is equal to HumanPlayer -> Human must play
- % Ask to human what to do.
- play([Player, play, Board], Player) :- !,
- nl, write('Next move ?'), nl,
- read(Pos),
- %read_line_to_codes(user_input,Cs), atom_codes(Pos, Cs), atomic_list_concat(L, ' ', Pos),
- nl, % Ask human where to play
- (
- humanMove([Player, play, Board], [NextPlayer, State, NextBoard], Pos), !, % Verify is move was "possible"
- write("Verify State"),nl,
- verifyState(State,Player,NextPlayer,NextBoard,Player)
- ;
- write('-> Bad Move !'), nl, % If humanMove fail -> bad move
- play([Player, play, Board], Player) % Ask again
- ).
- % play(+Position, +HumanPlayer)
- % If it is not human who must play -> Computer must play
- % Compute the best move for computer with minimax or alpha-beta.
- play([Player, play, Board], HumanPlayer) :-
- nl, write('Computer play : '), nl, nl,
- % Compute the best move
- %bestMove([Player, play, Board], [NextPlayer, State, BestSuccBoard]),
- verifyState(State,Player,NextPlayer,BestSuccBoard,HumanPlayer).
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % When human play
- humanMove([Player1, play, Board], [Player2, State, NextBoard], Pos) :-
- nextPlayer(Player1, Player2),
- set1(Pos, Player1, Board, NextBoard),
- write("about to decide"),nl,
- decide(Player1,NextBoard,State).
- % nextPlayer(X1, X2)
- % True if X2 is the next player to play after X1.
- nextPlayer(o, x).
- nextPlayer(x, o).
- set1(Pos,Player,Board, NextBoard):-
- atom_string(Pos, PosString),
- sub_string(PosString,0,Lenght,1,FirstChar),
- sub_string(PosString,1,Lenght,0,SecondChar),
- setOnBoard(FirstChar,SecondChar,Player,Board, NextBoard),
- write("Board Antiga: "),nl,
- show(Board),nl,
- write("Board Nova: "),nl,
- show(NextBoard).
- switch(X,[Val:Goal|Cases]) :-
- ( X=Val ->
- call(Goal)
- ;
- switch(X,Cases)
- ).
- getIndex(FirstChar,ResIndex):-
- write('getIndex FirstChar: '),write(FirstChar),nl,
- FirstChar = 'a',ResIndex is 0,!,write(FirstChar),write('###'),write(ResIndex);
- FirstChar = 'b',ResIndex is 1,!,write(FirstChar),write('###'),write(ResIndex);
- FirstChar = 'c',ResIndex is 2,!,write(FirstChar),write('###'),write(ResIndex);
- FirstChar = 'd',ResIndex is 3,!,write(FirstChar),write('###'),write(ResIndex);
- FirstChar = 'e',ResIndex is 4,!,write(FirstChar),write('###'),write(ResIndex);
- FirstChar = 'f',ResIndex is 5,!,write(FirstChar),write('###'),write(ResIndex);
- FirstChar = 'g',ResIndex is 6,!,write(FirstChar),write('###'),write(ResIndex);
- FirstChar = 'h',ResIndex is 7,!,write(FirstChar),write('###'),write(ResIndex).
- apagaElementoN([_|H],1,H):- !.
- apagaElementoN([G|H],N,[G|L]):-
- N > 1,
- Nn is N - 1,
- !,
- apagaElementoN(H,Nn,L).
- setOnBoard(FirstChar,SecondChar,Player,Board,BoardFinal):-
- getIndex(FirstChar,FirstCharIndex),
- nl,nl,write("FirstCharIndex: "),write(FirstCharIndex),nl,nl,
- %FirstCharIndex is 1,
- atom_number(SecondChar, SecondCharInteger),
- write('ABS POS: '),write(FirstCharIndex),write(' : '),write(SecondCharInteger),nl,
- validaMovimento(FirstCharIndex,SecondCharInteger,Player,Board),
- NLinha is (SecondCharInteger+1), NColuna is (FirstCharIndex+1),
- nth1(NLinha, Board, LinhaAux),
- apagaElementoN(LinhaAux, NColuna, NovaLinha),
- nth1(NColuna, LinhaFinal, Player, NovaLinha),
- apagaElementoN(Board, NLinha, BoardAux),
- nth1(NLinha, BoardFinal, LinhaFinal, BoardAux), !.
- validaMovimento(X,Y,Player,Board).
- decide(Player,Board,State):-
- verifyFullBoard(Player,Board,State).
- verifyFullBoard(Player, Board,State).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement