-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio_game.py
191 lines (153 loc) · 8.97 KB
/
gradio_game.py
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 numpy as np
import time
import argparse
from classes.game_model import tic_tac_toe_model
from classes.Qlearningagent import QlearningAgent
def main():
custom_css = """
#color {background-color: #00BFFF}
.gradio-button {
width: 75px; /* Defina a largura desejada */
height: 75px; /* Defina a altura desejada */
background-color: #6495ED; /* Cor de fundo azul */
color: black; /* Cor do texto (branco neste caso) */
border: none; /* Remove a borda */
text-align: center; /* Centraliza o texto horizontalmente */
text-decoration: none; /* Remove a decoração do texto */
display: inline-block; /* Permite definir largura e altura */
font-size: 16px; /* Tamanho do texto */
line-height: 75px; /* Centraliza o texto verticalmente */
}
"""
def usr_move(board,usr,game_over,temp,position,value):
temp = int(temp)
if value == '' and int(game_over) == -1:
usr_piece = 'X' if int(usr) == 1 else 'O'
i,j = np.unravel_index(int(position)-1, shape=(3,3))
board.move(i,j,int(usr))
w = board.check_win()
temp-=1
return gr.update(size='lg',elem_id='color', scale = 0, min_width = 100, value=usr_piece, interactive=True), gr.Number(value=temp, visible=False), gr.Number(value=w, visible=False), board
else:
return gr.update(size='lg',elem_id='color', scale = 0, min_width = 100, interactive=True), gr.update(value=temp, visible=False),gr.update(visible=False)
def ia_move(board,game_over,ia):
if int(game_over) == -1:
state_board = board.matriz.copy()
avaible_moves = board.get_avaible_moves()
action = q_agent.choose_move(state_board,avaible_moves,int(ia))
i, j = board.number_ij(action)
ia_piece = 'X' if int(ia) == 1 else 'O'
board.move(i,j,int(ia))
w = board.check_win()
#buttons
retornos_btn = [gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100, interactive=True)]*(action+1)
retornos_btn[-1] = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100, interactive=True, value=ia_piece)
retornos_btn.extend([gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100, interactive=True)]*(9 - action-1))
retornos_btn.append(gr.Number(value=w, visible=False))
retornos_btn.append(board)
return retornos_btn
else:
retornos_btn = [gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100, interactive=True)]*9
retornos_btn.append(gr.Number(visible=False))
retornos_btn.append(board)
return retornos_btn
def set_col_visible(board,game_over,temp,usr,ia,selected):
usr = 1 if selected == 'X' else 2
ia = 2 if usr == 1 else 1
game_over = -1
retorn_buttons = [gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=True)]*9
if selected == 'X':
retorn_buttons.append(gr.Number(value=temp, visible=False))
retorn_buttons.append(gr.Number(value=-1, visible=False))
retorn_buttons.append(gr.Number(value=ia, visible=False))
retorn_buttons.append(gr.Number(value=usr, visible=False))
retorn_buttons.append(board)
else:
retorn_buttons = ia_move(board,game_over,ia)
retorn_buttons.insert(-1,gr.Number(value=-1, visible=False))
retorn_buttons.insert(-1,gr.Number(value=ia, visible=False))
retorn_buttons.insert(-1,gr.Number(value=usr, visible=False))
return retorn_buttons
def reset(board):
board.reset_matrix()
buttons = [gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,elem_classes='gradio-button',interactive=False)]*9
buttons.append(board)
return buttons
def check(game_over,n):
msg = ''
if n == 1:
game_over = 1
msg = 'O jogador X ganhou'
elif n == 2:
game_over = 1
msg = 'O jogador O ganhou'
elif n == 3 :
print('check',game_over)
game_over = 1
msg = 'VELHA'
return gr.update(value=msg, show_label=False), gr.Number(value=int(game_over), visible=False)
parser = argparse.ArgumentParser()
parser.add_argument('--file_name', type=str, required=False, default='models/q_agent-0.9ep-0.6ap-0.9-1000r.json')
args = parser.parse_args()
q_agent= QlearningAgent(epsilon=0,alpha=0,discount_factor=0, train=False)
q_agent.load_agent_dict(args.file_name)
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
with gr.Row():
with gr.Column() as x:
title = gr.Text(value='Escolha uma peça', show_label=False)
with gr.Row():
p1 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
p2 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
p3 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
with gr.Row():
p4 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
p5 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
p6 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
with gr.Row():
p7 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
p8 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
p9 = gr.Button(size='lg',elem_id='color', scale = 0, min_width = 100,value='',elem_classes='gradio-button',interactive=False)
n1 = gr.Number(value="1", visible=False)
n2 = gr.Number(value="2", visible=False)
n3 = gr.Number(value="3", visible=False)
n4 = gr.Number(value="4", visible=False)
n5 = gr.Number(value="5", visible=False)
n6 = gr.Number(value="6", visible=False)
n7 = gr.Number(value="7", visible=False)
n8 = gr.Number(value="8", visible=False)
n9 = gr.Number(value="9", visible=False)
temp_number = gr.Number(value="10", visible=False)
win_number = gr.Number(value="4", visible=False)
usr = gr.Number(value="-2", visible=False)
ia = gr.Number(value="-1", visible=False)
game_over = gr.Number(value="-1", visible=False)
temp = gr.Number(value="-2", visible=False)
with gr.Column():
c1 = gr.Radio( ['X','O'],label='Jogar como')
clear_button = gr.Button(value='Resetar')
board = gr.State(tic_tac_toe_model(3))
buttons = [p1, p2, p3, p4, p5, p6, p7, p8, p9,win_number, game_over,ia,usr,board]
renders_components = [p1, p2, p3, p4, p5, p6, p7, p8, p9, win_number,board]
game_components = [p1, p2, p3, p4, p5, p6, p7, p8, p9, title,temp_number,win_number, c1,game_over]
only_buttons = [p1, p2, p3, p4, p5, p6, p7, p8, p9,board]
p1.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n1,p1], outputs = [p1,temp_number,win_number,board])
p2.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n2,p2], outputs = [p2,temp_number,win_number,board])
p3.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n3,p3], outputs = [p3,temp_number,win_number,board])
p4.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n4,p4], outputs = [p4,temp_number,win_number,board])
p5.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n5,p5], outputs = [p5,temp_number,win_number,board])
p6.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n6,p6], outputs = [p6,temp_number,win_number,board])
p7.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n7,p7], outputs = [p7,temp_number,win_number,board])
p8.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n8,p8], outputs = [p8,temp_number,win_number,board])
p9.click(fn=usr_move,inputs=[board,usr,game_over,temp_number,n9,p9], outputs = [p9,temp_number,win_number,board])
def update_buttons():
return {p1: '', p2:'', p3:'', p4:'', p5:'', p6:'', p7:'', p8:'', p9:'',
title:'Escolha uma peça',temp_number:10, win_number:4, c1: '', game_over: 1}
c1.select(fn=set_col_visible, inputs= [board,game_over,temp,usr,ia,c1], outputs=buttons)
temp_number.change(fn=ia_move,inputs=[board,game_over,ia],outputs=renders_components)
win_number.change(fn=check, inputs=[game_over,win_number], outputs=[title, game_over])
clear_button.click(fn=update_buttons,outputs=game_components)
clear_button.click(fn=reset,inputs=board,outputs=only_buttons)
demo.launch()
if __name__ == '__main__':
main()