Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Add the 4 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def new_game(n):
# 1 mark for creating the correct loop


def add_two(mat):
def add_new_item(mat):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
while(mat[a][b] != 0):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
mat[a][b] = 2
# choose what to add: 2 or 4
probability_of_2 = 0.9
mat[a][b] = 2 if random.random() < probability_of_2 else 4
return mat

###########
Expand Down
6 changes: 3 additions & 3 deletions puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def gen(self):
def init_matrix(self):
self.matrix = logic.new_game(4)
self.history_matrixs = list()
self.matrix = logic.add_two(self.matrix)
self.matrix = logic.add_two(self.matrix)
self.matrix = logic.add_new_item(self.matrix)
self.matrix = logic.add_new_item(self.matrix)

def update_grid_cells(self):
for i in range(c.GRID_LEN):
Expand All @@ -80,7 +80,7 @@ def key_down(self, event):
elif key in self.commands:
self.matrix, done = self.commands[repr(event.char)](self.matrix)
if done:
self.matrix = logic.add_two(self.matrix)
self.matrix = logic.add_new_item(self.matrix)
# record last move
self.history_matrixs.append(self.matrix)
self.update_grid_cells()
Expand Down