Last active
April 29, 2022 10:34
-
-
Save AlephAlpha/7265847 to your computer and use it in GitHub Desktop.
Gomoku game in Mathematica.
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
DynamicModule[{board, winner, p, pmouse, ppossible, v, Reset}, | |
Reset[] := {winner, board} = {"", ConstantArray[0, {20, 20}]}; | |
Reset[]; Deploy@ | |
Graphics[Button[{Brown, Rectangle[{0, 0}, {20, 20}], Black, | |
Line[Join @@ | |
Table[{{{i, 0}, {i, 20}}, {{0, i}, {20, i}}}, {i, .5, 19.5}]], | |
Dynamic[pmouse = | |
Ceiling@MousePosition["Graphics", {-1, -1}] /. {0 -> 1, | |
21 -> 20}; | |
If[1 <= Min@pmouse <= Max@pmouse <= 20 && | |
winner == "", {[email protected], Disk[pmouse - .5, .4]}, {}]], | |
Dynamic[Disk[# - .5, .4] & /@ Position[board, 1]], White, | |
Dynamic[Disk[# - .5, .4] & /@ Position[board, -1]], Red, | |
Text[Style[Dynamic@winner, 60], {10, 10}]}, | |
If[winner != "", Reset[], p = pmouse; | |
If[Extract[board, p] == 0, board[[p[[1]], p[[2]]]] = 1; | |
FinishDynamic[]; | |
If[Max[Table[ | |
Total[RotateRight[board, #*d] & /@ | |
Range[0, 4]], {d, {{0, 1}, {1, 0}, {1, 1}, {1, -1}}}]] == | |
5, winner = "You win!", ppossible = Position[board, 0]; | |
v = (9*^9 Count[#, {___, -1, -1, -1, -1, ___}] + | |
3*^8 Count[#, {___, 1, 1, 1, 1, ___}] + | |
1*^7 Count[#, {___, 0, -1, -1, -1, 0, ___}] + | |
5*^5 Count[#, {___, 0, 1, 1, 1, 0, ___}] + | |
3*^5 Count[#, {___, 0, -1, -1, -1, ___}] + | |
3*^5 Count[#, {___, -1, 0, -1, -1, ___}] + | |
3*^5 Count[#, {___, -1, -1, 0, -1, ___}] + | |
3*^5 Count[#, {___, -1, -1, -1, 0, ___}] + | |
2*^4 Count[#, {__, 0, -1, -1, 0, __}] + | |
2*^4 Count[#, {__, 0, -1, -1, 0, __}] + | |
2*^4 Count[#, {__, 0, 0, -1, -1, 0}] + | |
2*^4 Count[#, {0, -1, -1, 0, 0, __}] + | |
2*^4 Count[#, {___, 0, -1, 0, -1, 0, ___}] + | |
2*^3 Count[#, {___, 0, 1, 1, 1, ___}] + | |
2*^3 Count[#, {___, 1, 0, 1, 1, ___}] + | |
2*^3 Count[#, {___, 1, 1, 0, 1, ___}] + | |
2*^3 Count[#, {___, 1, 1, 1, 0, ___}] + | |
2*^3 Count[#, {__, 0, 1, 1, 0, __}] + | |
2*^3 Count[#, {__, 0, 1, 1, 0, __}] + | |
2*^3 Count[#, {__, 0, 0, 1, 1, 0}] + | |
2*^3 Count[#, {0, 1, 1, 0, 0, __}] + | |
2*^3 Count[#, {___, 0, 1, 0, 1, 0, ___}] + | |
100 Count[#, {_, __, 0, -1, 0, __, _}] + | |
100 Count[#, {__, 0, 0, -1, 0, _}] + | |
100 Count[#, {_, 0, -1, 0, 0, __}] + | |
50 Count[#, {_, __, 0, 1, 0, __, _}] + | |
50 Count[#, {__, 0, 0, 1, 0, _}] + | |
50 Count[#, {_, 0, 1, 0, 0, __}]) & /@ (Table[ | |
Extract[board, | |
Mod[# + n*d, 20, | |
1]], {d, {{0, 1}, {1, 0}, {1, 1}, {1, -1}}}, {n, | |
Delete[Range[-4, 4], 5]}] & /@ ppossible); | |
p = RandomChoice@Extract[ppossible, Position[v, Max@v]]; | |
board[[p[[1]], p[[2]]]] = -1; | |
If[Min[Table[ | |
Total[RotateRight[board, #*d] & /@ | |
Range[0, 4]], {d, {{0, 1}, {1, 0}, {1, | |
1}, {1, -1}}}]] == -5, winner = "You lost!"]]]]], | |
Background -> Lighter[Brown, .1], ImageSize -> 419]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment