Skip to content

Instantly share code, notes, and snippets.

@saaj
Last active May 10, 2020 10:45
Show Gist options
  • Save saaj/c2ecbc39c39239500c20f5d7e02066ec to your computer and use it in GitHub Desktop.
Save saaj/c2ecbc39c39239500c20f5d7e02066ec to your computer and use it in GitHub Desktop.
Better die with SL4A/QPython3 and Python on Android
import random
import android
__version__ = 1, 0, 1
__author__ = 'saaj'
__license__ = 'GPLv2+'
s = 1, 2, 3, 4, 5, 6
a = android.Android()
m = 'The die has been thrown'
while True:
# on psychology: http://goo.gl/dGWNxH
v = random.choice(s)
t = 'The value is {0}'.format(v)
a.ttsSpeak(t)
a.dialogCreateAlert(m, t)
a.dialogSetPositiveButtonText('Next')
a.dialogSetNegativeButtonText('Stop')
a.dialogShow()
r = a.dialogGetResponse().result
if r['which'] == 'negative':
break
import time
import random
from itertools import zip_longest
import android
__version__ = 0, 1, 0
__author__ = 'saaj'
__license__ = 'GPLv2+'
def ask(msg):
a.ttsSpeak(msg)
while a.ttsIsSpeaking().result:
time.sleep(0.1)
result = a.recognizeSpeech(msg, 'en',
'android.speech.action.'
'RECOGNIZE_SPEECH')
if result.error:
raise RuntimeError(result.error)
return result.result
def split(l, teams = 2):
l = list(l)
random.shuffle(l)
q, r = divmod(len(l), teams)
size = q + 1 if r else q
teams = zip_longest(*[iter(l)] * size)
for i, t in enumerate(teams):
a.ttsSpeak('Team {0}'.format(i + 1))
t = tuple(filter(bool, t))
[a.ttsSpeak(m) for m in t]
print(t)
a = android.Android()
confirm = 'Confirm the name: {0}'
lineup = []
retry = False
while True:
if retry:
n = ask('Say the name again')
else:
n = ask('Say a member name')
r = ask(confirm.format(n))
if r.lower() in ('right', 'ok', 'correct'):
retry = False
print('Added', n)
lineup.append(n)
else:
retry = True
print('Unconfirmed by', r)
continue
r = ask('Enough?')
if r.lower() in ('yes', 'yep', 'aye'):
split(lineup)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment