Programming Fundamentals for Bot Builders
Learning objectiveUse variables, types, branches, functions, loops, collections, JSON, and error handling.
Understand the idea
A variable stores a value: stack = 1200 is a number, street = 'flop' is text, and can_check = True is a boolean. A list such as ['As','Kd'] holds ordered cards; a dictionary such as {'kind':'check'} maps names to values. JSON is the text format used to carry these structures between programs.
An if/elif/else branch chooses a rule. A function packages the rule so you can test it. A loop can read one line at a time from stdin; parse each with json.loads and print one JSON action with json.dumps. Catch malformed input and respond conservatively instead of crashing or writing debugging text to stdout. Send diagnostics to stderr.
At the poker table
Two different JSON observations can call the same choose(observation) function. That function returns a dictionary; the loop serializes it as a single line.
Code and decision sketch
Pythonimport json, sys
def choose(observation):
legal = observation.get('legal') or {}
return {'kind': 'check'} if legal.get('check') else {'kind': 'fold'}
for line in sys.stdin:
try:
observation = json.loads(line)
reply = choose(observation)
except (ValueError, TypeError) as error:
print(f'Bad observation: {error}', file=sys.stderr)
reply = {'kind': 'fold'}
print(json.dumps(reply), flush=True)Common mistake
Printing explanations to stdout breaks the adapter's one-JSON-object-per-line contract. A malformed observation may need human investigation; do not pretend the fallback is always legal.
Try it yourself
Change choose so it calls only when legal.call is true and legal.callAmount is at most 20; otherwise check or fold.
