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
ab4cce8d
Commit
ab4cce8d
authored
Nov 17, 2017
by
Antoine Roux
Browse files
minor changes
parent
a044a5bf
Changes
3
Hide whitespace changes
Inline
Side-by-side
2048.py
View file @
ab4cce8d
...
...
@@ -143,12 +143,8 @@ class Grid (object) :
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
# we pick a random position for the number
emptyCounter
=
16
-
np
.
count_nonzero
(
self
.
grid
)
randomPosition
=
random
.
randint
(
0
,
emptyCounter
-
1
)
counter
=
0
...
...
@@ -195,6 +191,7 @@ arrayGrid3 = [[2, 16, 4, 8],
# MODE = "PLAY"
# MODE = "TEST"
MODE
=
"AI"
# MODE = "MULTI_AI"
##############################################
...
...
@@ -204,7 +201,6 @@ if MODE == "TEST" :
print
(
myGrid
.
canSwipe
(
2
))
pass
if
MODE
==
"PLAY"
:
myGrid
=
Grid
(
arrayGrid1
)
myGrid
.
addNbr
()
...
...
@@ -231,6 +227,7 @@ if MODE == "PLAY" :
print
(
myGrid
.
calcFitness
())
if
MODE
==
"AI"
:
start
=
time
.
time
()
myGrid
=
Grid
(
arrayGrid1
)
myGrid
.
addNbr
()
myGrid
.
addNbr
()
...
...
@@ -254,12 +251,54 @@ if MODE == "AI" :
try
:
maxArray
=
[
0
,
0
,
0
,
0
]
for
k
in
range
(
4
)
:
maxArray
[
k
]
=
max
(
fitnessArray
[
k
])
# 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
:
print
(
"Partie finie"
)
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
)
Idées 2048.pages
View file @
ab4cce8d
No preview for this file type
test.py
View file @
ab4cce8d
fitnessArray
=
[[
2
,
5
,
0
,
0
],
[
0
,
0
,
0
,
0
],
[
0
,
0
,
0
,
0
],
[
0
,
0
,
0
,
0
]]
arrayGrid3
=
[[
2
,
16
,
4
,
8
],
[
8
,
4
,
32
,
4
],
[
64
,
8
,
2
,
2
],
[
4
,
16
,
4
,
4
]]
maxArray
=
[
0
,
0
,
0
,
0
]
for
k
in
range
(
4
)
:
maxArray
[
k
]
=
max
(
fitnessArray
[
k
])
import
numpy
as
np
print
(
maxArray
)
direction
=
maxArray
.
index
(
max
(
maxArray
))
toto
=
np
.
array
(
arrayGrid3
)
print
(
direction
)
print
(
np
.
count_nonzero
(
toto
)
)
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