Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
|
4 |
+
# Simulated AI models for checkers
|
5 |
+
class BasicAI:
|
6 |
+
def move(self, board):
|
7 |
+
# Basic AI makes a random valid move
|
8 |
+
valid_moves = self.get_valid_moves(board)
|
9 |
+
return random.choice(valid_moves) if valid_moves else None
|
10 |
+
|
11 |
+
def get_valid_moves(self, board):
|
12 |
+
# Simulate valid moves as random pairs of coordinates
|
13 |
+
return [(random.randint(0, 7), random.randint(0, 7)) for _ in range(random.randint(1, 5))]
|
14 |
+
|
15 |
+
class AdvancedAI:
|
16 |
+
def move(self, board):
|
17 |
+
# Advanced AI prioritizes moves with a placeholder strategy
|
18 |
+
valid_moves = self.get_valid_moves(board)
|
19 |
+
return valid_moves[0] if valid_moves else None
|
20 |
+
|
21 |
+
def get_valid_moves(self, board):
|
22 |
+
# Simulate valid moves as random pairs of coordinates
|
23 |
+
return [(random.randint(0, 7), random.randint(0, 7)) for _ in range(random.randint(1, 5))]
|
24 |
+
|
25 |
+
# Initialize AI models
|
26 |
+
ai_models = {
|
27 |
+
"Basic AI": BasicAI(),
|
28 |
+
"Advanced AI": AdvancedAI()
|
29 |
+
}
|
30 |
+
|
31 |
+
def play_ai_vs_ai(ai1_name, ai2_name, rounds):
|
32 |
+
ai1 = ai_models[ai1_name]
|
33 |
+
ai2 = ai_models[ai2_name]
|
34 |
+
|
35 |
+
board = [["." for _ in range(8)] for _ in range(8)] # Simplified 8x8 board
|
36 |
+
history = []
|
37 |
+
|
38 |
+
for i in range(rounds):
|
39 |
+
ai1_move = ai1.move(board)
|
40 |
+
ai2_move = ai2.move(board)
|
41 |
+
history.append(f"Round {i + 1}: {ai1_name} -> {ai1_move}, {ai2_name} -> {ai2_move}")
|
42 |
+
|
43 |
+
return "\n".join(history)
|
44 |
+
|
45 |
+
# Gradio Interface
|
46 |
+
def build_interface():
|
47 |
+
with gr.Blocks() as interface:
|
48 |
+
gr.Markdown("""
|
49 |
+
# AI vs AI Checkers Game
|
50 |
+
Watch different AI models compete against each other in a simulated game of checkers. Select the models and number of rounds to see how they perform!
|
51 |
+
""")
|
52 |
+
|
53 |
+
with gr.Row():
|
54 |
+
ai1_dropdown = gr.Dropdown(choices=list(ai_models.keys()), label="Select AI Model for Player 1", value="Basic AI")
|
55 |
+
ai2_dropdown = gr.Dropdown(choices=list(ai_models.keys()), label="Select AI Model for Player 2", value="Advanced AI")
|
56 |
+
|
57 |
+
rounds_slider = gr.Slider(minimum=1, maximum=20, step=1, value=5, label="Number of Rounds")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
start_button = gr.Button("Start AI vs AI")
|
61 |
+
|
62 |
+
output_box = gr.Textbox(label="Game History", lines=10)
|
63 |
+
|
64 |
+
start_button.click(
|
65 |
+
play_ai_vs_ai,
|
66 |
+
inputs=[ai1_dropdown, ai2_dropdown, rounds_slider],
|
67 |
+
outputs=output_box
|
68 |
+
)
|
69 |
+
|
70 |
+
gr.Markdown("""
|
71 |
+
### How It Works
|
72 |
+
- **Basic AI**: Makes random valid moves.
|
73 |
+
- **Advanced AI**: Uses a placeholder strategy to prioritize specific moves.
|
74 |
+
|
75 |
+
Select models, adjust the rounds, and press "Start AI vs AI" to see the results!
|
76 |
+
""")
|
77 |
+
|
78 |
+
return interface
|
79 |
+
|
80 |
+
# Launch the app
|
81 |
+
interface = build_interface()
|
82 |
+
interface.launch()
|