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

swipe etc now take parameter + we choose with two shots in advance

parent 1f4111e3
......@@ -47,9 +47,7 @@ class Grid (object) :
if nbrNonZero == 0 : #if empty, go to next
#print("isEmpty")
continue
if nbrNonZero == 4 : #if full, go to next
#print("isFull")
continue
#warning : if the line is full, it can still be swiped !!
for lineNbr in range(3, -1 + nbrNonZero, -1) :
if currentColumn[lineNbr] != 0 :
......@@ -61,32 +59,27 @@ class Grid (object) :
return False
def canSwipeUp (self) :
""" returns True if swiping up has an effect
using canSwipeBase
def canSwipe (self, direction) :
""" returns True if swiping has an effect using canSwipeBase
direction = 0 up, 1 right, 2 down, 3 left
"""
return(self.canSwipeBase())
if direction == 0 :
return(self.canSwipeBase())
def canSwipeRight (self) :
""" returns True if swiping right has an effect
using canSwipeBase
"""
rotated = np.rot90(self.grid)
return(Grid(rotated).canSwipeBase())
elif direction == 1 :
rotated = np.rot90(self.grid)
return(Grid(rotated).canSwipeBase())
def canSwipeDown (self) :
""" returns True if swiping down has an effect
using canSwipeBase
"""
rotated = np.rot90(np.rot90(self.grid))
return(Grid(rotated).canSwipeBase())
elif direction == 2 :
rotated = np.rot90(np.rot90(self.grid))
return(Grid(rotated).canSwipeBase())
def canSwipeLeft (self) :
""" returns True if swiping left has an effect
using canSwipeBase
"""
rotated = np.rot90(np.rot90(np.rot90(self.grid)))
return(Grid(rotated).canSwipeBase())
elif direction == 3 :
rotated = np.rot90(np.rot90(np.rot90(self.grid)))
return(Grid(rotated).canSwipeBase())
else :
return False
def swipeBase (self) :
""" swipes the grid up, doing all the necessary additions ()
......@@ -116,32 +109,27 @@ class Grid (object) :
return (grid)
def swipeUp (self) :
""" swipes the grid up, doing all the necessary additions
uses swipeBase
def swipe (self, direction) :
""" swipes the grid, doing all the necessary additions (uses swipeBase)
"""
self.grid = self.swipeBase()
def swipeRight (self) :
""" swipes the grid right, doing all the necessary additions
uses swipeBase
"""
rotated = Grid(np.rot90(self.grid))
self.grid = np.rot90(np.rot90(np.rot90(rotated.swipeBase())))
if direction == 0 :
self.grid = self.swipeBase()
def swipeDown (self) :
""" swipes the grid down, doing all the necessary additions
uses swipeBase
"""
rotated = Grid(np.rot90(np.rot90(self.grid)))
self.grid = np.rot90(np.rot90(rotated.swipeBase()))
elif direction == 1 :
rotated = Grid(np.rot90(self.grid))
self.grid = np.rot90(np.rot90(np.rot90(rotated.swipeBase())))
def swipeLeft (self) :
""" swipes the grid left, doing all the necessary additions
uses swipeBase
"""
rotated = Grid(np.rot90(np.rot90(np.rot90(self.grid))))
self.grid = np.rot90(rotated.swipeBase())
elif direction == 2 :
rotated = Grid(np.rot90(np.rot90(self.grid)))
self.grid = np.rot90(np.rot90(rotated.swipeBase()))
elif direction == 3 :
rotated = Grid(np.rot90(np.rot90(np.rot90(self.grid))))
self.grid = np.rot90(rotated.swipeBase())
else :
pass
def addNbr (self) :
""" adds a number on a empty space
......@@ -180,15 +168,11 @@ class Grid (object) :
[16, 8, 4, 2],
[32, 16, 8, 4],
[64, 32, 16, 8]]
# fitnessArray = [[160, 80, 5, 4],
# [320, 40, 4, 3],
# [640, 20, 3, 2],
# [1280, 10, 2, 1]]
fitness = 0
for k in range(4) :
for i in range (4) :
fitness += self.grid[k,i] * fitnessArray[k][i]
return (fitness / 100)
return (fitness / 10)
##############################################
......@@ -202,6 +186,10 @@ arrayGrid2 = [[2, 8, 2, 2],
[4, 2, 4, 0],
[2, 2, 2, 2]]
arrayGrid3 = [[2, 16, 4, 8],
[8, 4, 32, 4],
[64, 8, 2, 2],
[4, 16, 4, 4]]
# MODE = "PLAY"
......@@ -212,8 +200,8 @@ MODE = "AI"
if MODE == "TEST" :
# myGrid = Grid(arrayGrid2)
# print(myGrid)
myGrid = Grid(arrayGrid3)
print(myGrid.canSwipe(2))
pass
......@@ -226,17 +214,17 @@ if MODE == "PLAY" :
print(myGrid)
print("Press key")
user_input = input()
if user_input == "z" and myGrid.canSwipeUp() :
myGrid.swipeUp()
if user_input == "z" and myGrid.canSwipe(0) :
myGrid.swipe(0)
myGrid.addNbr()
elif user_input == "s" and myGrid.canSwipeRight() :
myGrid.swipeUp()
elif user_input == "s" and myGrid.canSwipe(1) :
myGrid.swipe(1)
myGrid.addNbr()
elif user_input == "w" and myGrid.canSwipeDown() :
myGrid.swipeDown()
elif user_input == "w" and myGrid.canSwipe(2) :
myGrid.swipe(2)
myGrid.addNbr()
elif user_input == "q" and myGrid.canSwipeLeft() :
myGrid.swipeLeft()
elif user_input == "q" and myGrid.canSwipe(3) :
myGrid.swipe(3)
myGrid.addNbr()
else :
pass
......@@ -248,41 +236,30 @@ if MODE == "AI" :
myGrid.addNbr()
while True :
print(myGrid)
fitnessArray = [0,0,0,0]
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipeUp() :
tempGrid.swipeUp()
fitnessArray[0] = tempGrid.calcFitness()
# print(myGrid)
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)
if tempGrid.canSwipeRight() :
tempGrid.swipeRight()
fitnessArray[1] = tempGrid.calcFitness()
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipeDown() :
tempGrid.swipeDown()
fitnessArray[2] = tempGrid.calcFitness()
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipeLeft() :
tempGrid.swipeLeft()
fitnessArray[3] = tempGrid.calcFitness()
direction = fitnessArray.index(max(fitnessArray))
print(int(fitnessArray[0]), int(fitnessArray[1]), int(fitnessArray[2]), int(fitnessArray[3]))
print(direction)
if direction == 0 :
myGrid.swipeUp()
elif direction == 1 :
myGrid.swipeRight()
elif direction == 2 :
myGrid.swipeDown()
elif direction == 3 :
myGrid.swipeLeft()
myGrid.addNbr()
user_input = input()
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])
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 :
print("Partie finie")
print(myGrid)
break
#user_input = input()
import numpy as np
import random
import time
# class bcolors:
# HEADER = '\033[95m'
# OKBLUE = '\033[94m'
# OKGREEN = '\033[92m'
# WARNING = '\033[93m'
# FAIL = '\033[91m'
# ENDC = '\033[0m'
# BOLD = '\033[1m'
# UNDERLINE = '\033[4m'
# print (bcolors.OKBLUE + "Some text"
# + bcolors.ENDC)
class Grid (object) :
def __init__ (self, grid) :
"""
"""
self.grid = np.array(grid)
def __str__ (self) :
string = ""
for k in range (4) :
for i in range (4) :
if self.grid[k][i] == 0 :
string += "." + " "
else :
nbrDigits = 1
power = 10
while (self.grid[k][i] // power > 0.5) : #0.5 is to avoid numeric noise
nbrDigits+=1
power *= 10
string += str(self.grid[k][i]) + (5-nbrDigits)*" "
string += "\n"
return string
def canSwipeBase (self) :
""" returns True if swiping up has an effect
"""
for columnNbr in range(4) :
currentColumn = self.grid[:,columnNbr]
nbrNonZero = np.count_nonzero(currentColumn)
if nbrNonZero == 0 : #if empty, go to next
#print("isEmpty")
continue
if nbrNonZero == 4 : #if full, go to next
#print("isFull")
continue
for lineNbr in range(3, -1 + nbrNonZero, -1) :
if currentColumn[lineNbr] != 0 :
return True
for lineNbr in range(0, 3) :
if currentColumn[lineNbr] == currentColumn[lineNbr+1] :
return True
return False
def canSwipeUp (self) :
""" returns True if swiping up has an effect
using canSwipeBase
"""
return(self.canSwipeBase())
def canSwipeRight (self) :
""" returns True if swiping right has an effect
using canSwipeBase
"""
rotated = np.rot90(self.grid)
return(Grid(rotated).canSwipeBase())
def canSwipeDown (self) :
""" returns True if swiping down has an effect
using canSwipeBase
"""
rotated = np.rot90(np.rot90(self.grid))
return(Grid(rotated).canSwipeBase())
def canSwipeLeft (self) :
""" returns True if swiping left has an effect
using canSwipeBase
"""
rotated = np.rot90(np.rot90(np.rot90(self.grid)))
return(Grid(rotated).canSwipeBase())
def swipeBase (self) :
""" swipes the grid up, doing all the necessary additions ()
"""
grid = self.grid
#we start by putting every tile up
for columnNbr in range(4) :
nbrZeros = 4 - np.count_nonzero(grid[:,columnNbr])
for lineNbr in range(4) :
counter = 0
while (grid[lineNbr, columnNbr] == 0) and (counter < 4):
counter += 1
if np.count_nonzero(grid[lineNbr:4, columnNbr]) != 0 :
for remainingLine in range (lineNbr, 3) :
grid[remainingLine, columnNbr] = grid[remainingLine+1, columnNbr]
grid[3, columnNbr] = 0
#now we do the additions
for lineNbr in range(3) :
if grid[lineNbr, columnNbr] == grid[lineNbr+1, columnNbr] :
grid[lineNbr, columnNbr] *= 2
for remainingLine in range (lineNbr+1, 3) :
grid[remainingLine, columnNbr] = grid[remainingLine+1, columnNbr]
grid[3, columnNbr] = 0
return (grid)
def swipeUp (self) :
""" swipes the grid up, doing all the necessary additions
uses swipeBase
"""
self.grid = self.swipeBase()
def swipeRight (self) :
""" swipes the grid right, doing all the necessary additions
uses swipeBase
"""
rotated = Grid(np.rot90(self.grid))
self.grid = np.rot90(np.rot90(np.rot90(rotated.swipeBase())))
def swipeDown (self) :
""" swipes the grid down, doing all the necessary additions
uses swipeBase
"""
rotated = Grid(np.rot90(np.rot90(self.grid)))
self.grid = np.rot90(np.rot90(rotated.swipeBase()))
def swipeLeft (self) :
""" swipes the grid left, doing all the necessary additions
uses swipeBase
"""
rotated = Grid(np.rot90(np.rot90(np.rot90(self.grid))))
self.grid = np.rot90(rotated.swipeBase())
def addNbr (self) :
""" adds a number on a empty space
2 with probability 9/10 ; 4 with probability 1/10
doesnt return anything
UN PEU BOURRIN POUR LE MOMENT
"""
#we pick out the random number : 2 or 4
if random.randint(1,10) == 1:
randomNbr = 4
else :
randomNbr = 2
#we pick a random position for the number
emptyCounter = 0
for k in range (4) :
for i in range (4) :
if self.grid[k,i] == 0 :
emptyCounter += 1
randomPosition = random.randint(0,emptyCounter-1)
counter = 0
for k in range (4) :
for i in range (4) :
if self.grid[k,i] == 0 :
if (counter == randomPosition) :
self.grid[k,i] = randomNbr
return #we leave the function
counter += 1
def calcFitness (self) :
""" Returns the fitness of the grid
"""
fitnessArray = [[8, 4, 2, 1],
[16, 8, 4, 2],
[32, 16, 8, 4],
[64, 32, 16, 8]]
# fitnessArray = [[160, 80, 5, 4],
# [320, 40, 4, 3],
# [640, 20, 3, 2],
# [1280, 10, 2, 1]]
fitness = 0
for k in range(4) :
for i in range (4) :
fitness += self.grid[k,i] * fitnessArray[k][i]
return (fitness / 100)
##############################################
arrayGrid1 = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
arrayGrid2 = [[2, 8, 2, 2],
[2, 2, 2, 0],
[4, 2, 4, 0],
[2, 2, 2, 2]]
# MODE = "PLAY"
# MODE = "TEST"
MODE = "AI"
##############################################
if MODE == "TEST" :
# myGrid = Grid(arrayGrid2)
# print(myGrid)
pass
if MODE == "PLAY" :
myGrid = Grid(arrayGrid1)
myGrid.addNbr()
myGrid.addNbr()
while True :
print(myGrid)
print("Press key")
user_input = input()
if user_input == "z" and myGrid.canSwipeUp() :
myGrid.swipeUp()
myGrid.addNbr()
elif user_input == "s" and myGrid.canSwipeRight() :
myGrid.swipeUp()
myGrid.addNbr()
elif user_input == "w" and myGrid.canSwipeDown() :
myGrid.swipeDown()
myGrid.addNbr()
elif user_input == "q" and myGrid.canSwipeLeft() :
myGrid.swipeLeft()
myGrid.addNbr()
else :
pass
print(myGrid.calcFitness())
if MODE == "AI" :
myGrid = Grid(arrayGrid1)
myGrid.addNbr()
myGrid.addNbr()
while True :
print(myGrid)
fitnessArray = [0,0,0,0]
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipeUp() :
tempGrid.swipeUp()
fitnessArray[0] = tempGrid.calcFitness()
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipeRight() :
tempGrid.swipeRight()
fitnessArray[1] = tempGrid.calcFitness()
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipeDown() :
tempGrid.swipeDown()
fitnessArray[2] = tempGrid.calcFitness()
tempGrid = Grid(myGrid.grid)
if tempGrid.canSwipeLeft() :
tempGrid.swipeLeft()
fitnessArray[3] = tempGrid.calcFitness()
direction = fitnessArray.index(max(fitnessArray))
print(int(fitnessArray[0]), int(fitnessArray[1]), int(fitnessArray[2]), int(fitnessArray[3]))
print(direction)
if direction == 0 :
myGrid.swipeUp()
elif direction == 1 :
myGrid.swipeRight()
elif direction == 2 :
myGrid.swipeDown()
elif direction == 3 :
myGrid.swipeLeft()
myGrid.addNbr()
user_input = input()
a = 2048
fitnessArray = [[2,5,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
nbrDigits = 1
power = 10
maxArray = [0,0,0,0]
for k in range (4) :
maxArray[k] = max(fitnessArray[k])
while a//power > 0.5 :
nbrDigits+=1
power *= 10
print(maxArray)
direction = maxArray.index(max(maxArray))
print(nbrDigits)
print(direction)
Markdown is supported
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