Skip to content
Snippets Groups Projects
Commit 0ad4620a authored by Boucas Marco's avatar Boucas Marco
Browse files

Ajout de la fonction d'affichage qui semble bien marcher

parent 5d198f29
No related branches found
No related tags found
1 merge request!2Fusion sur le master pour commencer contribution
......@@ -13,15 +13,21 @@ def create_grid(taille):
game_grid.append([' ' for j in range(taille)])
return game_grid
def grid_initialiser(game_grid):
def grid_initialiser(taille):
"""
Function that take an empty grid and randomly add 2 elements in it and return the grid
"""
game_grid=grid_add_random(game_grid)
game_grid=grid_add_random(game_grid)
game_grid=create_grid(taille)
game_grid=grid_add_new_tile(game_grid)
game_grid=grid_add_new_tile(game_grid)
return game_grid
def grid_get_all_tiles(game_grid):
L=[]
for T in game_grid:
L.extend(T)
return L
def grid_add_random(game_grid):
def grid_add_new_tile(game_grid):
"""
Function that take a grid and add one element (a '2' of a '4' depending on the probability)
Only in an empty tile
......@@ -38,15 +44,11 @@ def grid_add_random(game_grid):
val="2"
else:
val='4'
grid_add_new_tile_at_position(game_grid,x,y,val)
return game_grid
def grid_add_new_tile_at_position(game_grid,x,y,val):
game_grid[x][y]=val
return game_grid
def grid_to_string(game_grid):
"""
Function that take a grid and return a string element containing what to show to the people
......@@ -54,9 +56,16 @@ def grid_to_string(game_grid):
taille=len(game_grid)
al=" ".join(["===" for i in range(taille)])
ligne=" "+al+"\n"
LIST=grid_get_all_tiles(game_grid)
max=0
for x in LIST:
if len(x)>max:
max=len(x)
def afficher_ligne(T):
l=' | '.join(T)
TT=T[:]
for t in TT:
t.ljust(max)
l=' | '.join(TT)
t='| '+l+' |\n'
print(t)
return t
......@@ -64,6 +73,4 @@ def grid_to_string(game_grid):
txt=ligne.join(L)
return ligne+txt+' '+al
return txt
\ No newline at end of file
......@@ -5,15 +5,11 @@ from pytest import *
def test_create_grid():
assert create_grid(4) == [[' ',' ',' ', ' '],[' ',' ',' ', ' '],[' ',' ',' ', ' '],[' ',' ',' ', ' ']]
def test_grid_add_new_tile_at_position():
def test_grid_add_new_tile():
game_grid=create_grid(4)
game_grid=grid_add_new_tile_at_position(game_grid,1,1,'2')
assert game_grid==[[' ',' ',' ', ' '],[' ', '2' ,' ', ' '],[' ',' ',' ', ' '],[' ',' ',' ', ' ']]
def test_grid_add_random():
game_grid=create_grid(4)
game_grid=grid_add_random(game_grid)
game_grid=grid_add_random(game_grid)
game_grid=grid_add_new_tile(game_grid)
game_grid=grid_add_new_tile(game_grid)
assert '2' or '4' in game_grid
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment