Created
June 19, 2024 18:56
-
-
Save afmika/1e68feb226af790b2d0dc63e1aff26a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
make_game ← {¯2 + {?⍵ ⍵⍴3}⍵} | |
sum_hv ← {g←⍵ ⋄ (+/¨) ¨ ({⍵⌷g}¨⍳≢g) ({⍵⌷⍉g}¨⍳≢g)} | |
sum_dlr ← {g←⍵ ⋄ y←≢g ⋄ (+/) ¨ ({⍵ (y-⍵-1)⌷g}¨⍳y) ({⍵ ⍵⌷g}¨⍳y)} | |
won_hv ← +/¨ 1 ¯1 × { t←≢⍵ ⋄ ∨/ t (¯1×t)∘.=sum_hv ⍵} | |
won_dg ← 1 ¯1 × {t←≢⍵ ⋄ ∨/ t (¯1×t)∘.=sum_dlr ⍵} | |
idx_empt_map ← {(⍵=0)×({{⍵∘.,⍵}⍳≢⍵} ⍵)} | |
idx_empt ← {(,idx_empt_map ⍵)~⊂0 0} | |
at ← {(g r c)←⍵ ⋄ (r c⌷g)←⍺ ⋄ g} | |
emul_emp ← {g←⍵ ⋄ ⍺ {(r c)←⍵ ⋄ ⍺ at g r c} ¨idx_empt ⍵} | |
won_fl ← { { ({⌈/⊃⌈/⍵}⍵) ({⌊/⊃⌊/⍵}⍵) } (won_hv ⍵) (won_dg ⍵)} | |
won_unorm ← {r←won_fl ⍵ ⋄ 1=⌈/r:1 ⋄ ⌊/r} | |
won ← {g←⍵ ⋄ ⌊/1 (⌈/¯1 ({won_unorm ⍵}g))} | |
pt ← {(g dp mdp)←⍵ ⋄ r←won g ⋄ (mdp-dp)×r} | |
minmax←{ | |
(g max dp mdp)←⍵ | |
0=≢idx_empt g:0 | |
dp≥mdp:0 | |
0≠won g:pt g dp mdp | |
not←max≢1 | |
p←(max+1)⌷¯1 1 | |
nxt_gen←p emul_emp g | |
flat← (∊ {minmax ⍵ not (dp+1) mdp}¨nxt_gen) | |
1=max:⌈/¯1 (⌈/flat) | |
⌊/1 (⌊/flat) | |
} | |
⍝ let's make a random mid-game and run this | |
game ← make_game 3 | |
{'X_O'[⍵+2]}game | |
minmax game 1 0 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment