GeminiAi commited on
Commit
b0fb74a
·
verified ·
1 Parent(s): ffafaf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import random
3
 
4
- # Initialize board, pieces, and game state
5
  class CheckersGame:
6
  def __init__(self):
7
  self.board = self.create_board()
@@ -19,10 +19,10 @@ class CheckersGame:
19
  board[row][col] = 'white'
20
  return board
21
 
22
- def make_move(self):
23
  # AI logic: Random move for the sake of simplicity
24
  if self.game_over:
25
- return self.board, "Game Over! Restart to play again."
26
 
27
  valid_moves = self.get_valid_moves(self.turn)
28
  if valid_moves:
@@ -30,10 +30,10 @@ class CheckersGame:
30
  self.board[move[2]][move[3]] = self.board[move[0]][move[1]]
31
  self.board[move[0]][move[1]] = ''
32
  self.turn = 'white' if self.turn == 'red' else 'red'
33
- return self.board, f"{self.turn.capitalize()} moved."
34
  else:
35
  self.game_over = True
36
- return self.board, f"Game Over! {self.turn.capitalize()} has no moves left."
37
 
38
  def get_valid_moves(self, color):
39
  valid_moves = []
@@ -54,27 +54,39 @@ class CheckersGame:
54
  board_repr += ' '.join([cell[0].upper() if cell else '.' for cell in row]) + '\n'
55
  return board_repr
56
 
57
- # Create a Checkers game instance
58
  game = CheckersGame()
59
 
60
  # Function to handle the game move and display
61
- def play_checkers():
62
- board, status = game.make_move()
63
- return game.render_board(), status
64
 
65
  # Gradio interface
66
  def create_ui():
67
  with gr.Blocks() as demo:
68
  gr.Markdown("## AI vs AI - Watch the Game!")
69
 
70
- # Display the board and game status
71
  with gr.Row():
72
- board_display = gr.Textbox(label="Game Board", interactive=False, value=game.render_board())
73
- game_status = gr.Textbox(label="Game Status", interactive=False, value="Game is running...")
 
74
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  # Button to simulate a move and update the board
76
  play_button = gr.Button("AI makes a move")
77
- play_button.click(play_checkers, outputs=[board_display, game_status])
78
 
79
  return demo
80
 
 
1
  import gradio as gr
2
  import random
3
 
4
+ # Initialize the board, pieces, and game state
5
  class CheckersGame:
6
  def __init__(self):
7
  self.board = self.create_board()
 
19
  board[row][col] = 'white'
20
  return board
21
 
22
+ def make_move(self, model_1, model_2):
23
  # AI logic: Random move for the sake of simplicity
24
  if self.game_over:
25
+ return self.render_board(), "Game Over! Restart to play again."
26
 
27
  valid_moves = self.get_valid_moves(self.turn)
28
  if valid_moves:
 
30
  self.board[move[2]][move[3]] = self.board[move[0]][move[1]]
31
  self.board[move[0]][move[1]] = ''
32
  self.turn = 'white' if self.turn == 'red' else 'red'
33
+ return self.render_board(), f"{self.turn.capitalize()} moved using {model_1 if self.turn == 'white' else model_2}."
34
  else:
35
  self.game_over = True
36
+ return self.render_board(), f"Game Over! {self.turn.capitalize()} has no moves left."
37
 
38
  def get_valid_moves(self, color):
39
  valid_moves = []
 
54
  board_repr += ' '.join([cell[0].upper() if cell else '.' for cell in row]) + '\n'
55
  return board_repr
56
 
57
+ # Initialize the Checkers game instance
58
  game = CheckersGame()
59
 
60
  # Function to handle the game move and display
61
+ def play_checkers(model_1, model_2):
62
+ board, status = game.make_move(model_1, model_2)
63
+ return board, status
64
 
65
  # Gradio interface
66
  def create_ui():
67
  with gr.Blocks() as demo:
68
  gr.Markdown("## AI vs AI - Watch the Game!")
69
 
 
70
  with gr.Row():
71
+ # Display the board and game status
72
+ board_display = gr.Textbox(label="Game Board", interactive=False, value=game.render_board(), lines=8, max_lines=8)
73
+ game_status = gr.Textbox(label="Game Status", interactive=False, value="Game is running...", lines=2)
74
 
75
+ with gr.Row():
76
+ model_dropdown_1 = gr.Dropdown(
77
+ choices=["GPT-2", "DistilGPT-2", "BERT"],
78
+ label="AI Model for Player 1 (White)",
79
+ value="GPT-2"
80
+ )
81
+ model_dropdown_2 = gr.Dropdown(
82
+ choices=["GPT-2", "DistilGPT-2", "BERT"],
83
+ label="AI Model for Player 2 (Red)",
84
+ value="DistilGPT-2"
85
+ )
86
+
87
  # Button to simulate a move and update the board
88
  play_button = gr.Button("AI makes a move")
89
+ play_button.click(play_checkers, inputs=[model_dropdown_1, model_dropdown_2], outputs=[board_display, game_status])
90
 
91
  return demo
92