Last active
December 23, 2016 21:36
-
-
Save ProfessorLogout/2f9016b325c2afaf40ba2811b0a82505 to your computer and use it in GitHub Desktop.
Implementing 2048 mechanics only using std-lib objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Implementing the 2048 mechanic with stdlib-only tools""" | |
import random | |
LEFT = "L" | |
RIGHT = "R" | |
UP = "U" | |
DOWN = "D" | |
def create_board(size = 4): | |
"""Creating a list of lists defining the board. | |
In this case 0 is used as a marker for empty tiles""" | |
return [[0] * size for x in range(0,size)] | |
def draw_board(board): | |
"""Drawing the board to std-out (e.g. command line)""" | |
for line in board: | |
print(line) | |
def advance_board(raw_board, direction): | |
"""Calculating a new board based on a move direction. | |
Also adding 1 random tile to the board afterwards""" | |
#Transitioning displayed, raw boards to generalized boards, used by the algorithm | |
generalized_board = [] | |
if direction == LEFT: | |
generalized_board = raw_board | |
if direction == RIGHT: | |
for row in raw_board: | |
temp_row = [] | |
for tile in reversed(row): | |
temp_row.append(tile) | |
generalized_board.append(temp_row) | |
if direction == UP: | |
pass | |
if direction == DOWN: | |
pass | |
del raw_board | |
#Algorithm recalculating the board | |
for row_num, row in enumerate(generalized_board): | |
for tile_num in range(1,len(row)): | |
if row[tile_num] == row[tile_num - 1]: | |
row[tile_num - 1] = row[tile_num] * 2 | |
row[tile_num] = 0 | |
elif row[tile_num -1] == 0: | |
row[tile_num -1], row[tile_num] = row[tile_num], row[tile_num -1] | |
desired_lenght = len(row) | |
while 0 in row: | |
row.remove(0) | |
while len(row) < desired_lenght: | |
row.append(0) | |
#Transitioning generalized boards to displayed, raw boards, used by the algorithm | |
raw_board = [] | |
if direction == LEFT: | |
raw_board = generalized_board | |
if direction == RIGHT: | |
for row in generalized_board: | |
temp_row = [] | |
for tile in reversed(row): | |
temp_row.append(tile) | |
raw_board.append(temp_row) | |
if direction == UP: | |
pass | |
if direction == DOWN: | |
pass | |
del generalized_board | |
#Adding random tile to the board | |
new_tile_value = random.sample([2,4],1) | |
while True: | |
row = random.randrange(0,len(raw_board) - 1) | |
tile = random.randrange(0,len(raw_board[row]) - 1) | |
if raw_board[row][tile] == 0: | |
raw_board[row][tile] = new_tile_value[0] | |
break | |
return raw_board | |
#Usage of Board-Toolkit | |
my_board = create_board() | |
while True: | |
draw_board(my_board) | |
my_input = input(" Move your tiles by using L - R - U - D\n> ") | |
my_board = advance_board(raw_board = my_board, direction= my_input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment