From 979f672d25fde2342d4efec8038c867ba94bc6ab Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 5 Feb 2020 09:20:12 -0800 Subject: [PATCH 1/2] Refactor: rename function 'add_two' to 'add_new_item' --- logic.py | 2 +- puzzle.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/logic.py b/logic.py index e41a4dd..24e6699 100644 --- a/logic.py +++ b/logic.py @@ -34,7 +34,7 @@ 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): diff --git a/puzzle.py b/puzzle.py index 90f466b..b514407 100644 --- a/puzzle.py +++ b/puzzle.py @@ -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): @@ -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() From 5fce928f72d470df6360b6b9ab80db1b4fbfd1e8 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 5 Feb 2020 09:56:24 -0800 Subject: [PATCH 2/2] The 4 is added with probability 0.1 --- logic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/logic.py b/logic.py index 24e6699..46ce286 100644 --- a/logic.py +++ b/logic.py @@ -40,7 +40,9 @@ def add_new_item(mat): 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 ###########