33 lines
1.0 KiB
Plaintext
33 lines
1.0 KiB
Plaintext
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
|
|
]]
|