Commit e5f90ca8 authored by Antoine Roux's avatar Antoine Roux
Browse files

some methods now take a grid as argument -> a bit faster maybe

parent b5c7af9a
...@@ -39,11 +39,11 @@ class Grid (object) : ...@@ -39,11 +39,11 @@ class Grid (object) :
string += "\n" string += "\n"
return string return string
def canSwipeBase (self) : def canSwipeBase (self, grid) :
""" returns True if swiping up has an effect """ returns True if swiping up has an effect
""" """
for columnNbr in range(4) : for columnNbr in range(4) :
currentColumn = self.grid[:,columnNbr] currentColumn = grid[:,columnNbr]
nbrNonZero = np.count_nonzero(currentColumn) nbrNonZero = np.count_nonzero(currentColumn)
if nbrNonZero == 0 : #if empty, go to next if nbrNonZero == 0 : #if empty, go to next
#print("isEmpty") #print("isEmpty")
...@@ -65,28 +65,26 @@ class Grid (object) : ...@@ -65,28 +65,26 @@ class Grid (object) :
direction = 0 up, 1 right, 2 down, 3 left direction = 0 up, 1 right, 2 down, 3 left
""" """
if direction == 0 : if direction == 0 :
return(self.canSwipeBase()) return(self.canSwipeBase(self.grid))
elif direction == 1 : elif direction == 1 :
rotated = np.rot90(self.grid) rotated = np.rot90(self.grid)
return(Grid(rotated).canSwipeBase()) return(self.canSwipeBase(rotated))
elif direction == 2 : elif direction == 2 :
rotated = np.rot90(np.rot90(self.grid)) rotated = np.rot90(np.rot90(self.grid))
return(Grid(rotated).canSwipeBase()) return(self.canSwipeBase(rotated))
elif direction == 3 : elif direction == 3 :
rotated = np.rot90(np.rot90(np.rot90(self.grid))) rotated = np.rot90(np.rot90(np.rot90(self.grid)))
return(Grid(rotated).canSwipeBase()) return(self.canSwipeBase(rotated))
else : else :
return False return False
def swipeBase (self) : def swipeBase (self, grid) :
""" swipes the grid up, doing all the necessary additions () """ swipes the grid up, doing all the necessary additions ()
""" """
grid = self.grid
#we start by putting every tile up #we start by putting every tile up
for columnNbr in range(4) : for columnNbr in range(4) :
nbrZeros = 4 - np.count_nonzero(grid[:,columnNbr]) nbrZeros = 4 - np.count_nonzero(grid[:,columnNbr])
...@@ -115,19 +113,19 @@ class Grid (object) : ...@@ -115,19 +113,19 @@ class Grid (object) :
""" """
if direction == 0 : if direction == 0 :
self.grid = self.swipeBase() self.grid = self.swipeBase(self.grid)
elif direction == 1 : elif direction == 1 :
rotated = Grid(np.rot90(self.grid)) rotated = np.rot90(self.grid)
self.grid = np.rot90(np.rot90(np.rot90(rotated.swipeBase()))) self.grid = np.rot90(np.rot90(np.rot90(self.swipeBase(rotated))))
elif direction == 2 : elif direction == 2 :
rotated = Grid(np.rot90(np.rot90(self.grid))) rotated = np.rot90(np.rot90(self.grid))
self.grid = np.rot90(np.rot90(rotated.swipeBase())) self.grid = np.rot90(np.rot90(self.swipeBase(rotated)))
elif direction == 3 : elif direction == 3 :
rotated = Grid(np.rot90(np.rot90(np.rot90(self.grid)))) rotated = np.rot90(np.rot90(np.rot90(self.grid)))
self.grid = np.rot90(rotated.swipeBase()) self.grid = np.rot90(self.swipeBase(rotated))
else : else :
pass pass
...@@ -165,6 +163,7 @@ class Grid (object) : ...@@ -165,6 +163,7 @@ class Grid (object) :
# [256, 64, 16, 4], # [256, 64, 16, 4],
# [1024, 256, 64, 16], # [1024, 256, 64, 16],
# [4096, 1024, 256, 64]] # [4096, 1024, 256, 64]]
fitnessArray = [[8, 4, 2, 1], fitnessArray = [[8, 4, 2, 1],
[16, 8, 4, 2], [16, 8, 4, 2],
[32, 16, 8, 4], [32, 16, 8, 4],
...@@ -173,7 +172,7 @@ class Grid (object) : ...@@ -173,7 +172,7 @@ class Grid (object) :
for k in range(4) : for k in range(4) :
for i in range (4) : for i in range (4) :
fitness += self.grid[k,i] * fitnessArray[k][i] fitness += self.grid[k,i] * fitnessArray[k][i]
return (fitness / 10) return (fitness)
############################################## ##############################################
...@@ -305,9 +304,10 @@ if MODE == "AI" : ...@@ -305,9 +304,10 @@ if MODE == "AI" :
print(Grid(finishedGrid)) print(Grid(finishedGrid))
print("------------------------") print("------------------------")
if MODE == "MULTI_AI" : if MODE == "MULTI_AI" :
startTime = time.time() startTime = time.time()
nbrGames = 100 nbrGames = 30
listScores = [[],[]] listScores = [[],[]]
for k in range(nbrGames) : for k in range(nbrGames) :
finishedGrid = single_AI() finishedGrid = single_AI()
......
No preview for this file type
arrayGrid3 = [[2, 16, 4, 8], import numpy as np
import time
grid = np.array([[2, 16, 4, 8],
[8, 4, 32, 4],
[64, 8, 2, 2],
[4, 16, 4, 4]])
grid2 = [[2, 16, 4, 8],
[8, 4, 32, 4], [8, 4, 32, 4],
[64, 8, 2, 2], [64, 8, 2, 2],
[4, 16, 4, 4]] [4, 16, 4, 4]]
import numpy as np
toto = np.array(arrayGrid3) fitnessArray = [[9, 5, 3, 1],
[17, 9, 4, 2],
[33, 16, 8, 4],
[64, 32, 16, 8]]
start = time.time()
for k in range (100000) :
# for i in range(4) :
# for j in range (4) :
# fitness = grid[i,j] * fitnessArray[i][j]
toto = grid2*fitnessArray
fitness = sum(toto)
print(np.count_nonzero(toto)) print(time.time()-start)
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