moth/roshambo/roshambo.py

48 lines
1.2 KiB
Python
Raw Normal View History

2009-08-25 18:04:42 -06:00
#! /usr/bin/env python3
from ctf import game
2009-08-25 18:04:42 -06:00
2009-08-26 15:14:09 -06:00
class Roshambo(game.TurnBasedGame):
2009-08-25 18:04:42 -06:00
def setup(self):
2009-08-26 15:14:09 -06:00
self.moves = []
def calculate_moves(self):
players = [m[0] for m in self.moves]
2009-08-26 15:14:09 -06:00
moves = [m[1] for m in self.moves]
if moves[0] == moves[1]:
players[0].write('tie')
players[1].write('tie')
2009-08-26 15:14:09 -06:00
self.moves = []
elif moves in (['rock', 'scissors'],
['scissors', 'paper'],
['paper', 'rock']):
2009-08-26 15:14:09 -06:00
# First player wins
self.declare_winner(players[0])
2009-08-26 15:14:09 -06:00
else:
self.declare_winner(players[1])
2009-08-25 18:04:42 -06:00
def make_move(self, player, move):
2009-08-26 15:14:09 -06:00
self.moves.append((player, move))
self.end_turn(player)
def do_rock(self, player, args):
self.make_move(player, 'rock')
def do_scissors(self, player, args):
self.make_move(player, 'scissors')
def do_paper(self, player, args):
self.make_move(player, 'paper')
2009-08-25 18:04:42 -06:00
def start():
return game.start(Roshambo, 5388, b'roshambo:::984233f357ecac03b3e38b9414cd262b', 2)
2009-08-25 18:04:42 -06:00
if __name__ == '__main__':
import asyncore
start()
asyncore.loop(use_poll=True)
2009-08-25 18:04:42 -06:00