Game Tree modulo Symmetry in Prolog

人走茶凉 提交于 2021-02-05 09:34:48

问题


Lets work out a game tree for Tic-Tac-Toe. How would one compute this result in Prolog:

Taking the 8 symmetries into account, and assuming computer (X) starts and plays deterministic, then only 49 table entries are needed!

  • 1 entry for empty board
  • 5 entries for 2 pieces
  • 21 entries for 4 pieces
  • 18 entries for 6 pieces
  • 4 entries for 8 pieces

https://stackoverflow.com/a/61298546/502187


回答1:


It's a vague question with an incorrect premise:

assuming computer (X) starts and plays deterministic

OK

5 entries for 2 pieces

If the X starts in the center (deterministic), then there are only two distinct 2-piece positions:

_ _ _
O X _    
_ _ _

O _ _
_ X _    
_ _ _

HTH




回答2:


Using an adaptation of the algorithm here, I get
a shorter solution with only 37 game positions:

?- init(X), best(X, x, L, W), 
   findall(Z, inside(L, Z), A), 
   sort(A, B), length(B, N).
B = [[[-, -, -], [-, x, -], [-, -, -]], [[x, -, o], 
[-, x, -], [-, -, -]], [[x, -, o], [-, x, x], [-, -, o]], 
[[x, o, -], [-, x, -], [-, -, -]], [[x, o, -], [-, x, -], 
[-, o, x]], [[x, o, -], [-, x, -], [x, -, o]], [[x, o, -], 
[-, x, o], [-, -, x]], [[x, o, -], [-, x, x], [o, -, -]], 
[[x, o, -], [o, x, -], [-, -, x]], [[x, o, o], [-, x, -], 
[-, -, x]], [[x, o, o], [-, x, -], [x, -, -]], [[x, o, o], 
[-, x, o], [x, -, x]], [[x, o, o], [-, x, x], [-, -, -]], 
[[x, o, o], [-, x, x], [o, x, -]], [[x, o, o], [-, x, x], 
[x, o, -]], [[x, o, x], [-, x, -], [o, x, o]], [[x, o, x], 
[-, x, -], [x, o, o]], [[x, o, x], [o, x, -], [o, -, x]], 
[[x, o, x], [o, x, -], [x, -, o]], [[x, x, -], [-, x, o], 
[o, o, x]], [[x, x, -], [-, x, o], [o, x, o]], [[x, x, -], 
[o, x, -], [o, x, o]], [[x, x, -], [o, x, o], [o, -, x]], 
[[x, x, o], [-, x, -], [o, -, -]], [[x, x, o], [-, x, -], 
[o, o, x]], [[x, x, o], [-, x, -], [o, x, o]], [[x, x, o], 
[-, x, o], [-, o, x]], [[x, x, o], [-, x, o], [o, x, -]], 
[[x, x, o], [o, x, -], [-, x, o]], [[x, x, o], [o, x, -], 
[o, x, -]], [[x, x, o], [o, x, o], [-, x, -]], [[x, x, o], 
[o, x, o], [x, o, x]], [[x, x, o], [o, x, x], [o, x, o]], 
[[x, x, o], [o, x, x], [x, o, o]], [[x, x, o], [x, x, o], 
[o, o, x]], [[x, x, x], [o, x, -], [o, -, o]], 
[[x, x, x], [o, x, o], [o, x, o]]],
N = 37 

But since best/4 uses random_member/2 to make the choice
deterministic, by rerunning, one gets also longer results.

Prolog code for the tic-tac-toe game
score via findall, return random winning strategy
reduce number of moves through symmetry
https://gist.github.com/jburse/928f060331ed7d5307a0d3fcd6d4aae9#file-tictac4-pl



来源:https://stackoverflow.com/questions/65783590/game-tree-modulo-symmetry-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!