Commit de3f79ea authored by LARIVIERE Axel's avatar LARIVIERE Axel
Browse files

ajout du joueur Random

parent 07fd8e2e
......@@ -17,11 +17,13 @@ class Game:
winner = 0
def __init__(self, joueur1, joueur2, nbPartie=100):
def __init__(self, joueur1, joueur2, nbPartie=10000):
self.joueur1 = joueur1
self.joueur2 = joueur2
self.joueur1.setPlayerNumber(1)
self.joueur2.setPlayerNumber(2)
self.winratej1 = 0
self.winratej2 = 0
self.joueur1.setGame(self)
self.joueur2.setGame(self)
self.window = Window(self)
......@@ -33,8 +35,8 @@ class Game:
return self.boardGame
def gameManager(self):
for _ in range(self.nbPartie):
print("\nNouvelle Partie!")
for actualNbGame in range(self.nbPartie):
print("\n <<<< Partie numéro : " + str(actualNbGame) + " >>>>")
while (not self.isEndGame(self.actualPlayer)):
self.window.drawTheScene()
if self.actualPlayer == 1:
......@@ -43,14 +45,22 @@ class Game:
else:
self.joueur2.takeDecision(self)
self.actualPlayer = 1
input()
if self.winner == 1:
self.winratej1 += 1
else:
self.winratej2 += 2
self.boardGame = [1, 1, 1, 1, 2, 2, 2, 2, 0]
self.winner = 0
self.actualPlayer = 1
print("Player " + str(self.winner) + " win!")
#self.window.drawTheScene()
if actualNbGame % 100:
print("winrate J1 = " + str(self.winratej1))
print("winrate J2 = " + str(self.winratej2))
def isEndGame(self, nextPlayer): # est ce la fin du jeu? est ce que le prochain joueur peut jouer?
......@@ -131,6 +141,27 @@ class RealPlayer(Player):
impossibleMove = 1 # le déplacement est impossible
print("Déplacement impossible, veuillez choisir un autre pion")
class RandomAgent(Player):
def __init__(self):
self.n_a = [0, 1, 2, 3, 4, 5, 6, 7, 8]
def playRandom(self,game):
board = game.getBoardGame()
played = False
while not played:
played = self.game.movePawn(random.randint(0,len(self.n_a)-1))
def takeDecision(self, game):
print("Au tour de l'IA random")
self.playRandom(game)
class Agent(Player):
......@@ -163,19 +194,17 @@ class Agent(Player):
def eps_greedy(self, Q, s, eps):
if random.random() < eps:
temp = random.randint(0,8)
#print("je joue random avec : "+str(temp))
return temp
else:
temp = self.greedy(Q, s)
#print("je greed avec : "+str(temp))
return temp
def SARSA(self, lr=0.01, num_episode=10000, eps=0.5, gamma=0.95, eps_decay=0.001):
tmp = self.game.getBoardGame()
ini_board = self.game.getBoardGame()
actual_board = ''
for i in tmp:
for i in ini_board:
actual_board = actual_board + str(i)
#state_actual_board = str(actual_board)
played = False
......@@ -201,33 +230,30 @@ class Agent(Player):
reward + gamma * self.Q[new_board_game,new_action] - self.Q[actual_board,action])
actual_board = new_board_game
action = new_action
reward = 20
if (self.game.winner == 1 and self.position == 1) or (self.game.winner == 2 and self.position == 2):
reward = 200
self.total_reward += reward
elif (self.game.winner == 1 and self.position == 2) or (self.game.winner == 2 and self.position == 1):
reward = -100
self.total_reward += reward
reward = 20
self.total_reward += reward
self.isWinner(self.game)
self.Q[actual_board,action] = self.Q[actual_board,action] + self.lr * (
reward + gamma * self.Q[new_board_game,new_action] - self.Q[actual_board,action])
print("Eps:{:2.4f} Rew:{:2.4f}".format(self.eps, self.total_reward))
def isWinner(isWinner):
if(isWinner):
reward = 200
self.total_reward += reward
def isWinner(self, game): #problème ici il ne rentre jamais dans le if
if game.isEndGame(game.actualPlayer):
self.reward = 200
self.total_reward += self.reward
print("t'as gagné !")
input()
else:
self.total_reward += reward
self.reward = -100
self.total_reward += self.reward
print("t'as perdu !")
input()
def takeDecision(self, game):
print("\nAu tour du joueur " + str(game.actualPlayer) + ", l'IA joue")
print("\nAu tour de l'IA intelligente " + str(game.actualPlayer) + ", l'IA joue")
self.SARSA()
......@@ -318,4 +344,4 @@ class Window(QMainWindow):
if __name__ == "__main__":
app = QApplication(sys.argv)
Game(Agent(), Agent())
Game(Agent(), RandomAgent())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment