Skip to content

Instantly share code, notes, and snippets.

@tobijibu
Last active May 18, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobijibu/7ef30e290c349a1d770b8b2091ead2ee to your computer and use it in GitHub Desktop.
Save tobijibu/7ef30e290c349a1d770b8b2091ead2ee to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class ObakeCatch():
obj = ['おばけ', 'いす', 'ほん', 'びん', 'ねずみ']
color = ['white', 'red', 'blue', 'green', 'gray']
def doAction(self, depict1, depict2):
self.depict1 = depict1
self.depict2 = depict2
act = self.getActText()
res = self.result()
print(res + act)
def getActText(self):
if (self.depict1['obj'] != 'ほん' and self.depict2['obj'] != 'ほん'):
return 'を取る'
return 'って言う'
def result(self):
# モノリストをコピー
_obj = ObakeCatch.obj[:]
# カードに描かれているモノ・色の要素番号
obj1Idx = ObakeCatch.obj.index(self.depict1['obj'])
col1Idx = ObakeCatch.color.index(self.depict1['color'])
obj2Idx = ObakeCatch.obj.index(self.depict2['obj'])
col2Idx = ObakeCatch.color.index(self.depict2['color'])
# 要素番号リストを生成して重複削除、降順ソート
depictsIdx = sorted(set([obj1Idx, col1Idx, obj2Idx, col2Idx]), reverse=True)
# ありえないパターン
# 同じモノ・色が描かれている
# (白いおばけと青いおばけ とか、赤いおばけと赤いびんとか)
if (obj1Idx == obj2Idx or col1Idx == col2Idx):
return 'どれでもないなにか'
# モノ・色が揃っているのが2つ描かれている
# (白いおばけと赤いいす とか)
if (obj1Idx == col1Idx and obj2Idx == col2Idx):
return 'どれでもないなにか'
# 描かれているものが正しい
if (obj1Idx == col1Idx):
return self.depict1['obj']
if (obj2Idx == col2Idx):
return self.depict2['obj']
# ありえないパターン
# 答えが特定できない
# (赤いおばけと白いいす とか)
if (len(depictsIdx) != 4):
return 'どれでもないなにか'
# 色も形も違うものが正しい
for i in depictsIdx:
del _obj[i]
return _obj[0]
oc = ObakeCatch()
depict1 = {'obj': 'いす', 'color': 'red'}
depict2 = {'obj': 'おばけ', 'color': 'gray'}
oc.doAction(depict1, depict2)
depict1 = {'obj': 'おばけ', 'color': 'gray'}
depict2 = {'obj': 'びん', 'color': 'red'}
oc.doAction(depict1, depict2)
depict1 = {'obj': 'ほん', 'color': 'gray'}
depict2 = {'obj': 'おばけ', 'color': 'red'}
oc.doAction(depict1, depict2)
# -*- coding: utf-8 -*-
class ObakeCatch2():
obj = ['おばけ', 'てぬぐい', 'くし', 'かえる', 'ふろ']
color = ['white', 'red', 'blue', 'green', 'gray']
objEn = ['ghost', 'towel', 'brash', 'frog', 'bath']
def doAction(self, depict1, depict2):
self.depict1 = depict1
self.depict2 = depict2
self.setAct()
self.setTowel()
act = self.getActText()
res = self.result()
print(res + act)
def setAct(self):
self.isGet = (self.depict1['obj'] != 'かえる' and self.depict2['obj'] != 'かえる')
def setTowel(self):
self.hasTowel = (self.depict1['obj'] == 'てぬぐい' or self.depict2['obj'] == 'てぬぐい')
def getActText(self):
return 'を取る' if self.isGet else 'って言う'
def result(self):
# モノリストをコピー
_obj = ObakeCatch2.obj[:]
# カードに描かれているモノ・色の要素番号
obj1Idx = ObakeCatch2.obj.index(self.depict1['obj'])
col1Idx = ObakeCatch2.color.index(self.depict1['color'])
obj2Idx = ObakeCatch2.obj.index(self.depict2['obj'])
col2Idx = ObakeCatch2.color.index(self.depict2['color'])
# 要素番号リストを生成して重複削除、降順ソート
depictsIdx = sorted(set([obj1Idx, col1Idx, obj2Idx, col2Idx]), reverse=True)
# ありえないパターン
# 同じモノ・色が描かれている
# (白いおばけと青いおばけ とか、赤いおばけと赤いくしとか)
if (obj1Idx == obj2Idx or col1Idx == col2Idx):
return 'どれでもないなにか'
# モノ・色が揃っているのが2つ描かれている
# (白いおばけと青いくし とか)
if (obj1Idx == col1Idx and obj2Idx == col2Idx):
return 'どれでもないなにか'
# てぬぐいがある
if (self.hasTowel):
if (self.depict1['obj'] == 'てぬぐい'):
return ObakeCatch2.obj[col1Idx] if self.isGet else ObakeCatch2.objEn[col1Idx]
if (self.depict2['obj'] == 'てぬぐい'):
return ObakeCatch2.obj[col2Idx] if self.isGet else ObakeCatch2.objEn[col2Idx]
# 描かれているものが正しい
if (obj1Idx == col1Idx):
# 言う場合は英語
return self.depict1['obj'] if self.isGet else ObakeCatch2.objEn[obj1Idx]
if (obj2Idx == col2Idx):
# 言う場合は英語
return self.depict2['obj'] if self.isGet else ObakeCatch2.objEn[obj2Idx]
# ありえないパターン
# 答えが特定できない
# (青いおばけと白いくし とか)
if (len(depictsIdx) != 4):
return 'どれでもないなにか'
# 色も形も違うものが正しい
for i in depictsIdx:
del _obj[i]
return _obj[0]
oc = ObakeCatch2()
depict1 = {'obj': 'おばけ', 'color': 'red'}
depict2 = {'obj': 'かえる', 'color': 'blue'}
oc.doAction(depict1, depict2)
depict1 = {'obj': 'おばけ', 'color': 'gray'}
depict2 = {'obj': 'くし', 'color': 'red'}
oc.doAction(depict1, depict2)
depict1 = {'obj': 'てぬぐい', 'color': 'green'}
depict2 = {'obj': 'かえる', 'color': 'blue'}
oc.doAction(depict1, depict2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment