Created
February 2, 2019 12:53
-
-
Save rhoboro/bcc50fa32ea809081c336b05494e2a3b to your computer and use it in GitHub Desktop.
Pythonのジェネレータでじゃんけんプログラム
This file contains 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
from random import randint | |
from enum import Enum | |
class Card(Enum): | |
Rock = 0 | |
Paper = 1 | |
Scissors = 2 | |
def Player(name): | |
yield name | |
while True: | |
own = Card(randint(0, 2)) | |
yield own | |
def start(p1, p2, n): | |
count = 0 | |
count_p1 = 0 | |
count_p2 = 0 | |
p1_name = next(p1) | |
p2_name = next(p2) | |
print(f'{p1_name} VS {p2_name}') | |
while True: | |
count += 1 | |
print(f'#### ROUND {count} ###') | |
p1x = next(p1) | |
p2x = next(p2) | |
result = (p1x.value - p2x.value + 3) % 3 | |
if result == 0: | |
print(f'{p1x.name}, {p2x.name} => even') | |
elif result == 1: | |
count_p1 += 1 | |
print(f'{p1x.name}, {p2x.name} => {p1_name} +1') | |
elif result == 2: | |
count_p2 += 1 | |
print(f'{p1x.name}, {p2x.name} => {p2_name} +1') | |
if max(count_p1, count_p2) < n: | |
print() | |
continue | |
if n <= count_p1: | |
print(f'{p1_name}: {count_p1} {p2_name}: {count_p2}') | |
print(f'{p1_name} are winner') | |
p1.close() | |
p2.close() | |
break | |
elif n <= count_p2: | |
print(f'{p1_name}: {count_p1} {p2_name}: {count_p2}') | |
print(f'{p1_name} are loser') | |
p1.close() | |
p2.close() | |
break | |
if __name__ == '__main__': | |
taro = Player('Taro') | |
hanako = Player('Hanako') | |
start(taro, hanako, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment