Skip to content

Instantly share code, notes, and snippets.

@jmoy
Created February 25, 2011 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmoy/843377 to your computer and use it in GitHub Desktop.
Save jmoy/843377 to your computer and use it in GitHub Desktop.
Keep score for Rosenthal's centipede game
"""
A script to keep score for a version of Rosenthal's centipede game.
The rules are:
1. There are two players.
2. At the beginning of the game the players have 1 point each.
3. The players get turns alternately.
4. On their turn a player can choose to continue (C) or stop (S).
5. If the player chooses C, 1 point is taken from their account
and 2 points are added to the other player's account.
6. If the player chooses S, the game stops.
7. If after any step both players have exactly MAX points, the game stops.
Usage: python centipede.py [MAX]
The default value of max is 100
"""
from sys import argv
max=100
if len(argv)>1:
n=int(argv[1])
if n>1:
max=n
k=[1,1]
i=0
while k[0]!=max or k[1]!=max:
p="[%d,%d]\tPlayer %d: C/s? "%(k[0],k[1],i+1)
ans="X"
while ans!="S" and ans!="C" and ans!="s" and ans!="c" and ans!="":
ans=raw_input(p)
if ans=="S" or ans=="s":
break
k[i]=k[i]-1
k[1-i]=k[1-i]+2
i=1-i
print "[%d,%d]"%(k[0],k[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment