Cheating at Rock, Paper, Scissors

Meta-programming in Python

Prog competition for iterated rock-paper-scissors

  • 1000 rounds
  • players have history
  • 3 for win, 0 for draw, 1 for loss

template_bot.py examples

‘good old rock’

def name():
    return 'Bart'

def move(my, opp):
    return 'R'

random

def name():
    return 'Bart'

def move(my, opp):
    return rand.sequence('RPS')

psychic bot? predict what opponent will do, change what opponent will do!!

  • level 1 - find opponent’s move function, make them play a test move, select winning move
  • inspect the stack, get the frame record -> from object -> f_back (check stack), f_locals (see if opp bot is in scope)
  • is_instance(f, ModuleType), hasattr(f, 'name') and hasattr(f, 'move')
  • next step, fiddle the pseudo random number generator
  • but when it plays itself which causes infinite recursion “Yuri Gellers all the way down”
  • fix by checking if we’re in the stack already - if so return random

Abstract syntax trees

  • how about just make the opponent do what we want
  • python tools - ast module, compile, exec function
  • make the first instruction of move() be return 'R'

Table Of Contents

Previous topic

How does Django start?

Next topic

Task Management with Celery & Django

This Page