CC_flows / RockPaperScissorsJudge.py
martinjosifoski's picture
Update import.
c31960c
raw
history blame
1.89 kB
from flows.base_flows import Flow
from typing import Dict
from .RockPaperScissorsPlayer import RockPaperScissorsPlayer
class RockPaperScissorsJudge(Flow):
def __init__(self, **kwargs):
super(RockPaperScissorsJudge, self).__init__(**kwargs)
self.flow_state["A"] = RockPaperScissorsPlayer(name="Player A", description="RockPaperScissorsPlayer")
self.flow_state["B"] = RockPaperScissorsPlayer(name="Player B", description="RockPaperScissorsPlayer")
self.flow_state["A_score"] = 0
self.flow_state["B_score"] = 0
self.flow_state["n_party_played"] = 0
def run(self, input_data, expected_outputs) -> Dict:
flow_a = self.flow_state["A"]
flow_b = self.flow_state["B"]
for _ in range(3):
A_task = self.package_task_message(flow_a, "run", {}, expected_outputs=["choice"])
B_task = self.package_task_message(flow_b, "run", {}, expected_outputs=["choice"])
# play another round
A_output = flow_a(A_task)
self._log_message(A_output)
B_output = flow_b(B_task)
self._log_message(B_output)
A_choice = A_output.data["choice"]
B_choice = B_output.data["choice"]
self._update_state({"n_party_played": self.flow_state["n_party_played"] + 1})
if A_choice == B_choice:
# neither has won
pass
elif (A_choice == "rock" and B_choice == "scissors"
or A_choice == "paper" and B_choice == "rock"
or A_choice == "scissors" and B_choice == "paper"):
self._update_state({"A_score": self.flow_state["A_score"] + 1})
else:
self._update_state({"B_score": self.flow_state["B_score"] + 1})
return self._get_keys_from_state(expected_outputs, allow_class_namespace=False)