I'm trying to build a tic tac toe program using a 3x3 board list running inside of a DynamicModule
with a separate window displaying the current value and accepting clicks from the user. For now I'm just trying to use EventHandler
to set any empty boxes to X
's when clicked.
When I click on a box, I get the error
Part::pkspec1: The expression x cannot be used as a part specification.
Is this an issue with assigning a new value for board inside of the EventHandler
?
How else should I structure this?
createBox[elem_] := Show[
Graphics[{White, Rectangle[]}, Frame -> True,
FrameStyle -> Thickness[.02], FrameTicks -> None],
Graphics@Text[Style[elem, FontSize -> 100], {.5, .5}],
ImageSize -> 150
]
DynamicModule[{board = Table[Table[" ", 3], 3]},
CreateWindow[
DocumentNotebook[
(*Displaying grid*)
Dynamic@Grid[
Table[Table[
EventHandler[
createBox[board[[y]][[x]]]
, {"MouseClicked" :> (
If[board[[y]][[x]] == " ", board[[y]][[x]] = "X";];
)}]
, {x, 3}], {y, 3}],
Spacings -> {0, 0}]
],
WindowElements -> {}, WindowTitle -> "Tic Tac Toe",
WindowSize -> All, Editable -> False, Selectable -> False,
ShowCellBracket -> False, WindowFrameElements -> {"CloseBox"}];
];
Toggler
there's an example of one way to use it for a Tic-Tac-Toe game (might be a possible starting point). $\endgroup$