Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
2048
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Bianco
2048
Commits
366a2268
Commit
366a2268
authored
6 years ago
by
Thomas Bianco
Browse files
Options
Downloads
Patches
Plain Diff
fonctionnalite 2
parent
883ba07a
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tb_grid_2048.py
+67
-0
67 additions, 0 deletions
tb_grid_2048.py
tb_test_grid_2048.py
+63
-0
63 additions, 0 deletions
tb_test_grid_2048.py
with
130 additions
and
0 deletions
tb_grid_2048.py
0 → 100644
+
67
−
0
View file @
366a2268
import
random
as
rd
THEMES
=
{
"
0
"
:
{
"
name
"
:
"
Default
"
,
0
:
""
,
2
:
"
2
"
,
4
:
"
4
"
,
8
:
"
8
"
,
16
:
"
16
"
,
32
:
"
32
"
,
64
:
"
64
"
,
128
:
"
128
"
,
256
:
"
256
"
,
512
:
"
512
"
,
1024
:
"
1024
"
,
2048
:
"
2048
"
,
4096
:
"
4096
"
,
8192
:
"
8192
"
},
"
1
"
:
{
"
name
"
:
"
Chemistry
"
,
0
:
""
,
2
:
"
H
"
,
4
:
"
He
"
,
8
:
"
Li
"
,
16
:
"
Be
"
,
32
:
"
B
"
,
64
:
"
C
"
,
128
:
"
N
"
,
256
:
"
O
"
,
512
:
"
F
"
,
1024
:
"
Ne
"
,
2048
:
"
Na
"
,
4096
:
"
Mg
"
,
8192
:
"
Al
"
},
"
2
"
:
{
"
name
"
:
"
Alphabet
"
,
0
:
""
,
2
:
"
A
"
,
4
:
"
B
"
,
8
:
"
C
"
,
16
:
"
D
"
,
32
:
"
E
"
,
64
:
"
F
"
,
128
:
"
G
"
,
256
:
"
H
"
,
512
:
"
I
"
,
1024
:
"
J
"
,
2048
:
"
K
"
,
4096
:
"
L
"
,
8192
:
"
M
"
}}
def
create_grid
(
n
):
game_grid
=
[]
for
i
in
range
(
n
):
game_grid
.
append
([
'
'
]
*
n
)
return
game_grid
def
get_value_new_tile
():
return
rd
.
choice
([
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
4
])
def
get_all_tiles
(
grid
):
tiles
=
[]
for
line
in
grid
:
for
tile
in
line
:
if
tile
==
'
'
:
tiles
.
append
(
0
)
else
:
tiles
.
append
(
tile
)
return
tiles
def
get_empty_tiles_positions
(
grid
):
empty_tiles
=
[]
n
=
len
(
grid
)
m
=
len
(
grid
[
0
])
for
i
in
range
(
n
):
for
j
in
range
(
m
):
if
grid
[
i
][
j
]
==
'
'
or
grid
[
i
][
j
]
==
0
:
empty_tiles
.
append
((
i
,
j
))
return
empty_tiles
def
get_new_position
(
grid
):
empty_tiles
=
get_empty_tiles_positions
(
grid
)
return
rd
.
choice
(
empty_tiles
)
def
grid_get_value
(
grid
,
x
,
y
):
if
grid
[
x
][
y
]
==
'
'
:
return
0
return
grid
[
x
][
y
]
def
grid_add_new_tile
(
grid
):
x
,
y
=
get_new_position
(
grid
)
grid
[
x
][
y
]
=
get_value_new_tile
()
return
grid
def
init_game
(
n
):
grid
=
create_grid
(
n
)
for
i
in
range
(
2
):
grid
=
grid_add_new_tile
(
grid
)
return
grid
def
grid_to_string_with_size_and_theme
(
grid
,
theme
,
n
):
m
=
long_value_with_theme
(
grid
,
theme
)
a
=
""
for
i
in
range
(
n
):
a
+=
(
"
"
+
"
=
"
*
(
m
+
2
))
*
n
+
"
\n
"
for
j
in
range
(
n
):
a
+=
"
| {}
"
.
format
(
theme
[
grid
[
i
][
j
]])
+
"
"
*
(
1
+
m
-
len
(
theme
[
grid
[
i
][
j
]]))
a
+=
"
|
\n
"
a
+=
(
"
"
+
"
=
"
*
(
m
+
2
))
*
n
return
a
def
long_value_with_theme
(
grid
,
theme
):
return
max
(
len
(
theme
[
v
])
for
v
in
get_all_tiles
(
grid
))
grid
=
[[
16
,
4
,
8
,
2
],
[
2
,
4
,
2
,
128
],
[
4
,
512
,
32
,
64
],
[
1024
,
2048
,
512
,
2
]]
print
(
grid_to_string_with_size_and_theme
(
grid
,
THEMES
[
"
1
"
],
4
))
This diff is collapsed.
Click to expand it.
tb_test_grid_2048.py
0 → 100644
+
63
−
0
View file @
366a2268
from
game2048.tb_grid_2048
import
*
from
pytest
import
*
def
test_create_grid
():
assert
create_grid
(
0
)
==
[]
assert
create_grid
(
1
)
==
[[
'
'
]]
assert
create_grid
(
4
)
==
[[
'
'
,
'
'
,
'
'
,
'
'
],[
'
'
,
'
'
,
'
'
,
'
'
],[
'
'
,
'
'
,
'
'
,
'
'
],[
'
'
,
'
'
,
'
'
,
'
'
]]
def
test_get_value_new_tile
():
assert
get_value_new_tile
()
==
2
or
4
def
test_get_all_tiles
():
assert
get_all_tiles
(
[[
'
'
,
4
,
8
,
2
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
512
,
32
,
64
],[
1024
,
2048
,
512
,
'
'
]])
==
[
0
,
4
,
8
,
2
,
0
,
0
,
0
,
0
,
0
,
512
,
32
,
64
,
1024
,
2048
,
512
,
0
]
assert
get_all_tiles
([[
16
,
4
,
8
,
2
],
[
2
,
4
,
2
,
128
],
[
4
,
512
,
32
,
64
],[
1024
,
2048
,
512
,
2
]])
==
[
16
,
4
,
8
,
2
,
2
,
4
,
2
,
128
,
4
,
512
,
32
,
64
,
1024
,
2048
,
512
,
2
]
assert
get_all_tiles
(
create_grid
(
3
))
==
[
0
for
i
in
range
(
9
)]
def
test_get_empty_tiles_positions
():
assert
get_empty_tiles_positions
([[
0
,
16
,
32
,
0
],
[
64
,
0
,
32
,
2
],
[
2
,
2
,
8
,
4
],[
512
,
8
,
16
,
0
]])
==
[(
0
,
0
),(
0
,
3
),(
1
,
1
),(
3
,
3
)]
assert
get_empty_tiles_positions
([[
'
'
,
16
,
32
,
0
],
[
64
,
0
,
32
,
2
],
[
2
,
2
,
8
,
4
],
[
512
,
8
,
16
,
0
]])
==
[(
0
,
0
),(
0
,
3
),(
1
,
1
),(
3
,
3
)]
assert
get_empty_tiles_positions
(
create_grid
(
2
))
==
[(
0
,
0
),(
0
,
1
),(
1
,
0
),(
1
,
1
)]
assert
get_empty_tiles_positions
([[
16
,
4
,
8
,
2
],
[
2
,
4
,
2
,
128
],
[
4
,
512
,
32
,
64
],[
1024
,
2048
,
512
,
2
]])
==
[]
def
test_get_new_position
():
grid
=
[[
0
,
16
,
32
,
0
],
[
64
,
0
,
32
,
2
],
[
2
,
2
,
8
,
4
],
[
512
,
8
,
16
,
0
]]
x
,
y
=
get_new_position
(
grid
)
assert
(
grid_get_value
(
grid
,
x
,
y
))
==
0
grid
=
[[
'
'
,
4
,
8
,
2
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
512
,
32
,
64
],
[
1024
,
2048
,
512
,
'
'
]]
x
,
y
=
get_new_position
(
grid
)
assert
(
grid_get_value
(
grid
,
x
,
y
))
==
0
def
test_grid_add_new_tile
():
game_grid
=
create_grid
(
4
)
game_grid
=
grid_add_new_tile
(
game_grid
)
tiles
=
get_all_tiles
(
game_grid
)
assert
2
or
4
in
tiles
def
test_init_game
():
grid
=
init_game
(
4
)
tiles
=
get_all_tiles
(
grid
)
assert
2
or
4
in
tiles
assert
len
(
get_empty_tiles_positions
(
grid
))
==
14
def
test_grid_to_string
():
a
=
"""
=== === === ===
| | | | |
=== === === ===
| | | | |
=== === === ===
| | | | |
=== === === ===
| 2 | | | 2 |
=== === === ===
"""
assert
grid_to_string_with_size_and_theme
([[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
2
,
'
'
,
'
'
,
2
]],
THEMES
[
"
0
"
],
4
)
==
a
def
test_long_value_with_theme
():
grid
=
[[
2048
,
16
,
32
,
0
],
[
0
,
4
,
0
,
2
],
[
0
,
0
,
0
,
32
],
[
512
,
1024
,
0
,
2
]]
assert
long_value_with_theme
(
grid
,
THEMES
[
"
0
"
])
==
4
assert
long_value_with_theme
(
grid
,
THEMES
[
"
1
"
])
==
2
assert
long_value_with_theme
(
grid
,
THEMES
[
"
2
"
])
==
1
grid
=
[[
16
,
4
,
8
,
2
],
[
2
,
4
,
2
,
128
],
[
4
,
512
,
32
,
4096
],
[
1024
,
2048
,
512
,
2
]]
assert
long_value_with_theme
(
grid
,
THEMES
[
"
0
"
])
==
4
assert
long_value_with_theme
(
grid
,
THEMES
[
"
1
"
])
==
2
assert
long_value_with_theme
(
grid
,
THEMES
[
"
2
"
])
==
1
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment