Last active
May 19, 2022 10:54
-
-
Save takahashilabo/fa1881cf524376b2038e181320865a28 to your computer and use it in GitHub Desktop.
Simple janken program on Strategy Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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