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]]]]