#!/usr/bin/env python
# ~*~ coding: utf-8 ~*~
from random import randrange
class Choice():
CHOICES = [1,2,3]
@classmethod
def get_random_choice(cls):
return CHOICES[randrange(0, len(CHOICES))]
class Person():
""" Contains meta info about players """
def __init__(self, name):
self.name = name
class LocalPersonController(Person):
""" Implements comunication with player on local machine """
INPUT_PROMPT = '''Choose one!
<1> Scissors
<2> Paper
<3> Rock
{0} choose: '''
def get_choice(self):
return int(raw_input(LocalPersonController.
INPUT_PROMPT.format(self.name)))
class NetworkPersonController(Person):
""" Implements comunication with player on remote machine """
def __init__(self, name, uri):
super(NetworkPersonController, self).__init__(name)
# TODO: init connection
def get_choice(self):
# TODO: replace stub
return Choice.get_random_choice()
class AIController(Person):
def __init__(self):
super(AIController, self).__init__("AI")
def get_choice(self):
return Choice.get_random_choice()
class Round():
def __init__(self, player1Ctrl, player2Ctrl):
self.player1 = player1Ctrl
self.player2 = player2Ctrl
def run(self):
self.turn1 = self.player1.get_choice()
self.turn2 = self.player2.get_choice()
class Game():
PlAY_PROMPT = '''Wish you play a round? Y/n '''
ROUND_PROMPT = '''Select oponentopponent:
<1> local player
<2> remote player
<3> AI
'''
def start(self):
print("Hello!")
while self.play():
round = self.select_round()
round.run()
self.show_victory(round)
print("By!")
def play(self):
choise = raw_input(Game.PlAY_PROMPT)
while choise.lower() not in ['n', 'y', '']:
choise = raw_input(Game.PlAY_PROMPT)
return not choise.lower() == 'n'
def select_round(self):
choise = raw_input(Game.ROUND_PROMPT)
while choise.lower() not in ['1', '2', '3']:
choise = raw_input(Game.ROUND_PROMPT)
oponentopponent = object
if choise.lower() == '1':
oponentopponent = LocalPersonController('player2')
elif choise.lower() == '2':
oponentopponent = NetworkPersonController('player2', 'some uri data')
else:
oponentopponent = AIController()
return Round(LocalPersonController('player1'), oponentopponent)
def show_victory(self, round):
# TOD: show which player win
pass
if __name__ == '__main__':
Game().start()
Player input
You can add few ways of player input. This approach gives you way to implement AI. Look at classes: Person
and inherited from it. All of them had common input interface, so nothing can stop you to add alien's behavior (:
Game mode
As for me, game mode is a part of Choice class, which implement different choices and common interface to find winner, get list choices and so on.
Game round
It is a single game round, perhaps it should be hard linked with game mode in case of behavior. Any way it should perform one game iteration. In my example I find winner outside of the Round
class, but it is a part of single loop iteration so it should be there.
Gama loop
You can add some preferences and don't ask for selecting opponent. Also add some defaults or even writing preferences into local settings.conf
.