Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Flipping-bits-game/Mathematica/flipping-bits-game.math
Normal file
32
Task/Flipping-bits-game/Mathematica/flipping-bits-game.math
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
ClearAll[PermuteState]
|
||||
PermuteState[state_, {rc_, n_Integer}] := Module[{s},
|
||||
s = state;
|
||||
Switch[rc, "R",
|
||||
s[[n]] = 1 - s[[n]],
|
||||
"C",
|
||||
s[[All, n]] = 1 - s[[All, n]]
|
||||
];
|
||||
s
|
||||
]
|
||||
SeedRandom[1337];
|
||||
n = 3;
|
||||
goalstate = state = RandomChoice[{0, 1}, {n, n}];
|
||||
While[goalstate == state,
|
||||
permutations = {RandomChoice[{"C", "R"}, 20], RandomInteger[{1, n}, 20]} // Transpose;
|
||||
state = Fold[PermuteState, state, permutations];
|
||||
];
|
||||
i = 0;
|
||||
history = {state};
|
||||
Grid[goalstate, ItemSize -> {5, 3}, Frame -> True]
|
||||
b1 = Button["", state = PermuteState[state, {"C", #}];
|
||||
AppendTo[history, state]; i++;
|
||||
If[state === goalstate, MessageDialog["You Won!"];
|
||||
Print[Grid[#, Frame -> True]] & /@ history]] & /@ Range[n];
|
||||
b2 = Button["", state = PermuteState[state, {"R", #}];
|
||||
AppendTo[history, state]; i++;
|
||||
If[state === goalstate, MessageDialog["You Won!"];
|
||||
Print[Grid[#, Frame -> True]] & /@ history]] & /@ Range[n];
|
||||
Dynamic[Grid[
|
||||
Prepend[MapThread[Prepend, {state, b2}],
|
||||
Prepend[b1, Row[{"Flips: ", i}]]], Frame -> True
|
||||
]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue