Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Roux Antoine
AI_2048
Compare Revisions
6d7998a27692dbdf56a8f44e5df358a297a4a17e...bffa7790c5eb9eb9553ba30afa9c454f83db4402
Commits (3)
cleaned a bit, added comments
· b5c7af9a
Antoine Roux
authored
Nov 18, 2017
b5c7af9a
some methods now take a grid as argument -> a bit faster maybe
· e5f90ca8
Antoine Roux
authored
Nov 18, 2017
e5f90ca8
corrected error in canSwipe, can achieve 2048 git add .
· bffa7790
Antoine Roux
authored
Nov 26, 2017
bffa7790
Hide whitespace changes
Inline
Side-by-side
2048.py
View file @
bffa7790
...
...
@@ -39,11 +39,11 @@ class Grid (object) :
string
+=
"
\n
"
return
string
def
canSwipeBase
(
self
)
:
def
canSwipeBase
(
self
,
grid
)
:
""" returns True if swiping up has an effect
"""
for
columnNbr
in
range
(
4
)
:
currentColumn
=
self
.
grid
[:,
columnNbr
]
currentColumn
=
grid
[:,
columnNbr
]
nbrNonZero
=
np
.
count_nonzero
(
currentColumn
)
if
nbrNonZero
==
0
:
#if empty, go to next
#print("isEmpty")
...
...
@@ -55,7 +55,7 @@ class Grid (object) :
return
True
for
lineNbr
in
range
(
0
,
3
)
:
if
currentColumn
[
lineNbr
]
==
currentColumn
[
lineNbr
+
1
]
:
if
currentColumn
[
lineNbr
]
==
currentColumn
[
lineNbr
+
1
]
and
currentColumn
[
lineNbr
]
!=
0
:
return
True
return
False
...
...
@@ -65,28 +65,26 @@ class Grid (object) :
direction = 0 up, 1 right, 2 down, 3 left
"""
if
direction
==
0
:
return
(
self
.
canSwipeBase
())
return
(
self
.
canSwipeBase
(
self
.
grid
))
elif
direction
==
1
:
rotated
=
np
.
rot90
(
self
.
grid
)
return
(
Grid
(
rotated
)
.
canSwipeBase
())
return
(
self
.
canSwipeBase
(
rotated
))
elif
direction
==
2
:
rotated
=
np
.
rot90
(
np
.
rot90
(
self
.
grid
))
return
(
Grid
(
rotated
)
.
canSwipeBase
())
return
(
self
.
canSwipeBase
(
rotated
))
elif
direction
==
3
:
rotated
=
np
.
rot90
(
np
.
rot90
(
np
.
rot90
(
self
.
grid
)))
return
(
Grid
(
rotated
)
.
canSwipeBase
())
return
(
self
.
canSwipeBase
(
rotated
))
else
:
return
False
def
swipeBase
(
self
)
:
def
swipeBase
(
self
,
grid
)
:
""" 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
])
...
...
@@ -115,19 +113,19 @@ class Grid (object) :
"""
if
direction
==
0
:
self
.
grid
=
self
.
swipeBase
()
self
.
grid
=
self
.
swipeBase
(
self
.
grid
)
elif
direction
==
1
:
rotated
=
Grid
(
np
.
rot90
(
self
.
grid
)
)
self
.
grid
=
np
.
rot90
(
np
.
rot90
(
np
.
rot90
(
rotated
.
swipeBase
())))
rotated
=
np
.
rot90
(
self
.
grid
)
self
.
grid
=
np
.
rot90
(
np
.
rot90
(
np
.
rot90
(
self
.
swipeBase
(
rotated
))))
elif
direction
==
2
:
rotated
=
Grid
(
np
.
rot90
(
np
.
rot90
(
self
.
grid
))
)
self
.
grid
=
np
.
rot90
(
np
.
rot90
(
rotated
.
swipeBase
()))
rotated
=
np
.
rot90
(
np
.
rot90
(
self
.
grid
))
self
.
grid
=
np
.
rot90
(
np
.
rot90
(
self
.
swipeBase
(
rotated
)))
elif
direction
==
3
:
rotated
=
Grid
(
np
.
rot90
(
np
.
rot90
(
np
.
rot90
(
self
.
grid
)))
)
self
.
grid
=
np
.
rot90
(
rotated
.
swipeBase
())
rotated
=
np
.
rot90
(
np
.
rot90
(
np
.
rot90
(
self
.
grid
)))
self
.
grid
=
np
.
rot90
(
self
.
swipeBase
(
rotated
))
else
:
pass
...
...
@@ -161,6 +159,11 @@ class Grid (object) :
""" Returns the fitness of the grid
"""
# fitnessArray = [[64, 16, 4, 1], #a peu près pareil
# [256, 64, 16, 4],
# [1024, 256, 64, 16],
# [4096, 1024, 256, 64]]
fitnessArray
=
[[
8
,
4
,
2
,
1
],
[
16
,
8
,
4
,
2
],
[
32
,
16
,
8
,
4
],
...
...
@@ -169,7 +172,7 @@ class Grid (object) :
for
k
in
range
(
4
)
:
for
i
in
range
(
4
)
:
fitness
+=
self
.
grid
[
k
,
i
]
*
fitnessArray
[
k
][
i
]
return
(
fitness
/
10
)
return
(
fitness
)
##############################################
...
...
@@ -183,10 +186,10 @@ arrayGrid2 = [[2, 8, 2, 2],
[
4
,
2
,
4
,
0
],
[
2
,
2
,
2
,
2
]]
arrayGrid3
=
[[
2
,
16
,
4
,
8
],
[
8
,
4
,
3
2
,
4
],
[
64
,
8
,
2
,
2
],
[
4
,
16
,
4
,
4
]]
arrayGrid3
=
[[
4
,
0
,
0
,
0
],
[
16
,
4
,
2
,
0
],
[
32
,
2
,
8
,
0
],
[
128
,
32
,
2
,
0
]]
# MODE = "PLAY"
...
...
@@ -196,6 +199,8 @@ MODE = "MULTI_AI"
##############################################
def
evaluateGrid
(
givenGrid
)
:
""" Returns the max value of the grid
"""
return
(
givenGrid
.
max
())
def
single_AI
()
:
...
...
@@ -207,40 +212,73 @@ def single_AI() :
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
]])
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
)
#we determine what moves to consider (not necessarily all the possible moves)
possibleMoves
=
[
False
,
False
,
False
,
False
]
for
k
in
range
(
4
)
:
possibleMoves
[
k
]
=
myGrid
.
canSwipe
(
k
)
if
sum
(
possibleMoves
)
!=
0
:
#if at least one move is possible
if
(
possibleMoves
[
0
]
==
True
or
possibleMoves
[
2
]
==
True
or
possibleMoves
[
3
]
==
True
)
:
fitnessArray
[
1
,:]
=
0
#if can swipe up, left or down, do not swipe right
consideredMovesArray
=
[
0
,
2
,
3
]
# if np.count_nonzero(myGrid.grid[:,0]) != 0 :
# #if first line is not full, do not swipe up
# #PAS SUFFISANT CAR MM SI LA LIGNE EST PLEINE SWIPER PEUT AVOIR UN EFFET
# #EN PLUS CA DIMINUE LE MEILLEUR SCORE...
# consideredMovesArray = [2, 3]
for
k
in
consideredMovesArray
:
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
()
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
#we reverse the array to make sure the default swipe is left and not up
reversedMaxArray
=
maxArray
[::
-
1
]
direction2
=
reversedMaxArray
.
index
(
max
(
reversedMaxArray
))
direction
=
maxArray
.
index
(
max
(
maxArray
))
myGrid
.
swipe
(
direction
)
if
direction2
==
0
:
direction2
=
3
elif
direction2
==
1
:
direction2
=
2
elif
direction2
==
2
:
direction2
=
1
elif
direction2
==
3
:
direction2
=
0
#useful for debugging and seeing what is happening:
# print(myGrid)
# print(fitnessArray)
# print(maxArray)
# print(direction2)
# user_input = input()
myGrid
.
swipe
(
direction2
)
#mettre direction pour swiper sans avoir reverse l'array
myGrid
.
addNbr
()
except
:
else
:
#if no move is possible
return
(
myGrid
.
grid
)
break
#user_input = input()
##############################################
...
...
@@ -248,7 +286,7 @@ def single_AI() :
if
MODE
==
"TEST"
:
myGrid
=
Grid
(
arrayGrid3
)
print
(
myGrid
.
canSwipe
(
2
))
print
(
myGrid
.
canSwipe
(
3
))
pass
if
MODE
==
"PLAY"
:
...
...
@@ -288,7 +326,7 @@ if MODE == "AI" :
if
MODE
==
"MULTI_AI"
:
startTime
=
time
.
time
()
nbrGames
=
10
nbrGames
=
1
5
0
listScores
=
[[],[]]
for
k
in
range
(
nbrGames
)
:
finishedGrid
=
single_AI
()
...
...
@@ -304,7 +342,7 @@ if MODE == "MULTI_AI" :
print
(
"Temps écoulé :"
,
int
((
endTime
-
startTime
)
*
1000
),
"ms"
)
print
(
np
.
array
(
listScores
))
print
(
"--------------------------"
)
#
width = 30
#
plt.bar(listScores[0], listScores[1], width)
#
plt.show()
#plot a bar graph
width
=
30
plt
.
bar
(
listScores
[
0
],
listScores
[
1
],
width
)
plt
.
show
()
Idées 2048.pages
View file @
bffa7790
No preview for this file type
test.py
View file @
bffa7790
arrayGrid3
=
[[
2
,
16
,
4
,
8
],
[
8
,
4
,
32
,
4
],
[
64
,
8
,
2
,
2
],
[
4
,
16
,
4
,
4
]]
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
],
[
64
,
8
,
2
,
2
],
[
4
,
16
,
4
,
4
]]
fitnessArray
=
[[
9
,
5
,
3
,
1
],
[
17
,
9
,
4
,
2
],
[
33
,
16
,
8
,
4
],
[
64
,
32
,
16
,
8
]]
toto
=
np
.
array
(
arrayGrid3
)
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
)