Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,10 +20,8 @@ def format_board(board):
|
|
20 |
|
21 |
def check_winner(board):
|
22 |
"""Check if there's a winner"""
|
23 |
-
# Convert to numpy array for easier checking
|
24 |
board = np.array(board)
|
25 |
|
26 |
-
# Check horizontal, vertical and diagonal wins
|
27 |
def check_line(line):
|
28 |
return (len(line) >= 4 and
|
29 |
(any(np.all(line[i:i+4] == 1) for i in range(len(line)-3)) or
|
@@ -58,7 +56,7 @@ def play_game(board, col, state):
|
|
58 |
board = make_move(board, col, 1)
|
59 |
if check_winner(board):
|
60 |
state["game_over"] = True
|
61 |
-
return board, "You win! 🎉"
|
62 |
|
63 |
# AI move
|
64 |
config = Configuration({"rows": 6, "columns": 7, "inarow": 4})
|
@@ -69,9 +67,9 @@ def play_game(board, col, state):
|
|
69 |
|
70 |
if check_winner(board):
|
71 |
state["game_over"] = True
|
72 |
-
return board, "AI wins! 🤖"
|
73 |
|
74 |
-
return board, "Your turn!"
|
75 |
|
76 |
def create_ui():
|
77 |
with gr.Blocks() as demo:
|
@@ -79,12 +77,17 @@ def create_ui():
|
|
79 |
gr.Markdown("You are Red (🔴), AI is Yellow (🟡)")
|
80 |
|
81 |
state = gr.State({"game_over": False})
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
85 |
interactive=False,
|
86 |
-
show_label=False
|
|
|
|
|
|
|
87 |
)
|
|
|
88 |
message = gr.Textbox(value="Your turn!", label="Status")
|
89 |
|
90 |
with gr.Row():
|
@@ -94,7 +97,7 @@ def create_ui():
|
|
94 |
|
95 |
def reset_game():
|
96 |
state = {"game_over": False}
|
97 |
-
return initialize_game(), "Your turn!", state
|
98 |
|
99 |
new_game.click(
|
100 |
reset_game,
|
@@ -110,5 +113,18 @@ def create_ui():
|
|
110 |
|
111 |
return demo
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
demo = create_ui()
|
114 |
-
demo.launch()
|
|
|
20 |
|
21 |
def check_winner(board):
|
22 |
"""Check if there's a winner"""
|
|
|
23 |
board = np.array(board)
|
24 |
|
|
|
25 |
def check_line(line):
|
26 |
return (len(line) >= 4 and
|
27 |
(any(np.all(line[i:i+4] == 1) for i in range(len(line)-3)) or
|
|
|
56 |
board = make_move(board, col, 1)
|
57 |
if check_winner(board):
|
58 |
state["game_over"] = True
|
59 |
+
return format_board(board), "You win! 🎉"
|
60 |
|
61 |
# AI move
|
62 |
config = Configuration({"rows": 6, "columns": 7, "inarow": 4})
|
|
|
67 |
|
68 |
if check_winner(board):
|
69 |
state["game_over"] = True
|
70 |
+
return format_board(board), "AI wins! 🤖"
|
71 |
|
72 |
+
return format_board(board), "Your turn!"
|
73 |
|
74 |
def create_ui():
|
75 |
with gr.Blocks() as demo:
|
|
|
77 |
gr.Markdown("You are Red (🔴), AI is Yellow (🟡)")
|
78 |
|
79 |
state = gr.State({"game_over": False})
|
80 |
+
|
81 |
+
# Using Grid instead of DataFrame
|
82 |
+
board = gr.Dataframe(
|
83 |
+
value=format_board(initialize_game()),
|
84 |
interactive=False,
|
85 |
+
show_label=False,
|
86 |
+
headers=None,
|
87 |
+
wrap=True,
|
88 |
+
elem_id="board"
|
89 |
)
|
90 |
+
|
91 |
message = gr.Textbox(value="Your turn!", label="Status")
|
92 |
|
93 |
with gr.Row():
|
|
|
97 |
|
98 |
def reset_game():
|
99 |
state = {"game_over": False}
|
100 |
+
return format_board(initialize_game()), "Your turn!", state
|
101 |
|
102 |
new_game.click(
|
103 |
reset_game,
|
|
|
113 |
|
114 |
return demo
|
115 |
|
116 |
+
# Add some CSS to make the board look better
|
117 |
+
css = """
|
118 |
+
#board {
|
119 |
+
max-width: 400px;
|
120 |
+
margin: 0 auto;
|
121 |
+
}
|
122 |
+
#board td {
|
123 |
+
text-align: center;
|
124 |
+
font-size: 24px;
|
125 |
+
padding: 8px;
|
126 |
+
}
|
127 |
+
"""
|
128 |
+
|
129 |
demo = create_ui()
|
130 |
+
demo.launch(css=css)
|