我希望显示状态 状态([[1,1] [2,2],[5,5]]) $ ???? ?$ ??? ????? ???? $
状态([]) ????? ????? ????? ????? ?????
你能理解吗? 它的尺寸为5x5。但不是固定的。尺寸可以是10x10,6x6,7x7 .... 我怎样才能做到这一点?我是prolog的新手。 实际上,我想要扫雷。我是prolog的新手答案 0 :(得分:0)
:- use_module(library(lists)).
status([[1,2], [3,2]]).
board([
[a, a, a, a, a],
[a, a, a, a, a],
[a, a, a, a, a],
[a, a, a, a, a],
[a, a, a, a, a]
]).
get_coords([X|Y], Coord_X, Coord_Y) :-
Coord_X is X,
Coord_Y is Y.
write_matrix :-
status(Positions),
board(Board),
print_matrix(Board, Positions).
print_matrix(Board, []).
print_matrix(Board, [Position | Next_positions]):-
get_coords(Position, X, Y),
set_piece(Board, X, Y, b, New_board),
display_columns(New_board, 0),
print_matrix(New_board, Next_positions).
display_columns([Row | Next_rows], Indexy) :-
Indexy2 is Indexy + 1,
Indexy2 =< 5, %max columns
display_rows(Row, 0, Indexy2),
display_columns(Next_rows, Indexy2).
display_columns(_, 5):-
nl. %finished columns
display_rows([Spot | Next_spots], Indexx, Indexy) :-
Indexx2 is Indexx + 1,
Indexx2 =< 5, %max rows
(
(Spot = b, write('$'));
(Spot = a, write('?'))
),
display_rows(Next_spots, Indexx2, Indexy).
display_rows(_, 5, _) :-
write(' '). %finished rows
/* Change a piece of the Board, remaking it row by row */
/* If 'Y' is invalid, the same list will be returned */
set_piece([Row | Other_rows], X, Y, New_piece, [Row | Other_new_rows]) :-
Y =\= 1,
Next_Y is Y - 1,
set_piece(Other_rows, X, Next_Y, New_piece, Other_new_rows).
set_piece([Row | Other_rows], X, 1, New_piece, [New_row | Other_new_rows]) :-
set_cell(X, New_piece, Row, New_row),
set_piece(Other_rows, X, 0, New_piece, Other_new_rows).
set_piece([], _X, _Y, _New_piece, []).
/* Change a piece of the Row, remaking it cell by cell */
/* If 'X' is invalid, the same list will be returned */
set_cell(X, New_piece, [Piece | Rest_row], [Piece | Rest_new_row]) :-
X =\= 1,
Next_X is X - 1,
set_cell(Next_X, New_piece, Rest_row, Rest_new_row).
set_cell(1, New_piece, [_Piece | Rest_row], [New_piece | Rest_new_row]) :-
set_cell(0, New_piece, Rest_row, Rest_new_row).
set_cell(_X, _New_piece, [], []).