# main.py
```python=
import numpy as np
import kamehame
from type import Choice, Event, Sound
import sound
import image
import time
import concurrent.futures
# 初期化
#Arduinoとの接続確認,chargeのリセット,cameraの接続
def init(player_choice_stack, cpu_choice_stack): #out:VideoCaptureのインスタンス
# VideoCaptureのインスタンスを作成
cap = cv2.VideoCapture(0)
player_choice_stack.clear()
cpu_choice_stack.clear()
model = image.load_model()
return cap, model
def wait_level_input(): # out: level (int)
return 0 #levelをoutput
def decide_choice(player_choice_stack, cpu_choice_stack, level=0): #out : Choice
num_cpu = 0 #cpuのため数
num_player = 0 #playerのため数
#stackからcharge数読み取る
for item in player_choice_stack:
if Choice(item) == Choice.CHARGE:
num_player += 1
elif Choice(item) == Choice.ATTACK:
num_player -= 1
elif Choice(item) == Choice.BEAM:
num_player -= 2
for item in cpu_choice_stack:
if Choice(item) == Choice.CHARGE:
num_cpu += 1
elif Choice(item) == Choice.ATTACK:
num_cpu -= 1
elif Choice(item) == Choice.BEAM:
num_cpu -= 2
#levelに合わせて難易度調整?(未実装)
prob_list = kamehame.choice_gen(num_cpu, num_player, True) #確率テーブルの作成
next_c = np.random.choice(4, p=prob_list) # prob_listに基づいてランダムに決定
return Choice(next_c)
def judge_game(player_choice_stack, cpu_choice_stack): #out Event
#stackから読み取り (Choice型)
latest_player_choice = Choice(player_choice_stack[-1]) #最後にplayerが出した手
latest_cpu_choice = Choice(cpu_choice_stack[-1]) #最後にcpuが出した手
num_player = 0
num_player_defend = 0
#stackからcharge数読み取る
for item in player_choice_stack:
if Choice(item) == Choice.CHARGE:
num_player += 1
elif Choice(item) == Choice.ATTACK:
num_player -= 1
elif Choice(item) == Choice.BEAM:
num_player -= 2
elif Choice(item) == Choice.DEFEND:
num_player_defend += 1
if (num_player == 0 and latest_player_choice == Choice.Attack)or(num_player < 2 and latest_player_choice == Choice.BEAM):
# charge数不足->強制的にchargeを出したことにする
player_choice_stack.pop()
player_choice_stack.append(Coice.CHARGE.value)
return Event.LACK_OF_CHARGE
elif (num_player_defend > 5 and latest_player_choice == Choice.DEFEND):
return Event.DEFEND_COUNT_EXCEED # defend数制限超過
# 勝敗判定
table = [[Event.DRAW, Event.CPU_WIN, Event.PLAYER_WIN, Event.DRAW],
[Event.PLAYER_WIN, Event.DRAW, Event.PLAYER_WIN, Event.DRAW],
[Event.CPU_WIN, Event.CPU_WIN, Event.DRAW, Event.DRAW],
[Event.DRAW, Event.DRAW, Event.DRAW, Event.DRAW]]
return table[latest_player_choice.value][latest_cpu_choice.value]
#終了処理
def end(cap): #input:VideoCaptureのインスタンス
#インスタンスのrelease
cap.release()
def func1(cap, player_choice_stack, cpu_choice_stack):
# 次の手の決定・stackに選んだ手を追加
cpu_choice = decide_choice(player_choice_stack, cpu_choice_stack)
cpu_choice_stack.append(cpu_choice.value)
# 写真の撮影・画像認識・stackに選んだ手を追加
img = image.capture_img(cap)
player_choice = image.classify_img(model, img)
player_choice_stack.append(player_choice.value)
def func2():
return 0
## サーボで手を動かす
if __name__ == "__main__":
#choice_stackには数字を格納
player_choice_stack = []
cpu_choice_stack = []
cap, model = init(player_choice_stack, cpu_choice_stack)
# 入力待ち
executor = concurrent.futures.ProcessPoolExecutor(max_workers=1) #ボタン入力用プロセス
while True:
executor.submit(sound_select)
if wait_level_input() == 0:
break
executor.shutdown()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=2) #スレッド用
# ゲーム開始
sound_start()
while True:
sound_clap()
executor.submit(func2) #独立してfunc2の実行
time.sleep(1)
# ポーズの段階
executor.submit(func1, cap, player_choice_stack, cpu_choice_stack) #独立してfunc1の実行
# 勝敗判定
event = judge_game(player_choice_stack, cpu_choice_stack)
if (event == Event.PLAYER_WIN) or (event == Event.CPU_WIN) or (event == Event.DEFEND_COUNT_EXCEED):
break #ループを抜ける
elif (event == Event.LACK_OF_CHARGE):
#サウンドを出す
elif (event == Event.DEFEND_COUNT_EXCEED):
#何かしらの処理
time.sleep(1)
# 変数eventの内容に応じてsound
sound_end(event)
```