-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnotebook.py
More file actions
106 lines (77 loc) · 2.24 KB
/
notebook.py
File metadata and controls
106 lines (77 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "anywidget>=0.9.0",
# "marimo",
# "traitlets>=5.0.0",
# "wigglystuff==0.2.37",
# ]
# ///
import marimo
__generated_with = "0.20.4"
app = marimo.App()
@app.cell
def _():
import marimo as mo
from rpsls_widget.rpsls_widget import RpslsWidget
return RpslsWidget, mo
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Rock, Paper, Scissors ... and beyond?
Rock-Paper-Scissors has **3** elements. Rock-Paper-Scissors-Lizard-Spock has **5**.
Could we make a variant with **4**? Or **6**?
For a game to be *fair*, every element must beat the same number of others.
With $n$ elements, each must beat exactly $k = \frac{n - 1}{2}$ others.
That's only a whole number when $n$ is **odd**.
Use the keyboard shortcuts to explore the widget. Click in the keyboard widget area and use the
arrows to see the widget update.
""")
return
@app.cell
def _(RpslsWidget, mo):
widget = mo.ui.anywidget(RpslsWidget(n=3))
return (widget,)
@app.cell
def _(widget):
widget
return
@app.cell
def _(mo):
from wigglystuff import KeystrokeWidget
keystroke = mo.ui.anywidget(KeystrokeWidget())
keystroke
return (keystroke,)
@app.cell
def _(keystroke):
keystroke.last_key["key"]
return
@app.cell
def _(get_highlight, keystroke, set_highlight, widget):
names = "abcdefghijklmnopqrstuvwxyz".upper()
if keystroke.last_key["key"] == "ArrowRight":
widget.animate_node(1)
set_highlight(-1)
if keystroke.last_key["key"] == "ArrowLeft":
widget.animate_node(-1)
set_highlight(-1)
if keystroke.last_key["key"] == "ArrowUp":
set_highlight(lambda _: (_ + 1) % widget.n)
if keystroke.last_key["key"] == "ArrowDown":
set_highlight(lambda _: (_ - 1) % widget.n)
if keystroke.last_key["key"] == "q":
set_highlight(-1)
widget.clear_highlight()
if get_highlight() >= 0:
widget.animate_highlight(names[get_highlight()])
return
@app.cell
def _(get_highlight):
get_highlight()
return
@app.cell
def _(mo):
get_highlight, set_highlight = mo.state(-1)
return get_highlight, set_highlight
if __name__ == "__main__":
app.run()