Prolog - 格式化填字游戏

时间:2011-09-12 11:00:30

标签: formatting prolog crossword

我写了一个名为solve_crossword的谓词,如下所示:

solve_crossword(X,C):-
C= [A1,A2,A3,A4,A5,
    B1,' ',B3, ' ', B5,
    C1, C2,C3,C4,C5,
    D1,' ',D3,' ', D5,
    E1,E2,E3,E4,E5],
member([A1, A2, A3, A4, A5], X),
member([C1, C2, C3, C4, C5], X),
member([E1, E2, E3, E4, E5], X),
member([A1, B1, C1, D1, E1], X),
member([A3, B3, C3, D3, E3], X),
member([A5, B5, C5, D5, E5], X).

现在,我想写一个名为write_crossword的谓词来格式化填字游戏。如果我有一个单词列表,我希望它看起来像这样:

| ?- words(X), solve_crossword(X, C), write_crossword(C).

DITCH
O U O
DITTO
G O E
EARLY

C = [[68,73,84,67,72],[79,32,85,32,79],...
X = [[68,73,83,84,82],[68,73,84,67,72],...

words([
       "DISTR",
       "DITCH",
       "DITTO",
       "DITTY",
       "DODGE",
       "EARED",
       "EARLY",
       "EARTH",
       "EASEL",
       "HONOR",
       "HOOEY",
       "HORDE",
       "TUQUE",
       "TURPS",
       "TUTOR",
       "TWAIN"
       ]). 

第1,3,5行和第1,3,5列应该是单词。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情(请注意我更正了您的solve_crossword代码):

solve_crossword(X,C):-
  C= [[A1,A2,A3,A4,A5],
      [B1,Space,B3, Space,B5],
      [C1,C2,C3,C4,C5],
      [D1,Space,D3,Space,D5],
      [E1,E2,E3,E4,E5]],
  atom_codes(' ', [Space]),
  member([A1, A2, A3, A4, A5], X),
  member([C1, C2, C3, C4, C5], X),
  member([E1, E2, E3, E4, E5], X),
  member([A1, B1, C1, D1, E1], X),
  member([A3, B3, C3, D3, E3], X),
  member([A5, B5, C5, D5, E5], X).

write_crossword([]).
write_crossword([Line|Lines]):-
  atom_codes(SLine, Line),
  write(SLine),
  nl,
  write_crossword(Lines).

atom_codes / 2在原子和字符代码列表之间进行转换。