Created
January 18, 2019 03:16
-
-
Save siketyan/0cf2e455e345c39da5ed92c1a316a721 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
#-*- coding: utf-8 -*- | |
from sys import exit | |
from tkinter import * | |
class Janken: | |
HANDS = ["g", "c", "p"] | |
STRINGS = ["グー", "チョキ", "パー"] | |
QUIT = ["q", "終了"] | |
def __init__(self): | |
self.num_hands = len(self.HANDS) | |
self.data = [[0 for i in range(3)] for j in range(4)] | |
self.last = self.num_hands | |
self.total = [0] * 3 | |
self.select_next() | |
def print_hands(self): | |
print("> ", end = "") | |
for i in range(self.num_hands + 1): | |
if i == self.num_hands: | |
print("%s: %s" %(self.QUIT[0], self.QUIT[1])) | |
return | |
print("%s: %s," %(self.HANDS[i], self.STRINGS[i]), end = " ") | |
def parse_hand(self, char): | |
if char == self.QUIT[0]: | |
return -2 | |
for i in range(self.num_hands): | |
if self.HANDS[i] == char: | |
return i | |
return -1 | |
def suggest(self, last): | |
maximum = -1 | |
indices = [] | |
for i in range(self.num_hands): | |
value = self.data[last][i] | |
if value > maximum: | |
maximum = value | |
indices = [i] | |
elif value == maximum: | |
indices.append(i) | |
return indices | |
def select(self, suggestion): | |
from random import randint | |
return suggestion[randint(0, len(suggestion) - 1)] | |
def win_hand(self, hand): | |
if hand == 0: | |
return self.num_hands - 1 | |
else: | |
return hand - 1 | |
def lose_hand(self, hand): | |
if hand == self.num_hands - 1: | |
return 0 | |
else: | |
return hand + 1 | |
def test(self, user, agent): | |
if user == agent: | |
return 0 | |
elif user == self.win_hand(agent): | |
return 1 | |
else: | |
return -1 | |
def select_next(self): | |
suggestion = self.suggest(self.last) | |
self.next = self.win_hand(self.select(suggestion)) | |
def main(): | |
total = [0] * 3 | |
last = num_hands | |
while True: | |
suggestion = suggest(last) | |
agent = win_hand(select(suggestion)) | |
print_hands() | |
now = parse_hand(input("< ")) | |
if now == -2: | |
print( | |
"> あなたは %d勝 %d敗 %d分 でした" | |
%(total[0], total[1], total[2]) | |
) | |
return | |
elif now < 0: | |
print("> 不正な値です") | |
continue | |
data[last][now] += 1 | |
last = now | |
print("> あなた: %s, コンピュータ: %s" %(STRINGS[now], STRINGS[agent])) | |
result = test(now, agent) | |
if result == 0: | |
total[2] += 1 | |
print("> あいこ") | |
elif result < 0: | |
total[1] += 1 | |
print("> あなたの負け") | |
else: | |
total[0] += 1 | |
print("> あなたの勝ち") | |
def play(self, hand): | |
now = hand | |
agent = self.next | |
self.data[self.last][now] += 1 | |
self.last = now | |
result = self.test(now, agent) | |
if result == 0: | |
self.total[2] += 1 | |
elif result < 0: | |
self.total[1] += 1 | |
else: | |
self.total[0] += 1 | |
return result | |
class App: | |
PADDING = 1 | |
def play(self, hand): | |
result = self.janken.play(hand) | |
self.label_result.configure(text = str(result)) | |
def place_label(self, text, row, column, columnspan = None): | |
label = Label(self.window, text = text) | |
label.grid( | |
row = row, | |
column = column, | |
columnspan = columnspan, | |
padx = self.PADDING, | |
pady = self.PADDING | |
) | |
return label | |
def place_button(self, text, command, row, column, width = None, columnspan = None, sticky = None): | |
button = Button(self.window, width = width, text = text, command = command) | |
button.grid( | |
row = row, | |
column = column, | |
columnspan = columnspan, | |
sticky = sticky, | |
padx = self.PADDING, | |
pady = self.PADDING | |
) | |
return button | |
def place_widgets(self): | |
self.place_label("じゃんけんゲーム", 0, 0, 3) | |
self.place_label("あなた", 1, 0) | |
self.place_label("コンピュータ", 1, 2) | |
self.label_user = self.place_label("じゃんけん", 2, 0) | |
self.label_agent = self.place_label("ジャンケン", 2, 2) | |
self.label_result = self.place_label("対戦結果", 3, 0, 3) | |
self.place_button("グー", lambda: self.play(0), 4, 0, 10) | |
self.place_button("チョキ", lambda: self.play(1), 4, 1, 10) | |
self.place_button("パー", lambda: self.play(2), 4, 2, 10) | |
self.place_button("ばいばい", sys.exit, 5, 0, None, 3, "ew") | |
def main(self): | |
self.janken = Janken() | |
self.window = Tk() | |
self.place_widgets() | |
self.window.mainloop() | |
if __name__ == "__main__": | |
App().main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment