Commit 6d7998a2 authored by Antoine Roux's avatar Antoine Roux
Browse files

fixed the multi AI + now never moves right -> 1024 half the games

parent ab4cce8d
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import numpy as np import numpy as np
import random import random
import time import time
import matplotlib.pyplot as plt
# class bcolors: # class bcolors:
# HEADER = '\033[95m' # HEADER = '\033[95m'
...@@ -191,7 +192,56 @@ arrayGrid3 = [[2, 16, 4, 8], ...@@ -191,7 +192,56 @@ arrayGrid3 = [[2, 16, 4, 8],
# MODE = "PLAY" # MODE = "PLAY"
# MODE = "TEST" # MODE = "TEST"
MODE = "AI" MODE = "AI"
# MODE = "MULTI_AI" MODE = "MULTI_AI"
##############################################
def evaluateGrid(givenGrid) :
return(givenGrid.max())
def single_AI() :
myGrid = Grid([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
myGrid.addNbr()
myGrid.addNbr()
while True :
fitnessArray = np.array([[0,0,0,0], #lignes = à 1er move constant
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]])
tempGrid = Grid(myGrid.grid) #c'est peut être ça qui est très lent
for k in range(4) :
for i in range (4) :
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipe(k) :
tempGrid.swipe(k)
if tempGrid.canSwipe(i) :
tempGrid.swipe(i)
fitnessArray[k][i] = tempGrid.calcFitness()
try :
possibleMoves = [False, False, False, False]
for k in range (4) :
possibleMoves[k] = myGrid.canSwipe(k)
if (possibleMoves[0] == True or possibleMoves[2] == True or possibleMoves[3] == True) :
fitnessArray[1,:] = 0
maxArray = [0,0,0,0]
for k in range (4) :
maxArray[k] = max(fitnessArray[k])
# maxArray[k] = fitnessArray[k].mean() #moins bon que le max
direction = maxArray.index(max(maxArray))
myGrid.swipe(direction)
myGrid.addNbr()
except :
return(myGrid.grid)
break
#user_input = input()
############################################## ##############################################
...@@ -227,78 +277,34 @@ if MODE == "PLAY" : ...@@ -227,78 +277,34 @@ if MODE == "PLAY" :
print(myGrid.calcFitness()) print(myGrid.calcFitness())
if MODE == "AI" : if MODE == "AI" :
start = time.time() startTime = time.time()
myGrid = Grid(arrayGrid1) finishedGrid = single_AI()
myGrid.addNbr() endTime = time.time()
myGrid.addNbr() print(" \n----- Partie finie -----")
print("Temps écoulé :", int((endTime-startTime)*1000), "ms")
while True : print("Valeur max :", evaluateGrid(finishedGrid))
# print(myGrid) print(Grid(finishedGrid))
fitnessArray = np.array([[0,0,0,0], #lignes = à 1er move constant print("------------------------")
[0,0,0,0],
[0,0,0,0], if MODE == "MULTI_AI" :
[0,0,0,0]]) startTime = time.time()
nbrGames = 10
tempGrid = Grid(myGrid.grid) listScores = [[],[]]
for k in range(4) : for k in range(nbrGames) :
for i in range (4) : finishedGrid = single_AI()
tempGrid = Grid(myGrid.grid) maxObtainedValue = evaluateGrid(finishedGrid)
if tempGrid.canSwipe(k) and tempGrid.canSwipe(i): if ( maxObtainedValue not in listScores[0][:]) :
tempGrid.swipe(k) listScores[0].append(maxObtainedValue)
tempGrid.swipe(i) listScores[1].append(1)
fitnessArray[k][i] = (tempGrid.calcFitness()) else :
listScores[1][listScores[0].index(maxObtainedValue)] += 1
try : print("Partie", k+1, "sur", nbrGames)
maxArray = [0,0,0,0] endTime = time.time()
for k in range (4) : print(" \n----- Parties finies -----")
# maxArray[k] = max(fitnessArray[k]) print("Temps écoulé :", int((endTime-startTime)*1000), "ms")
maxArray[k] = fitnessArray[k].mean() print(np.array(listScores))
direction = maxArray.index(max(maxArray)) # pas top, on pourrait voir le meilleur mais aussi ce qui peut arriver au pire, genre la meilleure moyenne print("--------------------------")
myGrid.swipe(direction)
myGrid.addNbr() # width = 30
except : # plt.bar(listScores[0], listScores[1], width)
print("Partie finie") # plt.show()
print(time.time()-start)
print(myGrid)
break
#user_input = input()
if MODE == "MULTI_AI" : #MARCHE PAS
start = time.time()
avgMax = 0
for k in range (2) :
myGrid = Grid(arrayGrid1)
myGrid.addNbr()
myGrid.addNbr()
while True :
fitnessArray = np.array([[0,0,0,0], #lignes = à 1er move constant
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]])
tempGrid = Grid(myGrid.grid)
for k in range(4) :
for i in range (4) :
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipe(k) and tempGrid.canSwipe(i):
tempGrid.swipe(k)
tempGrid.swipe(i)
fitnessArray[k][i] = (tempGrid.calcFitness())
try :
maxArray = [0,0,0,0]
for k in range (4) :
# maxArray[k] = max(fitnessArray[k])
maxArray[k] = fitnessArray[k].mean()
direction = maxArray.index(max(maxArray)) # pas top, on pourrait voir le meilleur mais aussi ce qui peut arriver au pire, genre la meilleure moyenne
myGrid.swipe(direction)
myGrid.addNbr()
except :
avgMax += max(myGrid.grid)
break
print(time.time()-start)
print("Average max:", avgMax)
No preview for this file type
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