alperugurcan commited on
Commit
c78c7d0
·
verified ·
1 Parent(s): fa59ed6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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((6, 7), dtype=int)
7
+
8
+ def make_move(board, column, player):
9
+ """Make a move on the board"""
10
+ for row in range(5, -1, -1):
11
+ if board[row][column] == 0:
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
+ # 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
30
+ any(np.all(line[i:i+4] == 2) for i in range(len(line)-3))))
31
+
32
+ # Horizontal
33
+ for row in board:
34
+ if check_line(row):
35
+ return True
36
+
37
+ # Vertical
38
+ for col in board.T:
39
+ if check_line(col):
40
+ return True
41
+
42
+ # Diagonals
43
+ for offset in range(-2, 4):
44
+ diag = np.diagonal(board, offset)
45
+ if check_line(diag):
46
+ return True
47
+ diag = np.diagonal(np.fliplr(board), offset)
48
+ if check_line(diag):
49
+ return True
50
+
51
+ return False
52
+
53
+ def play_game(board, col, state):
54
+ if state["game_over"]:
55
+ return board, "Game is over! Click 'New Game' to play again."
56
+
57
+ # Player move
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})
65
+ obs = Observation({"board": board.flatten().tolist(), "mark": 2})
66
+
67
+ ai_col = my_agent(obs, config)
68
+ board = make_move(board, ai_col, 2)
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:
78
+ gr.Markdown("# Play Connect Four Against AI")
79
+ gr.Markdown("You are Red (🔴), AI is Yellow (🟡)")
80
+
81
+ state = gr.State({"game_over": False})
82
+ board = gr.DataFrame(
83
+ format_board(initialize_game()),
84
+ headers=False,
85
+ interactive=False,
86
+ show_label=False
87
+ )
88
+ message = gr.Textbox(value="Your turn!", label="Status")
89
+
90
+ with gr.Row():
91
+ buttons = [gr.Button(str(i)) for i in range(7)]
92
+
93
+ new_game = gr.Button("New Game")
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,
101
+ outputs=[board, message, state]
102
+ )
103
+
104
+ for i, button in enumerate(buttons):
105
+ button.click(
106
+ play_game,
107
+ inputs=[board, gr.Number(value=i, visible=False), state],
108
+ outputs=[board, message]
109
+ )
110
+
111
+ return demo
112
+
113
+ demo = create_ui()
114
+ demo.launch()