with(combinat): Help:=proc(): print(`CombGames.txt: a Maple package for simulating and studying generalized Okey`): print(``): if nargs=0 then print(`The main procedures are: `): print(` RandomCG(n,k), TicTacToe(k), TicTacToeD(k), SimuStatDuration(G,n,k,N,t), SimuStatWin(G,n,k,N), SimulateGameV(G,n,k), SimulateGameT(G,n,k), SimulateTTTT(k), SimulateTTTDV(k), SimulateTTTDT(k), SimuStatDurationTTT(k,N,t), SimuStatWinTTT(k,N), SimuStatDurationTTTD(k,N,t), SimuStatWinTTTD(k,N), Oper1(G, i, X), Oper(G,X), ApplyOper(T,f,X), EvolutionGameT(G, n, k, N,x), EvolutionGameV(G, n, k , N,x), EvolutionGameVTTT(k,N,x), EvolutionGameT(G,n,k,N,x), EvolutionGameTTTT(k,N,x), GenFunDuration(G,k,N,x), GenFunWin(G,n,k,N,x,r), GenFunDurationTTT(k,N,x), GenFunWinTTT(k, N, x, r)`): elif nargs=1 and args[1] = RandomCG then print (`RandomCG(n,k) generates a Random Game with random rules, it has n states and k players..`): print (`It's output is of the format [IS (initial state), WinStates (Table of win states for player k, and WinStates[0] are tie states, and Ta (table, where Ta[i][1] is the states and Ta[i][2] is a table with states reachable from x in Ta[i][2][x] )]`): print(`For example, try RandomCG(45, 4)`): #inputs X (a formal variable), T (an operator in your data structure as a list of length n:=nops(T) of linear combinations of X[i]'s i=1..n) # and f (a linear combination of the X[i]'s) elif nargs = 1 and args[1] = ApplyOper then print(`ApplyOper(T,f,X) inputs X (a formal variable), f (a linear combination of states), T(an operator in your data structure as a list of length n:=nops(T) of linear combinations of X[i]'s i=1..n)`): print(`Outputs A linear combination of X[i] (i=1..n) obtained by replacing X[i] in f by T[i]`): print(`For example, try RG := RandomCG(15, 4): T := Oper(RG, X): f := T[RG[1]]: LC := ApplyOper(T, f, X):`): elif nargs=1 and args[1] = TicTacToe then print(`TicTacToe(k) generates a TicTacToe Game's rules in the format [IS (initial state), WinStates (Table of win states for player k, and WinStates[0] are tie states, and Ta (table, where Ta[i][1] is the states and Ta[i][2] is a table with states reachable from x in Ta[i][2][x] )]`): print(`This version only counts k in a row columns or rows.`): print(`For example, try TicTacToe(2)`): elif nargs=1 and args[1] = TicTacToeD then print(`TicTacToeD(k) generates a TicTacToe Game's rules in the format [IS (initial state), WinStates (Table of win states for player k, and WinStates[0] are tie states, and Ta (table, where Ta[i][1] is the states and Ta[i][2] is a table with states reachable from x in Ta[i][2][x] )]`): print(`This version counts k in a row columns, rows, and diagonals.`): print(`For example, try TicTacToeD(2)`): elif nargs =1 and args[1] = SimuStatDuration then print(`A tool to experiment the duration of a random game.`): print(`For example, try SimuStatDuration(RandomCG(45, 4), 45, 4, 100, t)`): elif nargs =1 and args[1] = SimuStatWin then print(`A tool to find statistics of the winner of a random game.`): print(`For example, try SimuStat(RandomCG(45, 4), 45, 4, 100)`): elif nargs =1 and args[1] = SimuStatDurationTTT then print(`A tool to experiment the duration of a game of TicTacToe.`): print(`For example, try SimuStatDurationTTT(2, 100, t)`): elif nargs =1 and args[1] = SimuStatWinTTT then print(`A tool to find statistics of the winner of a game of TicTacToe.`): print(`For example, try SimuStatWinTTT(2, 100)`): elif nargs =1 and args[1] = SimuStatDurationTTTD then print(`A tool to experiment the duration of a game of TicTacToe with Diagonals.`): print(`For example, try SimuStatDurationTTTD(2, 100, t)`): elif nargs =1 and args[1] = SimuStatWinTTTD then print(`A tool to find statistics of the winner of a game of TicTacToe with Diagonals.`): print(`For example, try SimuStatWinTTTD(2, 100)`): elif nargs=1 and args[1] = SimulateGameV then print(`SimulateGameV(G,n,k) simulates a Game G, with n states and k players.. `): print(`This version of SimulateGame is verbose and outputs every step of the game. For the terse version try SimulateGameT.`): print(`It's output is of the format [Winner, Game Duration]`): print(`For example, try G := RandomCG(45, 4): SimulateGameV(G, 45, 4):`): elif nargs=1 and args[1] = SimulateGameT then print(`SimulateGameT(G,n,k) simulates a Game G, with n states and k players.. `): print(`This version of SimulateGame is terse and only outputs the end result. For the verbose version try SimulateGameV.`): print(`It's output is of the format [Winner, Game Duration]`): print(`For example, try G := RandomCG(45, 4): SimulateGameT(G, 45, 4):`): elif nargs=1 and args[1] = SimulateTTTV then print(`SimulateTTTV(k) simulates a game of k x k TicTacToe and outputs every step of the game.`): print(`The output is of the format [Winner, Game Duration]`): print(`For example, try SimulateTTTV(2).`): elif nargs=1 and args[1] = SimulateTTTT then print(`SimulateTTTV(k) simulates a game of k x k TicTacToe and only outputs the end result.`): print(`The output is of the format [Winner, Game Duration]`): print(`For example, try SimulateTTTT(2).`): elif nargs=1 and args[1] = SimulateTTTDV then print(`SimulateTTTDV(k) simulates a game of k x k TicTacToe with Diagonals and outputs every step of the game.`): print(`The output is of the format [Winner, Game Duration]`): print(`For example, try SimulateTTTDV(2).`): elif nargs=1 and args[1] = SimulateTTTDV then print(`SimulateTTTDT(k) simulates a game of k x k TicTacToe with Diagonals and only outputs the end result.`): print(`The output is of the format [Winner, Game Duration]`): print(`For example, try SimulateTTTDT(2).`): elif nargs =1 and args[1] = Oper1 then print(`Oper1(G, i, X) inputs a game G and state i, and symbol X and outputs a linear combination (X[j1]+X[j2]+...+X[jk])/k `): print(`Where [j1,j2,...,jk] are the possible states reachable from state i.`): print(`For example, try RG:= RandomCG(45,5): Oper1(RG, 20, X);`): elif nargs = 1 and args[1] = Oper then print(`Oper(G,X) inputs a game G and a symbol X, and outputs [seq(Oper1(G,i, X), i = 1..n)] for all states from 1 to n in G.`): print(`For example, try RG:= RandomCG(45,5): Oper(RG, X);`): elif nargs = 1 and args[1] = EvolutionGameT then print(`EvolutionGameT(G, n, k, N,x) will return a table of all the probabilities that player i in T[i] will win on a given turn up to N. For every T[j], the coefficient p in p*x^i represents the probability of player j winning on move i`): print(`G is a random game on n states and k players, N is the number of rounds, and x is a formal variable for the table.`): print(` For example, try G:= RandomCG(45,4): EvolutionGameT(G, 45, 4, 3,x):`): print(`This is the terse version`): elif nargs = 1 and args[1] = EvolutionGameV then print(`EvolutionGameV(G, n, k, N,x) will return a table of all the probabilities that player i in T[i] will win on a given turn up to N. For every T[j], the coefficient p in p*x^i represents the probability of player j winning on move i`): print(`G is a random game on n states and k players, N is the number of rounds, and x is a formal variable for the table.`): print(` For example, try G:= RandomCG(45,4): EvolutionGameV(G, 45, 4, 3,x):`): print(`This is the verbose version`): elif nargs = 1 and args[1] = EvolutionGameVTTT then print(`EvolutionGameVTTT(k,N,x) for a game of TicTacToe will return a table of all the probabilities that player i in T[i] will win on a given turn up to N. For every T[j], the coefficient p in p*x^i represents the probability of player j winning on move i`): print(`k is the dimensions of the TicTacToe board, N is the number of rounds, and x is a formal variable for the table.`): print(` For example, try EvolutionGameVTTT(3, 3,x):`): print(`This is the verbose version`): elif nargs = 1 and args[1] = EvolutionGameTTTT then print(`EvolutionGameTTTT(k,N,x) for a game of TicTacToe will return a table of all the probabilities that player i in T[i] will win on a given turn up to N. For every T[j], the coefficient p in p*x^i represents the probability of player j winning on move i`): print(`k is the dimensions of the TicTacToe board, N is the number of rounds, and x is a formal variable for the table.`): print(` For example, try EvolutionGameTTTT(3, 3,x):`): print(`This is the terse version`): elif nargs = 1 and args[1] = GenFunDuration then print(`GenFunDuration(G,k,N,x) for a game G gets the poly. of degree<=N whose meaning is the prob. that the game ends exactly after i rounds.`): print(`G is a random game, k is the number of players, N is the number of rounds and x is a formal variable.`): print(`For example, try G:= RandomCG(45,4): GenFunDuration(G, 4, 3,x):`): elif nargs = 1 and args[1] = GenFunWin then print(`GenFunWin(G,n,k,N,x,r) where r is either 0 or 1,...,k, returns a polynomial of degree<=N whose coeff. of x^i is the prob. that player r won after exactly i moves.`): print(`G is a random game on n states and k players, N is the number of rounds, and x is a formal variable, and r is the player.`): print(`For example, try G:= RandomCG(45,4): GenFunDuration(G, 45, 4, 3,x, 2):`): elif nargs = 1 and args[1] = GenFunDurationTTT then print(`GenFunDurationTTT(k,N,x) for a game of TicTacToe gets the poly. of degree<=N whose meaning is the prob. that the game ends exactly after i rounds.`): print(`k is the dimensions of the TicTacToe board, N is the number of rounds and x is a formal variable.`): print(`For example, try GenFunDurationTTT(3, 3,x):`): elif nargs = 1 and args[1] = GenFunWinTTT then print(`GenFunWinTTT(k, N, x, r) for a game of TicTacToe when r is 0, 1, or 2 returns a polynomial of degree<=N whose coeff. of x^i is the prob. that player r won after exactly i moves.`): print(`k is the dimensions of the TicTacToe board, N is the number of rounds, x is a formal variable, and r is the player.`): print(`For example, try GenFunWinTTT(3, 3,x,2):`): fi: end: # k players, N states # Questions: # Question 1: given r, k, s, gs values you only get a set number of states. #You can't necessarily start at N states and determine which set of states have N states and work backwards # should we change this N? or do we just have 1 to N states as fake states without knowing what they really are? # Question 2: For the edges, or the probabilities, we would need to know what the actual states are in order to see #if a valid transition exists between the states. In order to do this, r, k, s, gs values are definitely needed #Question 3: What is randomized about the game, if r,k, s, gs values are set? #Question 4: for EN1_, ..., EN_k , I'm a little condused. Basically They're all connected components in our new graph? # Follow up Question 5: If EN1..ENk are all connected components, then we can't lump all the terminal states in one and need them all as seperate states, correct? RandomCG:= proc (n,k): local States, IS, Ta, PlayerStates, i, StatePerm, PlayerState, j, Reach, WinStates, leftoverStates, N: if(n<=(k)) then print(`Sorry, not enough states for all players!`): return(FAIL): fi: #Initial state is always n-1th state N:= n-1: States:= [seq(i, i = 1..n)]: IS:= States[rand(1..n)()]: States:= subsop(IS=NULL, States): StatePerm:= randperm(States): for i from 1 to k do PlayerState:= [seq(StatePerm[j], j = ((i-1)* (trunc(N/k)) + 1 ).. (((i-1)* (trunc(N/k)) ) + (trunc(N/k))) )]: PlayerStates[i] := PlayerState od: PlayerStates[1]:= [op(PlayerStates[1]), IS]: #randomly places any left over tiles. if(N mod k > 0 ) then for j from (k*trunc(N/k)+1) to N do i:= rand(1..k)(): PlayerStates[i]:= [op(PlayerStates[i]), StatePerm[j]]: od: fi: # Now for each state in PlayerStates[i], make a set of reachable states. # Reach[i][x] := set of states # The rules of this random game are unclear, so we can pick any x random states as reachable? for i from 1 to k do for j in PlayerStates[i] do Reach[i][j]:= randcomb(States, rand(1..N)()): od: od: for i from 1 to k do Ta[i] := [PlayerStates[i], eval(Reach[i])]: od: for i from 1 to k do WinStates[i]:= randcomb(Ta[i][1], rand(1..nops(Ta[i][1]))() ): od: i:=1: if(member(IS, WinStates[1])) then while WinStates[1][i] <> IS do i:= i+1: od: WinStates[1] := subsop(i=NULL, WinStates[1]): fi: leftoverStates:= {seq(States[i], i= 1..N)}: for i from 1 to k do leftoverStates := leftoverStates minus {seq(WinStates[i][j], j = 1.. nops(WinStates[i] ) ) }: od: if(nops(leftoverStates) > 0) then WinStates[0] := [ op( randcomb(leftoverStates, rand(1..nops(leftoverStates))() ) ) ]: else WinStates[0] := []: fi: #i:= rand(1..k)(): #Ta[i][1] := [op(Ta[i][1]), IS]: [IS, eval(WinStates), eval(Ta)]: end: SimulateGameV:= proc(G, s, p) local newstr,i,ps, IS,t,st, WinStates, Table, duration, StoP,cp,state: IS:= G[1]: WinStates:= G[2]: Table:= G[3]: print(cat("Let's start this random game with ", p, " players!")): StoP:= Array([0$(s)]): # First create a vector of state numbers, which have the player that has that state in it. i:= 1: for i from 1 to p do: for st in Table[i][1] do: StoP[st] := i: od: od: StoP: # Starts off with the initial state, this is always player 1. cp:= 1: duration:= 0: state:= IS: while( not (member(state, WinStates[0]) or member(state, WinStates[cp])) ) do: print(cat("Player ", cp, " is currently in state ", state)): newstr:= cat("The state and player has switched from state ", state, " and player ", cp, " to state "): state:= Table[cp][2][state][rand(1..nops(Table[cp][2][state]))()]: cp:= StoP[state]: print(cat(newstr, state, " and player ", cp, ".")): duration:= duration+1: od: if(member(state,WinStates[0])) then print(cat("The game has resulted in a tie, and took ", duration, " move(s).")): RETURN([0, duration]): else print(cat("Player ", cp, " has won the game in ", duration," moves.")): RETURN([cp, duration]): fi: end: SimulateGameT:= proc(G, s, p) local i,ps, IS,t,st, WinStates, Table, duration, StoP,cp,state: IS:= G[1]: WinStates:= G[2]: Table:= G[3]: StoP:= Array([0$(s)]): # First create a vector of state numbers, which have the player that has that state in it. i:= 1: for i from 1 to p do: for st in Table[i][1] do: StoP[st] := i: od: od: StoP: # Starts off with the initial state, this is always player 1. cp:= 1: duration:= 0: state:= IS: while( not (member(state, WinStates[0]) or member(state, WinStates[cp])) ) do: state:= Table[cp][2][state][rand(1..nops(Table[cp][2][state]))()]: cp:= StoP[state]: duration:= duration+1: od: if(member(state,WinStates[0])) then RETURN([0, duration]): else RETURN([cp, duration]): fi: end: SimuStatDuration:=proc(G,n,k,N,t): add(t^SimulateGameT(G,n,k)[2],t=1..N)/N: end: SimuStatWin:=proc(G,n,k,N) local t,f,i: f:=evalf(add(t^SimulateGameT(G,n,k)[1],i=1..N)/N): [coeff(f,t,0),seq(coeff(f,t,i),i=1..k)]: end: WinTTT:= proc(s, currPl) local k,r,i,j: k:= nops(s): for i from 1 to k do: if s[i] = [currPl $ k] then return true: fi: od: for i from 1 to k do if [seq(s[j][i], j = 1..k)] = [currPl $ k] then return true: fi: od: return false: end: WinTTTD:= proc(s, currPl) local k,r,i,j: k:= nops(s): for i from 1 to k do: if s[i] = [currPl $ k] then return true: fi: od: for i from 1 to k do if [seq(s[j][i], j = 1..k)] = [currPl $ k] then return true: fi: od: if ([seq(s[i][i], i=1..k)] = [currPl $ k] )or ([seq(s[i][k-i+1], i = 1..k)] = [currPl $ k]) then return true: fi: return false: end: TransSetTTT:= proc(s, currPl) local k, tSet, r, i, j: k:=nops(s): tSet:= {}: for i from 1 to k do for j from 1 to k do if s[i][j] = 0 then tSet:= tSet union {[seq(s[r], r = 1 .. (i-1)), subsop(j = (currPl ), s[i]), seq(s[r], r = (i+1) ..k)]}: fi: od: od: tSet: end: TicTacToe:= proc(k) local StateT, IS, currSet, prevSet, currPl, WinSetT, WinT, StateSet, Transitions, i, j, children, s: option remember: StateSet:= [{}, {}]: Transitions:= [table(), table()]: #The initial state is a k by k board of zeros (no matter what) IS:= [seq([seq(0, j = 1..k)], i=1..k)]: #create a set of the first player's states, build on that set for each player. currSet:= {IS}: currPl:= 1: #Win sets for ties, player 1 wins and player 2 wins. WinSetT[0]:= {}: WinSetT[1]:={}: WinSetT[2]:={}: # for every set of states at a certain stage for a certain player, you can create a new set of possible states for the next player # add the set of ALREADY ADVANCED states to the set of states of the player now # make that set the "previous set" and reset the currSet #for every state in prevSet, check if the player giving the current player that state won by transitioning to the state # if not, the current player can make a move. while nops(currSet) > 0 do StateSet[currPl]:= StateSet[currPl] union currSet: prevSet:= currSet: currSet:= {}: for s in prevSet do #Since the previous player made a move, if they won on their turn then they won and there are no transition states for that state. if(WinTTT(s, (currPl mod 2) + 1) ) then WinSetT[(currPl mod 2) + 1]:= WinSetT[(currPl mod 2) + 1] union {s}: else children:= TransSetTTT(s, currPl): if(nops(children) = 0) then WinSetT[0] := WinSetT[0] union {s}: else Transitions[currPl][s]:= children: currSet:= currSet union children: fi: fi: od: currPl:= (currPl mod 2) + 1: od: for i from 0 to 2 do WinT[i] := [op(WinSetT[i])]: od: StateT:= table(): StateT[1]:= [[], table()]: StateT[2]:=[[],table()]: StateT[1][1] := [op(StateSet[1])]: StateT[2][1] := [op(StateSet[2])]: for s in StateT[1][1] do: StateT[1][2][s] := [op(Transitions[1][s])]: od: for s in StateT[2][1] do: StateT[2][2][s] := [op(Transitions[2][s])]: od: [IS, eval(WinT), eval(StateT)]: # TicTacToe always has 2 players (n) # since x goes first, all possible initial states include one x on the k by k board. # All possible states include b x's and b-1 or b o's end: TicTacToeD:= proc(k) local StateT, IS, currSet, prevSet, currPl, WinSetT, WinT, StateSet, Transitions, i, j, children, s: StateSet:= [{}, {}]: Transitions:= [table(), table()]: IS:= [seq([seq(0, j = 1..k)], i=1..k)]: currSet:= {IS}: currPl:= 1: WinSetT[0]:= {}: WinSetT[1]:={}: WinSetT[2]:={}: while nops(currSet) > 0 do StateSet[currPl]:= StateSet[currPl] union currSet: prevSet:= currSet: currSet:= {}: for s in prevSet do #Since the previous player made a move, if they won on their turn then they won and there are no transition states for that state. if(WinTTTD(s, (currPl mod 2) + 1) ) then WinSetT[(currPl mod 2) + 1]:= WinSetT[(currPl mod 2) + 1] union {s}: else children:= TransSetTTT(s, currPl): if(nops(children) = 0) then WinSetT[0] := WinSetT[0] union {s}: else Transitions[currPl][s]:= children: currSet:= currSet union children: fi: fi: od: currPl:= (currPl mod 2) + 1: od: for i from 0 to 2 do WinT[i] := [op(WinSetT[i])]: od: StateT:= table(): StateT[1]:= [[], table()]: StateT[2]:=[[],table()]: StateT[1][1] := [op(StateSet[1])]: StateT[2][1] := [op(StateSet[2])]: for s in StateT[1][1] do: StateT[1][2][s] := [op(Transitions[1][s])]: od: for s in StateT[2][1] do: StateT[2][2][s] := [op(Transitions[2][s])]: od: [IS, eval(WinT), eval(StateT)]: # TicTacToe always has 2 players (n) # since x goes first, all possible initial states include one x on the k by k board. # All possible states include b x's and b-1 or b o's end: SimulateTTTV:= proc(k) local G, newstr,i,ps, IS,t,st, WinStates, Table, duration, cp,state,n: # Fine the number of states total # the number of players is 2 # then, use mostly the same logic as Simulate Game but instead of counting the winning states as what you encounter, make them the states you transition to. G:= TicTacToe(k): n:= nops(G[3][1][1]) + nops(G[3][2][1]): IS:= G[1]: WinStates:= G[2]: Table:= G[3]: print(cat("Let's start this game of TicTacToe!")): # Starts off with the initial state, this is always player 1. cp:= 1: duration:= 0: state:= IS: while( not (member(state, WinStates[0]) or member(state, WinStates[cp])) ) do: print(cat("Player ", cp, " has started their turn.")): newstr:= cat("The state and player has switched from state ", state, " to state "): state:= Table[cp][2][state][rand(1..nops(Table[cp][2][state]))()]: print(cat(newstr, state,".")): duration:= duration+1: if(member(state,WinStates[0])) then print(cat("The game has resulted in a tie, and took ", duration, " move(s).")): RETURN([0, duration]): elif(member(state, WinStates[cp])) then print(cat("Player ", cp, " has won the game in ", duration," moves.")): RETURN([cp, duration]): fi: cp := (cp mod 2) + 1: od: if member(state,WinStates[0]) then print(cat("The game has resulted in a tie, and took ", duration, " move(s).")): RETURN([0, duration]): else print(cat("Player ", cp, " has won the game in ", duration," moves.")): RETURN([cp, duration]): fi: end: SimulateTTTT:= proc(k) local G, newstr,i,ps, IS,t,st, WinStates, Table, duration, cp,state,n: # Fine the number of states total # the number of players is 2 # then, use mostly the same logic as Simulate Game but instead of counting the winning states as what you encounter, make them the states you transition to. G:= TicTacToe(k): n:= nops(G[3][1][1]) + nops(G[3][2][1]): IS:= G[1]: WinStates:= G[2]: Table:= G[3]: # Starts off with the initial state, this is always player 1. cp:= 1: duration:= 0: state:= IS: while( not (member(state, WinStates[0]) or member(state, WinStates[cp])) ) do: state:= Table[cp][2][state][rand(1..nops(Table[cp][2][state]))()]: duration:= duration+1: if(member(state,WinStates[0])) then RETURN([0, duration]): elif(member(state, WinStates[cp])) then RETURN([cp, duration]): fi: cp := (cp mod 2) + 1: od: if member(state,WinStates[0]) then RETURN([0, duration]): else RETURN([cp, duration]): fi: end: SimulateTTTDV:= proc(k) local G, newstr,i,ps, IS,t,st, WinStates, Table, duration, cp,state,n: # Fine the number of states total # the number of players is 2 # then, use mostly the same logic as Simulate Game but instead of counting the winning states as what you encounter, make them the states you transition to. G:= TicTacToeD(k): n:= nops(G[3][1][1]) + nops(G[3][2][1]): IS:= G[1]: WinStates:= G[2]: Table:= G[3]: print(cat("Let's start this game of TicTacToe!")): # Starts off with the initial state, this is always player 1. cp:= 1: duration:= 0: state:= IS: while( not (member(state, WinStates[0]) or member(state, WinStates[cp])) ) do: print(cat("Player ", cp, " has started their turn.")): newstr:= cat("The state has switched from state ", state, " to state "): state:= Table[cp][2][state][rand(1..nops(Table[cp][2][state]))()]: print(cat(newstr, state,".")): duration:= duration+1: if(member(state,WinStates[0])) then print(cat("The game has resulted in a tie, and took ", duration, " move(s).")): RETURN([0, duration]): elif(member(state, WinStates[cp])) then print(cat("Player ", cp, " has won the game in ", duration," moves.")): RETURN([cp, duration]): fi: cp := (cp mod 2) + 1: od: if member(state,WinStates[0]) then print(cat("The game has resulted in a tie, and took ", duration, " move(s).")): RETURN([0, duration]): else print(cat("Player ", cp, " has won the game in ", duration," moves.")): RETURN([cp, duration]): fi: end: SimulateTTTDT:= proc(k) local G, newstr,i,ps, IS,t,st, WinStates, Table, duration, cp,state,n: # Fine the number of states total # the number of players is 2 # then, use mostly the same logic as Simulate Game but instead of counting the winning states as what you encounter, make them the states you transition to. G:= TicTacToeD(k): #There's only two players in TicTacToe: n:= nops(G[3][1][1]) + nops(G[3][2][1]): IS:= G[1]: WinStates:= G[2]: Table:= G[3]: cp:= 1: duration:= 0: state:= IS: while( not (member(state, WinStates[0]) or member(state, WinStates[cp])) ) do: state:= Table[cp][2][state][rand(1..nops(Table[cp][2][state]))()]: duration:= duration+1: if(member(state,WinStates[0])) then RETURN([0, duration]): elif(member(state, WinStates[cp])) then RETURN([cp, duration]): fi: cp := (cp mod 2) + 1: od: if member(state,WinStates[0]) then RETURN([0, duration]): else RETURN([cp, duration]): fi: end: SimuStatDurationTTT:=proc(k,N,t) local i: add(t^SimulateTTTT(k)[2],i=1..N)/N: end: SimuStatWinTTT:=proc(k,N) local t,f,i: f:=evalf(add(t^SimulateTTTT(k)[1],i=1..N)/N): [coeff(f,t,0),seq(coeff(f,t,i),i=1..k)]: end: SimuStatDurationTTTD:=proc(k,N,t) local i: add(t^SimulateTTTDT(k)[2],i=1..N)/N: end: SimuStatWinTTTD:=proc(k,N) local t,f,i: f:=evalf(add(t^SimulateTTTDT(k)[1],i=1..N)/N): [coeff(f,t,0),seq(coeff(f,t,i),i=1..k)]: end: #Write a procedure #Oper1(G,i,X) (G is a game and i is state) #Let [j1,j2,...,jk] be the possible states reachable from state i #the ouput is a linear combination #(X[j1]+X[j2]+...+X[jk])/k Oper1:= proc(G, i, X) local Table, j, k, Reach, r: #First unpack G's Table Table:= G[3]: k := 0: while type(Table[k+1], list) do k := k + 1: od: j:= 1: while j <= k and (not evalb(i in Table[j][1])) do j:= j+1: od: if(j > k) then print(`Please enter a valid state!`): return(FAIL): fi: Reach := Table[j][2][i]: add(X[r], r in Reach) / (nops(Reach)): end: # Write a procedure # Oper(G,X): [seq(Oper1(G,i, X), i = 1..n)]: # (basically for all the states): Oper:= proc(G, X) local i, n, Table, k: Table:= G[3]: k := 0: while type(Table[k+1], list) do k := k + 1: od: #do Oper1 for all states in G n:= 0: for i from 1 to k do n:= n + nops(Table[i][1]): od: [seq(Oper1(G,i, X), i = 1..n)]: end: #inputs X (a formal variable), T (an operator in your data structure as a list of length n:=nops(T) of linear combinations of X[i]'s i=1..n) # and f (a linear combination of the X[i]'s) #Outputs A linear combination of X[i] (i=1..n) obtained by replacing X[i] in f by T[i], i.e. expand(subs({seq(X[i]=T[i],i=1..n)},f)): #For TTT, ApplyOper is the same. ApplyOper:= proc(T,f,X) local i: expand(subs({seq(X[i]=T[i],i=1..nops(T))},f)): end: EvolutionGameV:= proc(G,n,k,N,x) local i, f, T, END, WinP, FS,j,prob,s: if G = FAIL then RETURN(FAIL): fi: for j from 0 to k do WinP[j]:= 0: od: END:=0: print(cat(`The initial state is `,G[1], `.`)): f:= X[G[1]]: T := Oper(G, X): i:= 1: while i <= N do: f:= ApplyOper(T, f, X): for j from 0 to k do FS:= G[2][j]: prob:= 0: for s in FS do if has(f, X[s]) then prob:= prob + coeff(f, X[s], 1): END:= END + coeff(f, X[s], 1): f:=subs(X[s] = 0, f): fi: od: WinP[j] := WinP[j] + prob* x^i: if j = 0 then print(cat(`The probability that it ties on turn `, i, `is `, prob)): else print(cat(`The probability that player `, j, ` wins on turn `, i, ` is ` , prob)): fi: # check if any of the final states could be in LC # if any of the final states are in LC, add to the probability as coefficient that the player will win in that round to the polynomial x^i # remove the thing in the polynomial that caused that win and add the probability to the end variable. od: print(cat(`The probability that the game has ended on move `, i, ` is ` ,END )): i:= i+1: od: eval(WinP): end: # The only differences between EvolutionGameVTTT and EvolutionGameV is that # 1) there's always only 2 players in TTT # 2) in TTT, the state that a player ENCOUNTERS ( sees before making a move) is not the state that makes them win. The state that makes them win is the state they transition to. # In this case, it doesn't actually matter then to change EvolutionGameV to suit TTT, except to make the player count 2. # In EvolutionGameV, for every state, it switches to it's "next states" with a certain probability to each state and if that state is reached, then the winner of that state being reached will still win. # Therefore , TTT also works for EvolutionGameV and EvolutionGameT, but input k = 2 and the number of states in ttt.. # So, what we need is actually not n or k, but the board size, # Calculate the number of states for each board size, # put that in as n along with k =2 into EvolutionGameV and EvolutionGameT for ttt. EvolutionGameVTTT:= proc(k,N,x) local n, G, Table: G:= TicTacToe(k): Table:= G[3]: n:= 0: n:= nops(Table[1][1]) + nops(Table[2][1]): EvolutionGameV(G, n, 2, N, x): end: EvolutionGameT:= proc(G,n,k,N,x) local i, f, T, END, WinP, FS,j,prob,s: if G = FAIL then RETURN(FAIL): fi: for j from 0 to k do WinP[j]:= 0: od: END:=0: f:= X[G[1]]: T := Oper(G, X): i:= 1: while i <= N do: f:= ApplyOper(T, f, X): for j from 0 to k do FS:= G[2][j]: prob:= 0: for s in FS do if has(f, X[s]) then prob:= prob + coeff(f, X[s], 1): END:= END + coeff(f, X[s], 1): f:=subs(X[s] = 0, f): fi: od: WinP[j] := WinP[j] + prob* x^i: # check if any of the final states could be in LC # if any of the final states are in LC, add to the probability as coefficient that the player will win in that round to the polynomial x^i # remove the thing in the polynomial that caused that win and add the probability to the end variable. od: i:= i+1: od: eval(WinP): end: EvolutionGameTTTT:= proc(k,N,x) local n, G, Table: G:= TicTacToe(k): Table:= G[3]: n:= 0: n:= nops(Table[1][1]) + nops(Table[2][1]): EvolutionGameT(G, n, 2, N, x): end: #uses #the EvolutionGameT #to get the poly. of degree<=N whose meaning is the #prob. that the game ends exactly after i rounds GenFunDuration:= proc(G,k,N,x) local Prob, i, j, T, f,EndP, fs,s,FS : if G = FAIL then RETURN(FAIL): fi: EndP:= 0: f:= X[G[1]]: T := Oper(G, X): i:= 1: while i <= N do: Prob := 0: f:= ApplyOper(T, f, X): for j from 0 to k do FS:= G[2][j]: for s in FS do if has(f, X[s]) then Prob:= Prob + coeff(f, X[s], 1): f:=subs(X[s] = 0, f): fi: od: od: EndP := EndP + Prob*x^i: i:= i+1: od: EndP: end: #r is either 0 or 1,...,k, whose #meaning is a polynomial of degree<=N whose coeff. of z^i is the prob. that #player r won after exactly i moves. GenFunWin:= proc(G,n,k,N,x,r) : if r > k or r < 0 then print(`Please enter a valid player (r) value`): return(FAIL): fi: EvolutionGameT(G,n,k,N,x)[r]: end: GenFunDurationTTT:= proc(k,N,x) local f,T,i,Prob,j,s,FS,G,EndP: G:= TicTacToe(k): if G = FAIL then RETURN(FAIL): fi: EndP:= 0: f:= X[G[1]]: T := Oper(G, X): i:= 1: while i <= N do: Prob := 0: f:= ApplyOper(T, f, X): for j from 0 to 2 do FS:= G[2][j]: for s in FS do if has(f, X[s]) then Prob:= Prob + coeff(f, X[s], 1): f:=subs(X[s] = 0, f): fi: od: od: EndP := EndP + Prob*x^i: i:= i+1: od: EndP: end: GenFunWinTTT:= proc(k, N, x, r) local G: if r > 2 or r < 0 then print(`Please enter a valid player (r) value`): return(FAIL): fi: EvolutionGameTTTT(k, N, x)[r]: end: