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
ad2bf537
Commit
ad2bf537
authored
6 years ago
by
Garance Guey
Browse files
Options
Downloads
Patches
Plain Diff
Finish Fonctionnalité 1
parent
b789095b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
grid_2048.py
+36
-11
36 additions, 11 deletions
grid_2048.py
test_grid_2048.py
+36
-6
36 additions, 6 deletions
test_grid_2048.py
with
72 additions
and
17 deletions
grid_2048.py
+
36
−
11
View file @
ad2bf537
...
...
@@ -10,31 +10,56 @@ def create_grid(taille):
grille
.
append
(
ligne
.
copy
())
#permet de découpler les lignes
return
grille
def
add_new_tile
_at_position
(
name_grid
,
ligne
,
pos_ligne
,
value
):
def
add_new_tile
(
grid
):
#la position indiqué doit etre cohérente avec les dimensions de la grille considérée
name_grid
[
ligne
][
pos_ligne
]
=
value
value
=
choose_value_new_tile
()
(
x
,
y
)
=
get_new_position
(
grid
)
grid
[
x
][
y
]
=
value
#attention, la fonction modifie la liste passée en argument !
return
(
name_
grid
)
return
(
grid
)
grid
=
create_grid
(
4
)
add_new_tile_at_position
(
grid
,
2
,
1
,
4
)
print
(
grid
)
def
choose_value_new_tile
():
value
=
rd
.
choice
([
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
4
])
return
(
value
)
def
get_all_tiles
(
name_
grid
):
nb_ligne
=
len
(
name_
grid
)
nb_colonne
=
len
(
name_
grid
[
0
])
#on suppose que la grille a au moins une ligne
def
get_all_tiles
(
grid
):
nb_ligne
=
len
(
grid
)
nb_colonne
=
len
(
grid
[
0
])
#on suppose que la grille a au moins une ligne
tiles
=
[]
for
ligne
in
range
(
nb_ligne
):
for
colonne
in
range
(
nb_colonne
):
tile
=
name_
grid
[
ligne
][
colonne
]
tile
=
grid
[
ligne
][
colonne
]
if
tile
==
'
'
:
tiles
.
append
(
0
)
else
:
tiles
.
append
(
tile
)
return
(
tiles
)
print
(
get_all_tiles
(
grid
))
def
get_empty_tile_position
(
grid
):
position_empty
=
[]
for
ligne
in
range
(
len
(
grid
)):
for
colonne
in
range
(
len
(
grid
[
0
])):
if
grid
[
ligne
][
colonne
]
==
0
or
grid
[
ligne
][
colonne
]
==
'
'
:
position_empty
.
append
((
ligne
,
colonne
))
return
(
position_empty
)
def
get_new_position
(
grid
):
#choisi une postion au hasard parmis les position libre
position_empty
=
get_empty_tile_position
(
grid
)
(
x
,
y
)
=
rd
.
choice
(
position_empty
)
return
((
x
,
y
))
def
grid_get_value
(
grid
,
x
,
y
):
if
grid
[
x
][
y
]
==
'
'
:
return
(
0
)
else
:
return
(
grid
[
x
][
y
])
def
init_game
(
taille
):
new_grid
=
create_grid
(
taille
)
(
x1
,
y1
)
=
get_new_position
(
new_grid
)
#choisi la première tuile qui va être un 2 ou un 4
new_grid
[
x1
][
y1
]
=
choose_value_new_tile
()
(
x2
,
y2
)
=
get_new_position
(
new_grid
)
#choisi la deuxièeme tuile
new_grid
[
x2
][
y2
]
=
choose_value_new_tile
()
return
(
new_grid
)
This diff is collapsed.
Click to expand it.
test_grid_2048.py
+
36
−
6
View file @
ad2bf537
...
...
@@ -7,20 +7,50 @@ def test_create_grid():
assert
create_grid
(
1
)
==
[[
'
'
]]
assert
create_grid
(
2
)
==
[[
'
'
,
'
'
],[
'
'
,
'
'
]]
def
test_add_new_tile_at_position
():
"""
def test_add_new_tile_at_position():
game_grid=create_grid(4)
assert
add_new_tile
_at_position
(
game_grid
,
1
,
1
,
2
)
==
[[
'
'
,
'
'
,
'
'
,
'
'
],[
'
'
,
2
,
'
'
,
'
'
],[
'
'
,
'
'
,
'
'
,
'
'
],[
'
'
,
'
'
,
'
'
,
'
'
]]
assert add_new_tile(game_grid,
1,
1,
2)
==
[[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
, 2 ,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
]]
game_grid=create_grid(4)
assert
add_new_tile
_at_position
(
game_grid
,
2
,
1
,
4
)
==
[[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
4
,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
]]
assert add_new_tile(game_grid,
2,
1,
4)
==
[[
'
'
,
'
'
,
'
'
,
'
'
], [
'
'
,
'
'
,
'
'
,
'
'
], [
'
'
, 4,
'
'
,
'
'
], [
'
'
,
'
'
,
'
'
,
'
'
]]
game_grid=create_grid(4)
assert
add_new_tile
_at_position
(
game_grid
,
3
,
0
,
2
)
==
[[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
'
'
,
'
'
,
'
'
,
'
'
],
[
2
,
'
'
,
'
'
,
'
'
]]
assert add_new_tile(game_grid,
3,
0,
2)
==
[[
'
'
,
'
'
,
'
'
,
'
'
], [
'
'
,
'
'
,
'
'
,
'
'
], [
'
'
,
'
'
,
'
'
,
'
'
], [2,
'
'
,
'
'
,
'
'
]]
game_grid=create_grid(4)
value=choose_value_new_tile()
game_grid
=
add_new_tile
_at_position
(
game_grid
,
1
,
1
,
value
)
game_grid=add_new_tile(game_grid,
1,
1,
value)
tiles=get_all_tiles(game_grid)
assert
2
or
4
in
tiles
assert 2 or 4 in tiles
"""
def
test_choose_value_new_tile
():
assert
choose_value_new_tile
()
==
2
or
4
def
test_get_all_tiles
():
assert
get_all_tiles
([[
'
'
,
4
,
4
,
6
],[
'
'
,
'
'
,
'
'
,
8
],[
32
,
32
,
'
'
,
64
],[
1024
,
'
'
,
'
'
,
512
]])
==
[
0
,
4
,
4
,
6
,
0
,
0
,
0
,
8
,
32
,
32
,
0
,
64
,
1024
,
0
,
0
,
512
]
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
]
def
test_get_empty_tile_position
():
assert
get_empty_tile_position
([[
0
,
0
,
16
,
32
],[
64
,
8
,
0
,
6
],[
64
,
0
,
4
,
8
],[
512
,
4
,
4
,
6
]])
==
[(
0
,
0
),(
0
,
1
),(
1
,
2
),(
2
,
1
)]
assert
get_empty_tile_position
([[
16
,
4
,
8
,
2
],
[
2
,
4
,
2
,
128
],
[
4
,
512
,
32
,
64
],[
1024
,
2048
,
512
,
2
]])
==
[]
assert
get_empty_tile_position
([[
'
'
,
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_tile_position
(
create_grid
(
2
))
==
[(
0
,
0
),(
0
,
1
),(
1
,
0
),(
1
,
1
)]
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_add_new_tile
():
game_grid
=
create_grid
(
4
)
game_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_tile_position
(
grid
))
==
14
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