Skip to content

Instantly share code, notes, and snippets.

@takahashilabo
Last active May 19, 2022 10:54
Show Gist options
  • Save takahashilabo/fa1881cf524376b2038e181320865a28 to your computer and use it in GitHub Desktop.
Save takahashilabo/fa1881cf524376b2038e181320865a28 to your computer and use it in GitHub Desktop.
Simple janken program on Strategy Pattern
import random
class Hand:
hand = ['GUU', 'CHO', 'PA']
@classmethod
def getHand(cls, i):
return cls.hand[i]
class Strategy:
def nextHand(self):
pass
class SameHandStrategy(Strategy):
def __init__(self):
self.__hand = Hand.getHand(random.randint(0,2))
def nextHand(self):
return self.__hand
class RandomStrategy(Strategy):
def nextHand(self):
return Hand.getHand(random.randint(0,2))
class Player:
def __init__(self, strategy):
self.__strategy = strategy
def nextHand(self):
return self.__strategy.nextHand()
p1 = Player(SameHandStrategy())
p2 = Player(RandomStrategy())
for _ in range(10):
h1 = p1.nextHand()
h2 = p2.nextHand()
if h1 == h2:
print("あいこ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment