Advertisement
Guest User

Untitled

a guest
May 26th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. :- use_module(library(http/json)).
  2.  
  3. main :-
  4. current_input(Stream),
  5. json_read_dict(Stream,GameDict),
  6. close(Stream),
  7. do_move(GameDict,NewGameDict),
  8. % get_dict(turn, GameDict, X),
  9. % print(X),
  10. % print(BoardString), nl,
  11. % split_string(BoardString, "|", "", BoardList),
  12. % print(BoardList), nl,
  13.  
  14. % nth0(1,BoardList,X2),
  15. % print(X2), nl,
  16. % string_chars(X2,X3),
  17. % print(X3), nl,
  18. % nth0(1,X3,X4),
  19. % print(X4), nl,
  20. % print("aaa"), nl,
  21.  
  22. current_output(OutStream),
  23. json_write_dict(OutStream, NewGameDict),
  24. close(OutStream).
  25.  
  26. do_move(GameDict, NewGameDict) :-
  27. GameDict = _{
  28. turn:Turn,
  29. size:_{
  30. columns:ColumnCount,
  31. rows:RowCount
  32. },
  33. board:BoardString,
  34. variant:Variant,
  35. capability:Capability,
  36. status:_{
  37. winner:Winner,
  38. finished:Finished
  39. },
  40. show_all:ShowAll
  41. },
  42.  
  43. % print(BoardString), nl,
  44. split_string(BoardString, "|", "", BoardList),
  45. ColumnCount0 is ColumnCount - 1,
  46. next_moves(BoardList, NextMoves, ColumnCount0, RowCount, Turn),
  47. print(NextMoves), nl,
  48. % print(BoardList), nl,
  49. NewGameDict = GameDict.
  50.  
  51. next_moves(Board, [], -1, _, _).
  52. next_moves(Board, [Move|Moves], ColumnsLeft, RowCount, Turn) :- %Note: ColumnsLeft is 0-indexed, and must start at 1 less than the amount of columns!
  53. ColumnsLeft >= 0,
  54. print("iterate: "), print(ColumnsLeft), nl, %TODO remove duplicate code?
  55. nth0(ColumnsLeft, Board, Col),
  56. print(Col), nl,
  57. string_length(Col, Len),
  58. print(Len), nl,
  59. Len < RowCount,
  60. print("check"), nl,
  61. string_concat(Col, Turn, NewColumn),
  62. print(NewColumn), nl,
  63. replaceAt(Board, ColumnsLeft, NewColumn, Move),
  64. print(Move), nl,
  65. NewColumnsLeft is ColumnsLeft - 1,
  66. print(NewColumnsLeft), nl,
  67. next_moves(Board, Moves, NewColumnsLeft, RowCount, Turn),
  68. print("EndIteration"), nl.
  69. next_moves(Board, Moves, ColumnsLeft, RowCount, Turn) :-
  70. ColumnsLeft >= 0,
  71. nth0(ColumnsLeft, Board, Col),
  72. string_length(Col, Len),
  73. Len >= RowCount,
  74. NewColumnsLeft is ColumnsLeft - 1,
  75. next_moves(Board, Moves, NewColumnsLeft, RowCount, Turn).
  76.  
  77.  
  78. replaceAt([_|T], 0, X, [X|T]).
  79. replaceAt([H|T], I, X, [H|R]) :- I > 0, I1 is I-1, replaceAt(T, I1, X, R), !.
  80. replaceAt(L, _, _, L).
  81.  
  82. %has_won(BoardList, Player) :-
  83. % tolistlist(BoardList, BoardListList),
  84.  
  85. win_right_from(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft) :-
  86. length(BoardListList, NumCols),
  87. Val is NumCols - StartColumn,
  88. Val >= ChecksLeft,
  89. win_right_straight(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft);
  90. win_right_up(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft);
  91. win_right_down(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft).
  92.  
  93. win_right_straight(_, _, _, _, _, 0).
  94. win_right_straight(BoardListList, StartHeight, StartColumn, MaxHeight, Player, ChecksLeft) :- %TODO more efficient/add !
  95. % !,
  96. nth0(StartColumn, BoardListList, Col),
  97. nth0(StartHeight, Col, El),
  98. El = Player,
  99. NewCol is StartColumn + 1,
  100. NewChecks is ChecksLeft - 1,
  101. win_right_straight(BoardListList, StartHeight, NewCol, MaxHeight, Player, NewChecks).
  102.  
  103. win_right_up(_, _, _, _, _, 0).
  104.  
  105. win_right_down(_, _, _, _, _, 0).
  106.  
  107.  
  108.  
  109.  
  110. tolistlist([], []).
  111. tolistlist([H|T],[H2|T2]) :-
  112. string_chars(H, H2),
  113. tolistlist(T, T2).
  114.  
  115. % Wrong replace - replaces element, not element at index!
  116. replaceP(_, _, [], []).
  117. replaceP(O, R, [O|T], [R|T2]) :- replaceP(O, R, T, T2).
  118. 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