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
Commits
e5f90ca8
Commit
e5f90ca8
authored
Nov 18, 2017
by
Antoine Roux
Browse files
some methods now take a grid as argument -> a bit faster maybe
parent
b5c7af9a
Changes
3
Show whitespace changes
Inline
Side-by-side
2048.py
View file @
e5f90ca8
...
...
@@ -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")
...
...
@@ -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
...
...
@@ -165,6 +163,7 @@ class Grid (object) :
# [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
],
...
...
@@ -173,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
)
##############################################
...
...
@@ -305,9 +304,10 @@ if MODE == "AI" :
print
(
Grid
(
finishedGrid
))
print
(
"------------------------"
)
if
MODE
==
"MULTI_AI"
:
startTime
=
time
.
time
()
nbrGames
=
10
0
nbrGames
=
3
0
listScores
=
[[],[]]
for
k
in
range
(
nbrGames
)
:
finishedGrid
=
single_AI
()
...
...
Idées 2048.pages
View file @
e5f90ca8
No preview for this file type
test.py
View file @
e5f90ca8
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
],
[
64
,
8
,
2
,
2
],
[
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment