File size: 7,452 Bytes
4ebf9b9
 
f1a1769
e5f0047
 
 
 
3c51ffb
bbe8538
e5f0047
bbe8538
e5f0047
 
4ebf9b9
e5f0047
 
 
 
 
cd2b97b
4ebf9b9
cd2b97b
4ebf9b9
 
 
e5f0047
 
 
a3e00d4
e5f0047
3c51ffb
e5f0047
 
 
 
 
 
 
 
 
 
 
 
 
 
3c51ffb
e5f0047
 
 
 
 
 
 
 
3c51ffb
e5f0047
 
 
 
3c51ffb
e5f0047
 
 
 
 
 
3c51ffb
e5f0047
 
 
 
 
 
bbe8538
 
 
 
 
 
 
 
 
 
 
 
 
 
e5f0047
bbe8538
e5f0047
 
bbe8538
e5f0047
 
57709f6
3c51ffb
e5f0047
bbe8538
4ebf9b9
 
 
 
3c51ffb
e5f0047
bbe8538
e5f0047
 
3c51ffb
e5f0047
bbe8538
e5f0047
 
 
bbe8538
4ebf9b9
 
 
3c51ffb
e5f0047
bbe8538
4ebf9b9
e5f0047
3c51ffb
e5f0047
bbe8538
e5f0047
 
3c51ffb
e5f0047
 
bbe8538
e5f0047
 
6dce411
3c51ffb
 
 
 
 
 
 
 
 
 
 
 
6dce411
3795c88
6dce411
5bb6b79
 
 
 
 
fb01ec3
5bb6b79
 
 
 
 
 
3c51ffb
bbe8538
e5f0047
 
 
 
 
 
 
 
 
 
 
 
 
 
bbe8538
 
a220423
4ebf9b9
e5f0047
 
 
 
bbe8538
e5f0047
cd2b97b
e5f0047
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import gradio as gr
import random

# Initialize the game board and state
def initialize_game():
    board = [["" for _ in range(3)] for _ in range(3)]
    current_player = "X"
    status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"
    difficulty = "Medium"
    buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
    return board, current_player, difficulty, status, *buttons

# Check for a winner
def check_winner(board):
    for i in range(3):
        if board[i][0] == board[i][1] == board[i][2] and board[i][0] != "":
            return board[i][0]
        if board[0][i] == board[1][i] == board[2][i] and board[0][i] != "":
            return board[0][i]
    if board[0][0] == board[1][1] == board[2][2] and board[0][0] != "":
        return board[0][0]
    if board[0][2] == board[1][1] == board[2][0] and board[0][2] != "":
        return board[0][2]
    return None

# Check for a draw
def check_draw(board):
    return all(cell != "" for row in board for cell in row)

# Minimax algorithm for AI's move
def minimax(board, depth, is_maximizing):
    winner = check_winner(board)
    if winner == "X":
        return -10 + depth
    elif winner == "O":
        return 10 - depth
    elif check_draw(board):
        return 0

    if is_maximizing:
        best = -float('inf')
        for i in range(3):
            for j in range(3):
                if board[i][j] == "":
                    board[i][j] = "O"
                    best = max(best, minimax(board, depth + 1, False))
                    board[i][j] = ""
        return best
    else:
        best = float('inf')
        for i in range(3):
            for j in range(3):
                if board[i][j] == "":
                    board[i][j] = "X"
                    best = min(best, minimax(board, depth + 1, True))
                    board[i][j] = ""
        return best

# Find the best move for AI
def get_best_move(board):
    best_val = -float('inf')
    best_move = (-1, -1)
    for i in range(3):
        for j in range(3):
            if board[i][j] == "":
                board[i][j] = "O"
                move_val = minimax(board, 0, False)
                board[i][j] = ""
                if move_val > best_val:
                    best_move = (i, j)
                    best_val = move_val
    return best_move

# Random AI move (for Easy level)
def get_random_move(board):
    empty_cells = [(i, j) for i in range(3) for j in range(3) if board[i][j] == ""]
    return random.choice(empty_cells) if empty_cells else (-1, -1)

# AI move based on difficulty level
def get_ai_move(board, difficulty):
    if difficulty == "Easy":
        return get_random_move(board)
    elif difficulty == "Medium":
        return get_random_move(board) if random.random() < 0.5 else get_best_move(board)
    elif difficulty == "Hard":
        return get_best_move(board)

# Handle a move
def handle_move(board, current_player, button_idx, difficulty, game_status):
    if "wins" in game_status or "draw" in game_status:
        buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
        return board, current_player, difficulty, game_status, *buttons

    row, col = divmod(button_idx, 3)
    if board[row][col] != "":
        status = f"<div style='font-size: 1.5em; color: orange; font-weight: bold;'>Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})</div>"
        buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
        return board, current_player, difficulty, status, *buttons

    board[row][col] = current_player
    winner = check_winner(board)
    if winner:
        status = f"<div style='font-size: 2em; color: red; font-weight: bold;'>Player {1 if winner == 'X' else 2} ({winner}) wins! ๐ŸŽ‰</div>"
        buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
        return board, current_player, difficulty, status, *buttons

    if check_draw(board):
        status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! ๐Ÿค</div>"
        buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
        return board, current_player, difficulty, status, *buttons

    if current_player == "X":
        current_player = "O"
        ai_row, ai_col = get_ai_move(board, difficulty)
        board[ai_row][ai_col] = "O"
        winner = check_winner(board)
        if winner:
            status = f"<div style='font-size: 2em; color: purple; font-weight: bold;'>AI ({winner}) wins! ๐ŸŽ‰</div>"
            buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
            return board, current_player, difficulty, status, *buttons

        if check_draw(board):
            status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! ๐Ÿค</div>"
            buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
            return board, current_player, difficulty, status, *buttons

        current_player = "X"
        status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"

    buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
    return board, current_player, difficulty, status, *buttons

# Build the Gradio UI
css = """
.cell-btn {
    height: 100px; 
    width: 100px; 
    font-size: 2em; 
    text-align: center; 
    background-color: #f0f0f0; 
    border: 2px solid #ddd; 
    transition: all 0.3s;
}
.cell-btn:hover {
    background-color: #e0e0e0;
}
"""

with gr.Blocks(css=css) as tic_tac_toe:
    with gr.Row():
        gr.HTML(
            """
            <div style="text-align: center; margin: auto;">
                <h1 style="color: #1155ff; font-size: 3.5em; font-weight: bold; font-family: 'Arial', sans-serif; margin-bottom: 0.5em;"> Tic-Tac-Toe with AI ๐ŸŽฎ</h1>
                <p style="font-size: 1.5em; color: #555555; font-weight: bold; font-family: 'Verdana', sans-serif;">
                    Let's Play!
                </p>
            </div>
            """
        )
       
    game_status = gr.Markdown(value="<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>")
    difficulty = gr.Radio(["Easy", "Medium", "Hard"], value="Medium", label="Select Difficulty Level")

    board_state = gr.State([["" for _ in range(3)] for _ in range(3)])
    current_player = gr.State("X")

    buttons = []
    for i in range(3):
        with gr.Row():
            for j in range(3):
                btn = gr.Button(value="", elem_classes=["cell-btn"])
                buttons.append(btn)

    for idx, btn in enumerate(buttons):
        btn.click(
            handle_move,
            inputs=[board_state, current_player, gr.Number(idx, visible=False), difficulty, game_status],
            outputs=[board_state, current_player, difficulty, game_status, *buttons],
        )

    reset_button = gr.Button("Reset Game")
    reset_button.click(
        initialize_game,
        inputs=[],
        outputs=[board_state, current_player, difficulty, game_status, *buttons],
    )

tic_tac_toe.launch()