#C22.txt, April 15, 2019, Dr,Z.'s ExpMath class Help:=proc(): print(` PSB(m,n), IP(m,n), RHM(S,B), Moves(S,B) , RG(S,B) ,`): print(` Cont(L,B) , AllGramesK(S,B,K), PlotPos(S) , EF(S,B) `): print(`SmartOne(S,B), SmartGame(S,B) , BestRand(S,B,K), BestRandStory(S,B,K) `): print(`RectBoard(m,n), RectBoardIP(m,n) `): end: PSB:=proc(m,n) local i,j: { seq(seq([i,j],i=m+1..m+2*n-1),j=1..m), seq(seq([i,j],i=1..2*m+2*n-1),j=m+1..m+2*n-1), seq(seq([i,j],i=m+1..m+2*n-1),j=m+n+1..2*m+2*n-1)} end: IP:=proc(m,n): PSB(m,n) minus {[m+n,m+n]}: end: #RHM(S,B): Given a position S in a peg-solitaire with board B, all the right horizontal moves in Peg Solitaire RHM:=proc(S,B) local mo,s: mo:={}: for s in S do if member(s+[1,0],S) and (not member(s+[2,0],S)) and member(s+[2,0],B) then mo:=mo union {S minus {s,s+[1,0]} union {s+[2,0]}}: fi: od: mo: end: #LHM(S,B): Given a position S in a peg-solitaire with board B, all the left horizontal moves in Peg Solitaire LHM:=proc(S,B) local mo,s: mo:={}: for s in S do if member(s-[1,0],S) and (not member(s-[2,0],S)) and member(s-[2,0],B) then mo:=mo union {S minus {s,s-[1,0]} union {s-[2,0]}}: fi: od: mo: end: #UVM(S,B): Given a position S in a peg-solitaire with board B, all the up vertical moves in Peg Solitaire UVM:=proc(S,B) local mo,s: mo:={}: for s in S do if member(s+[0,1],S) and (not member(s+[0,2],S)) and member(s+[0,2],B) then mo:=mo union {S minus {s,s+[0,1]} union {s+[0,2]}}: fi: od: mo: end: #DVM(S,B): Given a position S in a peg-solitaire with board B, all the down vertical moves in Peg Solitaire DVM:=proc(S,B) local mo,s: mo:={}: for s in S do if member(s-[0,1],S) and (not member(s-[0,2],S)) and member(s-[0,2],B) then mo:=mo union {S minus {s,s-[0,1]} union {s-[0,2]}}: fi: od: mo: end: Moves:=proc(S,B):RHM(S,B) union LHM(S,B) union UVM(S,B) union DVM(S,B): end: #RG(S,B): a random game RG:=proc(S,B) local L,S1,mo: L:=[S]; S1:=S: while Moves(S1,B)<>{} do mo:=Moves(S1,B): S1:=mo[rand(1..nops(mo))()]: L:=[op(L),S1]: od: L: end: Cont:=proc(L,B) local la,gu,gu1: la:=L[-1]: gu:=Moves(la,B): {seq([op(L),gu1],gu1 in gu)} end: #AllGamesK(S,B,K) of length K AllGamesK:=proc(S,B,K) local gu,gu1: option remember: if K=0 then RETURN({[S]}): fi: gu:=AllGamesK(S,B,K-1): {seq(op(Cont(gu1,B)), gu1 in gu)}: end: with(plots): PlotPos:=proc(S) plot(S,style=point,axes=none): end: #PlotGame(G): plots all the intermediate positions of the game #pausing for s seconds PlotGame:=proc(G) local i: for i from 1 to nops(G) do PlotPos(G[i]): #Threads[Sleep](s): od: end: #EF1(S,B): the number of moves available at position S with Peg Solitaire with board B EF1:=proc(S,B) : nops(Moves(S,B)): end: #SmartOne(S,B,f): the best move from S using Quentin D.'s evaluation function #(the position with most options) SmartOne:=proc(S,B,f) local mo,rec,cha,i: mo:=Moves(S,B): if mo={} then RETURN(S): fi: cha:=mo[1]: rec:=f(cha,B): for i from 2 to nops(mo) do if EF(mo[i],B)>rec then cha:=mo[i]: rec:=f(cha,B): fi: od: cha: end: #SmartGame(S,B,f) SmartGame:=proc(S,B,f) local L,S1,S2: S1:=S: L:=[S1]: S2:=SmartOne(S1,B,f): while S1<>S2 do L:=[op(L),S2]: S1:=S2: S2:=SmartOne(S1,B,f): od: L: end: #EF2(S,B,k): the best move according to QD's but looking k moves ahead EF2:=proc(S,B,k) end: #BestRand(S,B,K): the best of K random game BestRand:=proc(S,B,K) local i,rec,cha,hope: cha:=RG(S,B): rec:=nops(cha[-1]): for i from 1 to K do hope:=RG(S,B): if nops(hope[-1])< rec then cha:=hope: rec:=nops(hope[-1]): fi: od: cha: end: #BestRandStory(S,B,K): the best of K random game BestRandStory:=proc(S,B,K) local i,rec,cha,hope: cha:=RG(S,B): rec:=nops(cha[-1]): for i from 1 to K do hope:=RG(S,B): if nops(hope[-1])< rec then cha:=hope: rec:=nops(hope[-1]): print(`The current record is`, rec): print(`The current champion is`): lprint(cha): fi: od: cha: end: #RectBoard(m,n): a rectangular m by n Peg Solitaire Board RectBoard:=proc(m,n) local i,j: {seq(seq([i,j],i=1..n),j=1..m)}: end: #RectBoardIP(m,n): the inital position of a rectangular m by n Peg Solitaire Board RectBoardIP:=proc(m,n) : RectBoard(m,n) minus {[1,1]}: end: