Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- use_module(library(http/json)).
- main :-
- current_input(Stream),
- json_read_dict(Stream,GameDict),
- close(Stream),
- do_move(GameDict,NewGameDict),
- % get_dict(turn, GameDict, X),
- % print(X),
- % print(BoardString), nl,
- % split_string(BoardString, "|", "", BoardList),
- % print(BoardList), nl,
- % nth0(1,BoardList,X2),
- % print(X2), nl,
- % string_chars(X2,X3),
- % print(X3), nl,
- % nth0(1,X3,X4),
- % print(X4), nl,
- % print("aaa"), nl,
- current_output(OutStream),
- json_write_dict(OutStream, NewGameDict),
- close(OutStream).
- do_move(GameDict, NewGameDict) :-
- GameDict = _{
- turn:Turn,
- size:_{
- columns:ColumnCount,
- rows:RowCount
- },
- board:BoardString,
- variant:Variant,
- capability:Capability,
- status:_{
- winner:Winner,
- finished:Finished
- },
- show_all:ShowAll
- },
- % print(BoardString), nl,
- split_string(BoardString, "|", "", BoardList),
- ColumnCount0 is ColumnCount - 1,
- next_moves(BoardList, NextMoves, ColumnCount0, RowCount, Turn),
- print(NextMoves), nl,
- % print(BoardList), nl,
- NewGameDict = GameDict.
- next_moves(Board, [], -1, _, _).
- next_moves(Board, [Move|Moves], ColumnsLeft, RowCount, Turn) :- %Note: ColumnsLeft is 0-indexed, and must start at 1 less than the amount of columns!
- ColumnsLeft >= 0,
- print("iterate: "), print(ColumnsLeft), nl, %TODO remove duplicate code?
- nth0(ColumnsLeft, Board, Col),
- print(Col), nl,
- string_length(Col, Len),
- print(Len), nl,
- Len < RowCount,
- print("check"), nl,
- string_concat(Col, Turn, NewColumn),
- print(NewColumn), nl,
- replaceAt(Board, ColumnsLeft, NewColumn, Move),
- print(Move), nl,
- NewColumnsLeft is ColumnsLeft - 1,
- print(NewColumnsLeft), nl,
- next_moves(Board, Moves, NewColumnsLeft, RowCount, Turn),
- print("EndIteration"), nl.
- next_moves(Board, Moves, ColumnsLeft, RowCount, Turn) :-
- ColumnsLeft >= 0,
- nth0(ColumnsLeft, Board, Col),
- string_length(Col, Len),
- Len >= RowCount,
- NewColumnsLeft is ColumnsLeft - 1,
- next_moves(Board, Moves, NewColumnsLeft, RowCount, Turn).
- replaceAt([_|T], 0, X, [X|T]).
- replaceAt([H|T], I, X, [H|R]) :- I > 0, I1 is I-1, replaceAt(T, I1, X, R), !.
- replaceAt(L, _, _, L).
- %has_won(BoardList, Player) :-
- % tolistlist(BoardList, BoardListList),
- win_right_from(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft) :-
- length(BoardListList, NumCols),
- Val is NumCols - StartColumn,
- Val >= ChecksLeft,
- win_right_straight(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft);
- win_right_up(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft);
- win_right_down(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft).
- win_right_straight(_, _, _, _, _, 0).
- win_right_straight(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft) :- %TODO more efficient/add !
- % !,
- nth0(StartColumn, BoardListList, Col),
- nth0(StartHeight, Col, El),
- El = Player,
- NewCol is StartColumn + 1,
- NewChecks is ChecksLeft - 1,
- win_right_straight(BoardListList, StartHeight, NewCol, MaxHeight, Player, NewChecks).
- win_right_up(_, _, _, _, _, 0).
- win_right_down(_, _, _, _, _, 0).
- tolistlist([], []).
- tolistlist([H|T],[H2|T2]) :-
- string_chars(H, H2),
- tolistlist(T, T2).
- % Wrong replace - replaces element, not element at index!
- replaceP(_, _, [], []).
- replaceP(O, R, [O|T], [R|T2]) :- replaceP(O, R, T, T2).
- replaceP(O, R, [H|T], [H|T2]) :- dif(H,O), replaceP(O, R, T, T2).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement