Spaces:
Runtime error
Runtime error
File size: 4,363 Bytes
b3accf9 |
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 |
import tqdm
import random
from classes.game_model import tic_tac_toe_model
from classes.Qlearningagent import QlearningAgent
class Game():
def __init__(self, tic_tac_toe: tic_tac_toe_model, q_agent: QlearningAgent):
self.board = tic_tac_toe
self.q_agent = q_agent
def ia_vs_ia(self):
game_over = False
self.board.reset_matrix()
while not game_over:
w = self.board.check_win()
if w != 4:
win_piece = w
break
state = self.board.matriz.copy()
avaible_moves = self.board.get_avaible_moves()
action = self.q_agent.choose_move(state,avaible_moves,1)
i, j = self.board.number_ij(action)
self.board.move(i,j,1)
if w != 4:
win_piece = w
break
state = self.board.matriz.copy()
avaible_moves = self.board.get_avaible_moves()
action = self.q_agent.choose_move(state,avaible_moves,2)
i, j = self.board.number_ij(action)
self.board.move(i,j,2)
return win_piece
def run_ia_vs_ia(self,n):
games = []
for i in tqdm.tqdm(range(0,n)):
w = self.ia_vs_ia()
games.append(w)
return games
def ia_vs_user(self):
print('Menu')
print("1 - Iniciar aleatorio")
print("2 - Escolher peça [X-O]")
m1 = int(input())
while m1 != 1 and m1 !=2:
print('Insira um valor valido 1-2')
print("1 - Iniciar aleatorio")
print("2 - Escolher peça [X-O]")
m1 = int(input())
game_over = False
self.board.reset_matrix()
pieces = [1,2]
win_piece = 0
if m1 == 1 :
user = random.choice(pieces)
else:
print('1 - X \n 2 - O')
user = int(input())
while user != 1 and user !=2:
print("Insira um valor válido 1-2")
print('1 - X\n2 - O')
user = int(input())
ia = 2 if user == 1 else 1
print("Começo de jogo")
self.board.print_game()
if ia == 1:
while not game_over:
state = self.board.matriz.copy()
avaible_moves = self.board.get_avaible_moves()
action = self.q_agent.choose_move(state,avaible_moves,ia)
i, j = self.board.number_ij(action)
print('Jogada da IA - X')
self.board.move(i,j,ia)
self.board.print_game()
w = self.board.check_win()
if w != 4:
win_piece = w
break
print("Jogue Usuario - O [Numero da linha][Numero da coluna]")
ml,mv = int(input()), int(input())
self.board.move(ml,mv,user)
self.board.move(i,j,user)
self.board.print_game()
w = self.board.check_win()
if w != 4:
win_piece = w
break
else:
while not game_over:
print('Jogue Usuario - X [Numero da linha][Numero da coluna]')
ml,mv = int(input()), int(input())
self.board.move(ml,mv,user)
self.board.print_game()
w = self.board.check_win()
if w != 4:
win_piece = w
break
print('Jogada da IA - O')
state = self.board.matriz.copy()
avaible_moves = self.board.get_avaible_moves()
action = self.q_agent.choose_move(state,avaible_moves,ia)
i, j = self.board.number_ij(action)
self.board.move(i,j,ia)
w = self.board.check_win()
self.board.print_game()
if w != 4:
win_piece = w
break
if win_piece == ia :
print("IA venceu Humano Fraco")
elif win_piece == user :
print("Humano venceu, esta preparado para a revolução?")
else:
print('Deu velha, mas a I.A segue aprendendo e melhorando e você?')
return win_piece
|