Spaces:
Runtime error
Runtime error
alperugurcan
commited on
Commit
•
648fc8c
1
Parent(s):
2de04bc
Update app.py
Browse files
app.py
CHANGED
@@ -2,72 +2,80 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from game_logic import my_agent, Configuration, Observation
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def initialize_game():
|
6 |
-
return np.zeros(
|
7 |
|
8 |
def make_move(board, column, player):
|
9 |
"""Make a move on the board"""
|
10 |
-
for row in range(
|
11 |
-
if board[row][column] ==
|
12 |
board[row][column] = player
|
13 |
return board
|
14 |
return board
|
15 |
|
16 |
-
def format_board(board):
|
17 |
-
"""Convert board to player-friendly visualization"""
|
18 |
-
symbols = {0: "⚪", 1: "🔴", 2: "🟡"}
|
19 |
-
return [[symbols[cell] for cell in row] for row in board]
|
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
|
28 |
-
any(np.all(line[i:i+4] == 2) for i in range(len(line)-3))))
|
29 |
-
|
30 |
# Horizontal
|
31 |
-
for row in
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
|
35 |
# Vertical
|
36 |
-
for
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
|
40 |
# Diagonals
|
41 |
-
for
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
return False
|
50 |
|
|
|
|
|
|
|
|
|
51 |
def play_game(board, col, state):
|
52 |
if state["game_over"]:
|
53 |
return board, "Game is over! Click 'New Game' to play again."
|
54 |
|
55 |
-
# Convert
|
56 |
-
board_array = np.array([[0 if cell ==
|
57 |
-
|
|
|
58 |
|
59 |
# Player move
|
60 |
-
board_array = make_move(board_array, col,
|
61 |
if check_winner(board_array):
|
62 |
state["game_over"] = True
|
63 |
return format_board(board_array), "You win! 🎉"
|
64 |
|
65 |
# AI move
|
66 |
-
config = Configuration({"rows":
|
67 |
-
obs = Observation({"board": board_array.flatten().tolist(), "mark":
|
68 |
|
69 |
ai_col = my_agent(obs, config)
|
70 |
-
board_array = make_move(board_array, ai_col,
|
71 |
|
72 |
if check_winner(board_array):
|
73 |
state["game_over"] = True
|
@@ -75,6 +83,34 @@ def play_game(board, col, state):
|
|
75 |
|
76 |
return format_board(board_array), "Your turn!"
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
def create_ui():
|
79 |
with gr.Blocks(css=css) as demo:
|
80 |
gr.Markdown("# Play Connect Four Against AI")
|
@@ -117,34 +153,5 @@ def create_ui():
|
|
117 |
|
118 |
return demo
|
119 |
|
120 |
-
# CSS definition
|
121 |
-
css = """
|
122 |
-
#board {
|
123 |
-
max-width: 400px;
|
124 |
-
margin: 20px auto;
|
125 |
-
}
|
126 |
-
#board table {
|
127 |
-
width: 100%;
|
128 |
-
table-layout: fixed;
|
129 |
-
}
|
130 |
-
#board td {
|
131 |
-
text-align: center;
|
132 |
-
font-size: 32px;
|
133 |
-
padding: 12px;
|
134 |
-
width: 14.28%; /* 100% / 7 columns */
|
135 |
-
}
|
136 |
-
.button-row {
|
137 |
-
max-width: 400px;
|
138 |
-
margin: 0 auto;
|
139 |
-
display: flex;
|
140 |
-
justify-content: space-between;
|
141 |
-
gap: 10px;
|
142 |
-
}
|
143 |
-
.button-row button {
|
144 |
-
flex: 1;
|
145 |
-
min-width: 40px;
|
146 |
-
}
|
147 |
-
"""
|
148 |
-
|
149 |
demo = create_ui()
|
150 |
-
demo.launch()
|
|
|
2 |
import numpy as np
|
3 |
from game_logic import my_agent, Configuration, Observation
|
4 |
|
5 |
+
# Constants
|
6 |
+
EMPTY = 0
|
7 |
+
PLAYER = 1
|
8 |
+
AI = 2
|
9 |
+
SYMBOLS = {EMPTY: "⚪", PLAYER: "🔴", AI: "🟡"}
|
10 |
+
BOARD_SIZE = (6, 7)
|
11 |
+
WIN_LENGTH = 4
|
12 |
+
|
13 |
def initialize_game():
|
14 |
+
return np.zeros(BOARD_SIZE, dtype=int)
|
15 |
|
16 |
def make_move(board, column, player):
|
17 |
"""Make a move on the board"""
|
18 |
+
for row in range(BOARD_SIZE[0] - 1, -1, -1):
|
19 |
+
if board[row][column] == EMPTY:
|
20 |
board[row][column] = player
|
21 |
return board
|
22 |
return board
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
def check_winner(board):
|
25 |
"""Check if there's a winner"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Horizontal
|
27 |
+
for row in range(BOARD_SIZE[0]):
|
28 |
+
for col in range(BOARD_SIZE[1] - 3):
|
29 |
+
window = board[row, col:col + 4]
|
30 |
+
if np.all(window == PLAYER) or np.all(window == AI):
|
31 |
+
return True
|
32 |
|
33 |
# Vertical
|
34 |
+
for row in range(BOARD_SIZE[0] - 3):
|
35 |
+
for col in range(BOARD_SIZE[1]):
|
36 |
+
window = board[row:row + 4, col]
|
37 |
+
if np.all(window == PLAYER) or np.all(window == AI):
|
38 |
+
return True
|
39 |
|
40 |
# Diagonals
|
41 |
+
for row in range(BOARD_SIZE[0] - 3):
|
42 |
+
for col in range(BOARD_SIZE[1] - 3):
|
43 |
+
# Positive diagonal
|
44 |
+
window = board[range(row, row + 4), range(col, col + 4)]
|
45 |
+
if np.all(window == PLAYER) or np.all(window == AI):
|
46 |
+
return True
|
47 |
+
# Negative diagonal
|
48 |
+
window = board[range(row, row + 4), range(col + 3, col - 1, -1)]
|
49 |
+
if np.all(window == PLAYER) or np.all(window == AI):
|
50 |
+
return True
|
51 |
|
52 |
return False
|
53 |
|
54 |
+
def format_board(board):
|
55 |
+
"""Convert board to emoji representation"""
|
56 |
+
return [[SYMBOLS[cell] for cell in row] for row in board]
|
57 |
+
|
58 |
def play_game(board, col, state):
|
59 |
if state["game_over"]:
|
60 |
return board, "Game is over! Click 'New Game' to play again."
|
61 |
|
62 |
+
# Convert display format to game logic format
|
63 |
+
board_array = np.array([[0 if cell == SYMBOLS[EMPTY] else
|
64 |
+
1 if cell == SYMBOLS[PLAYER] else 2
|
65 |
+
for cell in row] for row in board.values.tolist()])
|
66 |
|
67 |
# Player move
|
68 |
+
board_array = make_move(board_array, col, PLAYER)
|
69 |
if check_winner(board_array):
|
70 |
state["game_over"] = True
|
71 |
return format_board(board_array), "You win! 🎉"
|
72 |
|
73 |
# AI move
|
74 |
+
config = Configuration({"rows": BOARD_SIZE[0], "columns": BOARD_SIZE[1], "inarow": WIN_LENGTH})
|
75 |
+
obs = Observation({"board": board_array.flatten().tolist(), "mark": AI})
|
76 |
|
77 |
ai_col = my_agent(obs, config)
|
78 |
+
board_array = make_move(board_array, ai_col, AI)
|
79 |
|
80 |
if check_winner(board_array):
|
81 |
state["game_over"] = True
|
|
|
83 |
|
84 |
return format_board(board_array), "Your turn!"
|
85 |
|
86 |
+
css = """
|
87 |
+
#board {
|
88 |
+
max-width: 400px;
|
89 |
+
margin: 20px auto;
|
90 |
+
}
|
91 |
+
#board table {
|
92 |
+
width: 100%;
|
93 |
+
table-layout: fixed;
|
94 |
+
}
|
95 |
+
#board td {
|
96 |
+
text-align: center;
|
97 |
+
font-size: 32px;
|
98 |
+
padding: 12px;
|
99 |
+
width: 14.28%;
|
100 |
+
}
|
101 |
+
.button-row {
|
102 |
+
max-width: 400px;
|
103 |
+
margin: 0 auto;
|
104 |
+
display: flex;
|
105 |
+
justify-content: space-between;
|
106 |
+
gap: 10px;
|
107 |
+
}
|
108 |
+
.button-row button {
|
109 |
+
flex: 1;
|
110 |
+
min-width: 40px;
|
111 |
+
}
|
112 |
+
"""
|
113 |
+
|
114 |
def create_ui():
|
115 |
with gr.Blocks(css=css) as demo:
|
116 |
gr.Markdown("# Play Connect Four Against AI")
|
|
|
153 |
|
154 |
return demo
|
155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
demo = create_ui()
|
157 |
+
demo.launch()
|