Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def pretty_print(mas):
  5.     print('-' * 10)
  6.     for row in mas:
  7.         print(*row)
  8.     print('-' * 10)
  9.  
  10.  
  11. def get_number_from_index(i, j):
  12.     return i * 4 + j + 1
  13.  
  14.  
  15. def get_index_from_number(num):
  16.     num -= 1
  17.     x, y = num // 4, num % 4
  18.     return x, y
  19.  
  20.  
  21. def insert_2_or_4(mas, x, y):
  22.     if random.random() <= 0.75:
  23.         mas[x][y] = 2
  24.     else:
  25.         mas[x][y] = 4
  26.     return mas
  27.  
  28.  
  29. def get_empty_list(mas):
  30.     empty = []
  31.     for i in range(4):
  32.         for j in range(4):
  33.             if mas[i][j] == 0:
  34.                 num = get_number_from_index(i, j)
  35.                 empty.append(num)
  36.     return empty
  37.  
  38.  
  39. def is_zero_in_mas(mas):
  40.     for row in mas:
  41.         if 0 in row:
  42.             return True
  43.     return False
  44.  
  45.  
  46. def move_left(mas):
  47.     delta = 0
  48.     for row in mas:
  49.         while 0 in row:
  50.             row.remove(0)
  51.         while len(row) != 4:
  52.             row.append(0)
  53.     for i in range(4):
  54.         for j in range(3):
  55.             if mas[i][j] == mas[i][j + 1] and mas[i][j] != 0:
  56.                 mas[i][j] *= 2
  57.                 delta += mas[i][j]
  58.                 mas[i].pop(j + 1)
  59.                 mas[i].append(0)
  60.     return mas, delta
  61.  
  62.  
  63. def move_right(mas):
  64.     delta = 0
  65.     for row in mas:
  66.         while 0 in row:
  67.             row.remove(0)
  68.         while len(row) != 4:
  69.             row.insert(0, 0)
  70.     for i in range(4):
  71.         for j in range(3, 0, -1):
  72.             if mas[i][j] == mas[i][j - 1] and mas[i][j] != 0:
  73.                 mas[i][j] *= 2
  74.                 delta += mas[i][j]
  75.                 mas[i].pop(j - 1)
  76.                 mas[i].insert(0, 0)
  77.     return mas, delta
  78.  
  79.  
  80. def move_up(mas):
  81.     delta = 0
  82.     for j in range(4):
  83.         column = []
  84.         for i in range(4):
  85.             if mas[i][j] != 0:
  86.                 column.append(mas[i][j])
  87.         while len(column) != 4:
  88.             column.append(0)
  89.         for i in range(3):
  90.             if column[i] == column[i + 1] and column[i] != 0:
  91.                 column[i] *= 2
  92.                 delta += mas[i][j]
  93.                 column.pop(i + 1)
  94.                 column.append(0)
  95.         for i in range(4):
  96.             mas[i][j] = column[i]
  97.     return mas, delta
  98.  
  99.  
  100. def move_down(mas):
  101.     delta = 0
  102.     for j in range(4):
  103.         column = []
  104.         for i in range(4):
  105.             if mas[i][j] != 0:
  106.                 column.append(mas[i][j])
  107.         while len(column) != 4:
  108.             column.insert(0, 0)
  109.         for i in range(3, 0, -1):
  110.             if column[i] == column[i - 1] and column[i] != 0:
  111.                 column[i] *= 2
  112.                 delta += mas[i][j]
  113.                 column.pop(i - 1)
  114.                 column.insert(0, 0)
  115.         for i in range(4):
  116.             mas[i][j] = column[i]
  117.     return mas, delta
  118.  
  119.  
  120. def can_move(mas):
  121.     for i in range(3):
  122.         for j in range(3):
  123.             if mas[i][j] == mas[i][j + 1] or mas[i][j] == mas[i + 1][j]:
  124.                 return True
  125.     return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement