Spaces:
Runtime error
Runtime error
remove duplicated code
Browse files- connectfour/app.py +19 -90
- connectfour/connect4.py +20 -26
connectfour/app.py
CHANGED
@@ -12,6 +12,7 @@ demo = gr.Blocks()
|
|
12 |
|
13 |
with demo:
|
14 |
connect4 = Connect4("You")
|
|
|
15 |
|
16 |
with gr.Row():
|
17 |
with gr.Column(scale=1):
|
@@ -33,20 +34,9 @@ with demo:
|
|
33 |
)
|
34 |
|
35 |
with gr.Row():
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
drop_token1_btn = gr.Button("X")
|
40 |
-
with gr.Column(scale=1, min_width=20):
|
41 |
-
drop_token2_btn = gr.Button("X")
|
42 |
-
with gr.Column(scale=1, min_width=20):
|
43 |
-
drop_token3_btn = gr.Button("X")
|
44 |
-
with gr.Column(scale=1, min_width=20):
|
45 |
-
drop_token4_btn = gr.Button("X")
|
46 |
-
with gr.Column(scale=1, min_width=20):
|
47 |
-
drop_token5_btn = gr.Button("X")
|
48 |
-
with gr.Column(scale=1, min_width=20):
|
49 |
-
drop_token6_btn = gr.Button("X")
|
50 |
|
51 |
who_plays_first.change(
|
52 |
connect4.init_env, who_plays_first, outputs=[output, game_state]
|
@@ -62,7 +52,7 @@ with demo:
|
|
62 |
|
63 |
def wait(game_state_value):
|
64 |
if game_state_value == "Game On":
|
65 |
-
time.sleep(
|
66 |
return gr.Checkbox.update(interactive=False)
|
67 |
else:
|
68 |
return gr.Checkbox.update(interactive=True)
|
@@ -79,89 +69,28 @@ with demo:
|
|
79 |
gr.Checkbox.update(interactive=True),
|
80 |
)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
).then(
|
86 |
-
wait, inputs=[game_state], outputs=who_plays_first
|
87 |
-
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
88 |
-
|
89 |
-
drop_token1_btn.click(
|
90 |
-
lambda: connect4.play(1),
|
91 |
-
outputs=[output, game_state],
|
92 |
-
).then(
|
93 |
-
wait, inputs=[game_state], outputs=who_plays_first
|
94 |
-
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
95 |
-
|
96 |
-
drop_token2_btn.click(
|
97 |
-
lambda: connect4.play(2),
|
98 |
-
outputs=[output, game_state],
|
99 |
-
).then(
|
100 |
-
wait, inputs=[game_state], outputs=who_plays_first
|
101 |
-
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
102 |
-
|
103 |
-
drop_token3_btn.click(
|
104 |
-
lambda: connect4.play(3),
|
105 |
-
outputs=[output, game_state],
|
106 |
-
).then(
|
107 |
-
wait, inputs=[game_state], outputs=who_plays_first
|
108 |
-
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
109 |
-
|
110 |
-
drop_token4_btn.click(
|
111 |
-
lambda: connect4.play(4),
|
112 |
-
outputs=[output, game_state],
|
113 |
-
).then(
|
114 |
-
wait, inputs=[game_state], outputs=who_plays_first
|
115 |
-
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
116 |
-
|
117 |
-
drop_token5_btn.click(
|
118 |
-
lambda: connect4.play(5),
|
119 |
-
outputs=[output, game_state],
|
120 |
-
).then(
|
121 |
-
wait, inputs=[game_state], outputs=who_plays_first
|
122 |
-
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
123 |
-
|
124 |
-
drop_token6_btn.click(
|
125 |
-
lambda: connect4.play(6),
|
126 |
-
outputs=[output, game_state],
|
127 |
-
).then(
|
128 |
-
wait, inputs=[game_state], outputs=who_plays_first
|
129 |
-
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
130 |
|
131 |
def game_state_change(value):
|
132 |
if value == "Game On":
|
133 |
-
return [
|
134 |
-
gr.Button.update(interactive=True),
|
135 |
-
gr.Button.update(interactive=True),
|
136 |
-
gr.Button.update(interactive=True),
|
137 |
-
gr.Button.update(interactive=True),
|
138 |
-
gr.Button.update(interactive=True),
|
139 |
-
gr.Button.update(interactive=True),
|
140 |
-
gr.Button.update(interactive=True),
|
141 |
-
]
|
142 |
else:
|
143 |
-
return [
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
|
153 |
game_state.change(
|
154 |
game_state_change,
|
155 |
game_state,
|
156 |
-
outputs=
|
157 |
-
drop_token0_btn,
|
158 |
-
drop_token1_btn,
|
159 |
-
drop_token2_btn,
|
160 |
-
drop_token3_btn,
|
161 |
-
drop_token4_btn,
|
162 |
-
drop_token5_btn,
|
163 |
-
drop_token6_btn,
|
164 |
-
],
|
165 |
)
|
166 |
|
167 |
|
|
|
12 |
|
13 |
with demo:
|
14 |
connect4 = Connect4("You")
|
15 |
+
drop_token_btns = []
|
16 |
|
17 |
with gr.Row():
|
18 |
with gr.Column(scale=1):
|
|
|
34 |
)
|
35 |
|
36 |
with gr.Row():
|
37 |
+
for i in range(7):
|
38 |
+
with gr.Column(min_width=20):
|
39 |
+
drop_token_btns.append(gr.Button("X", elem_id=i))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
who_plays_first.change(
|
42 |
connect4.init_env, who_plays_first, outputs=[output, game_state]
|
|
|
52 |
|
53 |
def wait(game_state_value):
|
54 |
if game_state_value == "Game On":
|
55 |
+
time.sleep(0.7)
|
56 |
return gr.Checkbox.update(interactive=False)
|
57 |
else:
|
58 |
return gr.Checkbox.update(interactive=True)
|
|
|
69 |
gr.Checkbox.update(interactive=True),
|
70 |
)
|
71 |
|
72 |
+
def click_column(evt: gr.EventData):
|
73 |
+
output, game_state = connect4.play(int(evt.target.elem_id))
|
74 |
+
return output, game_state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
def game_state_change(value):
|
77 |
if value == "Game On":
|
78 |
+
return [gr.Button.update(interactive=True)] * 7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
else:
|
80 |
+
return [gr.Button.update(interactive=False)] * 7
|
81 |
+
|
82 |
+
for i in range(7):
|
83 |
+
drop_token_btns[i].click(
|
84 |
+
click_column,
|
85 |
+
outputs=[output, game_state],
|
86 |
+
).then(
|
87 |
+
wait, inputs=[game_state], outputs=who_plays_first
|
88 |
+
).then(bot, inputs=[game_state], outputs=[output, game_state, who_plays_first])
|
89 |
|
90 |
game_state.change(
|
91 |
game_state_change,
|
92 |
game_state,
|
93 |
+
outputs=drop_token_btns,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
)
|
95 |
|
96 |
|
connectfour/connect4.py
CHANGED
@@ -30,28 +30,28 @@ class Connect4:
|
|
30 |
return flatten_obs[np.newaxis, ...].astype(np.float32)
|
31 |
|
32 |
def play(self, action=None):
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
return self.blue_screen()
|
35 |
|
36 |
-
if self.human != self.current_player_id:
|
37 |
-
action = self.session.run(
|
38 |
-
["output"],
|
39 |
-
{
|
40 |
-
"obs": self.flatten_observation(
|
41 |
-
self.env.observe(self.current_player_id)
|
42 |
-
),
|
43 |
-
"state_ins": [],
|
44 |
-
},
|
45 |
-
)
|
46 |
-
action = int(np.argmax(action[0]))
|
47 |
-
|
48 |
-
if action not in self.legal_moves:
|
49 |
-
action = np.random.choice(self.legal_moves)
|
50 |
-
|
51 |
-
self.env.step(action)
|
52 |
-
|
53 |
-
return self.render_and_state
|
54 |
-
|
55 |
@property
|
56 |
def current_player_id(self):
|
57 |
return self.env.agent_selection
|
@@ -90,9 +90,3 @@ class Connect4:
|
|
90 |
error_screen = np.load(f)
|
91 |
|
92 |
return (error_screen, "Restart the Game")
|
93 |
-
|
94 |
-
def has_erroneous_state(self):
|
95 |
-
obs = self.env.observe(self.current_player_id)
|
96 |
-
if len(list(obs.keys())) == 0:
|
97 |
-
return True
|
98 |
-
return False
|
|
|
30 |
return flatten_obs[np.newaxis, ...].astype(np.float32)
|
31 |
|
32 |
def play(self, action=None):
|
33 |
+
try:
|
34 |
+
if self.human != self.current_player_id:
|
35 |
+
action = self.session.run(
|
36 |
+
["output"],
|
37 |
+
{
|
38 |
+
"obs": self.flatten_observation(
|
39 |
+
self.env.observe(self.current_player_id)
|
40 |
+
),
|
41 |
+
"state_ins": [],
|
42 |
+
},
|
43 |
+
)
|
44 |
+
action = int(np.argmax(action[0]))
|
45 |
+
|
46 |
+
if action not in self.legal_moves:
|
47 |
+
action = np.random.choice(self.legal_moves)
|
48 |
+
|
49 |
+
self.env.step(action)
|
50 |
+
|
51 |
+
return self.render_and_state
|
52 |
+
except:
|
53 |
return self.blue_screen()
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
@property
|
56 |
def current_player_id(self):
|
57 |
return self.env.agent_selection
|
|
|
90 |
error_screen = np.load(f)
|
91 |
|
92 |
return (error_screen, "Restart the Game")
|
|
|
|
|
|
|
|
|
|
|
|