Add tags

Add tags to the following comment
#6256(http://ja.doukaku.org/comment/6256/)の続きです

新たに実装したのは23行目以降
nextStates:新しい盤面のリストを作る補助関数
minimaxDecision:ミニマックス戦略
minimaxValue:ミニマックス値を計算する補助関数

マルがミニマックス戦略、バツがランダム・プレーヤーのとき、マルの9954勝0敗46分

Timing[
  result=Table[game[minimaxDecision,randomDecision],{10000}];
  Count[result,#]&/@{1,-1,0}
  ]

{65.656 Second, {9954, 0, 46}}

マルがランダム・プレーヤー、バツがミニマックス戦略のとき、バツの8024勝0敗1976分

Timing[
  result=Table[game[randomDecision,minimaxDecision],{10000}];
  Count[result,#]&/@{1,-1,0}
  ]

{68.484 Second, {0, 8024, 1976}}

マルバツともにミニマックス戦略のときは引き分け

game[minimaxDecision,minimaxDecision]

0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
wins={{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};

isWinner[player_]:=MemberQ[Length[Intersection[player,#]]&/@wins,3]

judge[{p1_,p2_}]:=
  If[isWinner@p1,1,
    If[isWinner@p2,-1,
      If[Length@p1==5,0,Null]]]

operators[state_]:=Complement[Range@9,Flatten@state]

game[decision1_,decision2_]:=Module[{state={{},{}},result=Null},
    While[result===Null,
      If[Length@state[[1]]==Length@state[[2]],
        AppendTo[state[[1]],decision1[Sort/@state]],
        AppendTo[state[[2]],decision2[Sort/@state]]];
      result=judge@state];
    result]

randomDecision[state_]:=With[{x=operators@state},
    x[[Random[Integer,{1,Length@x}]]]]

nextStates[{p1_,p2_},ops_]:=
  Map[If[Length@p1==Length@p2,
      {Append[p1,#],p2}&,{p1,Append[p2,#]}&],ops]

Remove[minimaxDecision];
minimaxDecision[s_]:=
  Module[{ops=operators@s,vals,isMax=Length@s[[1]]==Length@s[[2]]},
    vals=minimaxValue[#,Not@isMax]&/@nextStates[s,ops];
    minimaxDecision[s]=ops[[Position[vals,If[isMax,Max,Min]@vals][[1,1]]]]]

minimaxValue[state_,isMax_]:=Module[{result=judge@state},
    If[result=!=Null,result,
      If[isMax,Max,Min][
        minimaxValue[#,Not@isMax]&/@nextStates[state,operators@state]]]]

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...