Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
2
2048-gp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
We are planning to upgrade GitLab to the latest version this Friday morning. Expect some downtime!
Open sidebar
Sébastiaan Versteeg
2048-gp
Commits
92142204
Commit
92142204
authored
May 20, 2019
by
Jeremy Guijt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allows the framework to play an automated game
parent
f2e9635d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
5 deletions
+108
-5
logic.py
logic.py
+18
-5
play.py
play.py
+90
-0
No files found.
logic.py
View file @
92142204
...
...
@@ -37,13 +37,17 @@ def new_game(n):
def
add_tile
(
mat
):
a
=
random
.
randint
(
0
,
len
(
mat
)
-
1
)
b
=
random
.
randint
(
0
,
len
(
mat
)
-
1
)
while
mat
[
a
][
b
]
!=
0
:
if
any
([
any
([
x
==
0
for
x
in
arr
])
for
arr
in
mat
]):
a
=
random
.
randint
(
0
,
len
(
mat
)
-
1
)
b
=
random
.
randint
(
0
,
len
(
mat
)
-
1
)
mat
[
a
][
b
]
=
2
if
random
.
random
()
<
0.9
else
4
return
mat
while
mat
[
a
][
b
]
!=
0
:
a
=
random
.
randint
(
0
,
len
(
mat
)
-
1
)
b
=
random
.
randint
(
0
,
len
(
mat
)
-
1
)
mat
[
a
][
b
]
=
2
if
random
.
random
()
<
0.9
else
4
return
mat
else
:
return
mat
###########
# Task 1c #
...
...
@@ -97,6 +101,15 @@ def game_score(mat):
return
int
(
score
)
def
total_value
(
mat
):
score
=
0
for
i
in
range
(
c
.
GRID_LEN
):
for
j
in
range
(
c
.
GRID_LEN
):
value
=
mat
[
i
][
j
]
score
+=
value
return
int
(
score
)
###########
# Task 2a #
###########
...
...
play.py
0 → 100755
View file @
92142204
from
enum
import
Enum
,
unique
import
logic
from
constants
import
*
import
readchar
@
unique
class
Terminal
(
Enum
):
Left
=
1
Up
=
2
Right
=
3
Down
=
4
class
Player
(
object
):
def
__init__
(
self
):
pass
def
play
(
self
,
game
):
raise
NotImplementedError
(
'Implement this in a subclass of Player!'
)
class
DumbPlayer
(
object
):
did_left
=
False
def
play
(
self
,
game
):
if
self
.
did_left
:
self
.
did_left
=
False
return
Terminal
.
Down
else
:
self
.
did_left
=
True
return
Terminal
.
Left
class
Automated2048
(
object
):
state
=
None
def
__init__
(
self
):
pass
def
play_game
(
self
,
player
):
self
.
_new_game
()
loop_detected
=
False
while
not
loop_detected
:
previous_score
=
logic
.
total_value
(
self
.
state
)
move
=
player
.
play
(
self
.
state
)
self
.
process_move
(
move
)
self
.
state
=
logic
.
add_tile
(
self
.
state
)
new_score
=
logic
.
total_value
(
self
.
state
)
loop_detected
=
new_score
==
previous_score
# If there is no increase in the score, then the move did nothing
return
logic
.
total_value
(
self
.
state
)
def
_new_game
(
self
):
self
.
state
=
logic
.
new_game
(
GRID_LEN
)
self
.
state
=
logic
.
add_tile
(
self
.
state
)
self
.
state
=
logic
.
add_tile
(
self
.
state
)
def
process_move
(
self
,
move
):
if
move
==
Terminal
.
Left
:
new_state
,
_
=
logic
.
left
(
self
.
state
)
elif
move
==
Terminal
.
Up
:
new_state
,
_
=
logic
.
up
(
self
.
state
)
elif
move
==
Terminal
.
Right
:
new_state
,
_
=
logic
.
right
(
self
.
state
)
elif
move
==
Terminal
.
Down
:
new_state
,
_
=
logic
.
down
(
self
.
state
)
self
.
state
=
new_state
def
print_game
(
self
):
for
row
in
self
.
state
:
row_line
=
''
for
value
in
row
:
row_line
+=
f
' {value} '
print
(
row_line
)
if
__name__
==
'__main__'
:
print
(
"Let's play a game"
)
player
=
DumbPlayer
()
automated_game
=
Automated2048
()
score
=
automated_game
.
play_game
(
player
)
print
(
"You got a total value of {}"
.
format
(
score
))
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