Sanjayraju30 commited on
Commit
a099b2f
·
verified ·
1 Parent(s): 6a59296

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -47
app.py CHANGED
@@ -1,53 +1,32 @@
1
  import gradio as gr
2
-
3
- # Initial empty board
4
- board = [""] * 9
5
- current_player = ["X"]
6
- winner = [None]
7
-
8
- def check_winner(b):
9
- win_combos = [
10
- [0, 1, 2], [3, 4, 5], [6, 7, 8], # rows
11
- [0, 3, 6], [1, 4, 7], [2, 5, 8], # cols
12
- [0, 4, 8], [2, 4, 6] # diagonals
13
- ]
14
- for combo in win_combos:
15
- if b[combo[0]] and b[combo[0]] == b[combo[1]] == b[combo[2]]:
16
- return b[combo[0]]
17
- if "" not in b:
18
- return "Draw"
19
- return None
20
-
21
- def make_move(i):
22
- if board[i] or winner[0]:
23
- return update_game()
24
- board[i] = current_player[0]
25
- result = check_winner(board)
26
- if result:
27
- winner[0] = result
28
  else:
29
- current_player[0] = "O" if current_player[0] == "X" else "X"
30
- return update_game()
31
-
32
- def update_game():
33
- status = f"Winner: {winner[0]}" if winner[0] else f"Turn: {current_player[0]}"
34
- return board, status
35
-
36
- def reset_game():
37
- for i in range(9): board[i] = ""
38
- current_player[0] = "X"
39
- winner[0] = None
40
- return update_game()
41
 
42
- with gr.Blocks() as demo:
43
- gr.Markdown("## 🎮 Tic Tac Toe (Hugging Face Version)")
44
- btns = [gr.Button("", elem_id=f"btn{i}") for i in range(9)]
45
- status = gr.Markdown("Turn: X")
46
- reset = gr.Button("Reset Game")
47
 
48
- for i, btn in enumerate(btns):
49
- btn.click(make_move, inputs=[gr.Number(value=i, visible=False)], outputs=[*btns, status])
 
 
 
 
 
50
 
51
- reset.click(fn=reset_game, inputs=[], outputs=[*btns, status])
52
 
53
- demo.launch()
 
1
  import gradio as gr
2
+ import random
3
+
4
+ choices = ["Rock", "Paper", "Scissors"]
5
+
6
+ def play_rps(user_choice):
7
+ bot_choice = random.choice(choices)
8
+ result = ""
9
+
10
+ if user_choice == bot_choice:
11
+ result = "It's a draw!"
12
+ elif (
13
+ (user_choice == "Rock" and bot_choice == "Scissors") or
14
+ (user_choice == "Paper" and bot_choice == "Rock") or
15
+ (user_choice == "Scissors" and bot_choice == "Paper")
16
+ ):
17
+ result = "You win! 🎉"
 
 
 
 
 
 
 
 
 
 
18
  else:
19
+ result = "You lose 😢"
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ return f"You chose: {user_choice}\nBot chose: {bot_choice}\n\n{result}"
 
 
 
 
22
 
23
+ iface = gr.Interface(
24
+ fn=play_rps,
25
+ inputs=gr.Radio(choices, label="Choose your move"),
26
+ outputs="text",
27
+ title="Rock-Paper-Scissors Game",
28
+ description="Play against a bot. Make your move!"
29
+ )
30
 
31
+ iface.launch()
32