from game2048.gg_grid_2048 import *
from pytest import *

def test_create_grid():
    assert create_grid(4)==[[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' ']]
    assert create_grid(5)==[[' ',' ',' ',' ',' '],[' ',' ',' ',' ',' '],[' ',' ',' ',' ',' '],[' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ']]
    assert create_grid(1)==[[' ']]
    assert create_grid(2)==[[' ',' '],[' ',' ']]

"""def test_add_new_tile_at_position():
    game_grid=create_grid(4)
    assert add_new_tile(game_grid, 1, 1, 2) == [[' ', ' ', ' ', ' '], [' ', 2 , ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ']]
    game_grid=create_grid(4)
    assert add_new_tile(game_grid, 2, 1, 4) == [[' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', 4, ' ', ' '], [' ', ' ', ' ', ' ']]
    game_grid=create_grid(4)
    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(game_grid, 1, 1, value)
    tiles=get_all_tiles(game_grid)
    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

def test_grid_to_string():
    a ="""
 === === === ===
|   |   |   |   |
 === === === ===
|   |   |   |   |
 === === === ===
|   |   |   |   |
 === === === ===
| 2 |   |   | 2 |
 === === === ===
"""
    grid_to_string([[' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ''], [2, ' ', ' ', 2]])==a