-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtennis.py
166 lines (132 loc) · 5.53 KB
/
tennis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import asyncio
import curses
import random
from kiwi_cogs import Machine, StateType
def check_if_score_is_deuce(context, _):
"""Checks if the score is deuce in a tennis game
:param context: The current context of the game, including the scores of both players
:type context: dict
:returns: True if the score is deuce, False otherwise
:rtype: bool
"""
return context["player1_score"] >= 3 and context["player2_score"] == context["player1_score"]
def check_if_score_is_advantage(context, _):
"""Checks if the score is advantage for one of the players in a tennis game
:param context: The current context of the game, including the scores of both players
:type context: dict
:returns: True if the score is advantage for one player, False otherwise
:rtype: bool
"""
if context["player1_score"] > context["player2_score"]:
return context["player1_score"] >= 4 and context["player1_score"] - context["player2_score"] == 1
else:
return context["player2_score"] >= 4 and context["player2_score"] - context["player1_score"] == 1
def check_if_score_is_game(context, _):
"""Checks if the score is a game for one of the players in a tennis game
:param context: The current context of the game, including the scores of both players
:type context: dict
:returns: True if the score is a game for one player, False otherwise
:rtype: bool
"""
if context["player1_score"] > context["player2_score"]:
return context["player1_score"] >= 4 and context["player1_score"] - context["player2_score"] >= 2
else:
return context["player2_score"] >= 4 and context["player2_score"] - context["player1_score"] >= 2
def increment_player1_score(context, _):
context["player1_score"] += 1
def increment_player2_score(context, _):
context["player2_score"] += 1
config = {
"name": "tennis_game",
"initial": "serving",
"context": {"player1_score": 0, "player2_score": 0},
"states": {
"serving": {
"transitions": [
{"target": "deuce", "cond": "isDeuce"}, # use a named guard for transitions
{"target": "advantage", "cond": "isAdvantage"},
{"target": "game", "cond": "isGame"},
],
"events": {
"PLAYER1_SCORES": {"actions": "incPlayer1Score"}, # use a named action for events
"PLAYER2_SCORES": {"actions": "incPlayer2Score"},
},
},
"deuce": {
"transitions": [
{"target": "advantage", "cond": "isAdvantage"},
{"target": "game", "cond": "isGame"},
],
"events": {
"PLAYER1_SCORES": {"actions": increment_player1_score}, # use a function for events as well!
"PLAYER2_SCORES": {"actions": increment_player2_score},
},
},
"advantage": {
"transitions": [
{"target": "deuce", "cond": check_if_score_is_deuce}, # Functions can be used as well
{"target": "game", "cond": check_if_score_is_game},
],
"events": {
"PLAYER1_SCORES": {"actions": increment_player1_score},
"PLAYER2_SCORES": {"actions": increment_player2_score},
},
},
"game": {
"transitions": [
{
"target": "player1_wins",
"cond": lambda context, _: context["player1_score"] > context["player2_score"],
}, # any callable can be used as a condition
{
"target": "player2_wins",
"cond": lambda context, _: context["player2_score"] > context["player1_score"],
},
],
},
"player1_wins": {"type": "final"}, # final states are used to indicate that the machine is finished
"player2_wins": {"type": "final"},
},
"guards": { # Register the guards, reusable conditions that can be used in transitions
"isDeuce": check_if_score_is_deuce,
"isAdvantage": check_if_score_is_advantage,
"isGame": check_if_score_is_game,
},
"actions": {
"incPlayer1Score": increment_player1_score,
"incPlayer2Score": increment_player2_score,
},
}
async def get_machine():
return await Machine.create(config)
def print_score(stdscr, state, score1, score2):
stdscr.clear()
# Create a new window for the scores, with a border
score_window = curses.newwin(5, 30, 3, 0)
score_window.box()
# Print scores
score_window.addstr(1, 1, f"State: {state}")
score_window.addstr(2, 1, f"Player 1: {score1}")
score_window.addstr(3, 1, f"Player 2: {score2}")
score_window.refresh()
async def tennis_game(stdscr):
machine = await get_machine()
events = ("PLAYER1_SCORES", "PLAYER2_SCORES")
while machine.state.type != StateType.final:
score_1 = machine.context["player1_score"]
score_2 = machine.context["player2_score"]
state = machine.state.value
print_score(stdscr, state, score_1, score_2)
event = random.choice(events) # randomly select a player to score
await asyncio.sleep(0.2)
await machine.event(event)
score_1 = machine.context["player1_score"]
score_2 = machine.context["player2_score"]
state = machine.state.value
print_score(stdscr, state, score_1, score_2)
await asyncio.sleep(30)
def run(stdscr):
asyncio.run(tennis_game(stdscr))
if __name__ == "__main__":
# Initialize curses
curses.wrapper(run)