def test_set_get(self): mat = MutSqMat(4) mat[Vec2(1, 1)] = 8 mat[Vec2(2, 3)] = 2 self.assertEqual(8, mat[Vec2(1, 1)]) self.assertEqual(2, mat[Vec2(2, 3)])
def flip_vertical(self): """ Flip along the vertical axis. """ old = self.copy() for x in range(self.width): for y in range(self.width): self[Vec2(x, y)] = old[Vec2(self.width - x - 1, y)]
def test_eq(self): mat = MutSqMat(4) mat[Vec2(3, 3)] = 4 mat[Vec2(1, 1)] = 2 mat_ref = MutSqMat(4) mat_ref[Vec2(3, 3)] = 4 mat_ref[Vec2(1, 1)] = 2 self.assertEqual(mat_ref, mat)
def test_rotate_3(self): mat = MutSqMat(4) mat[Vec2(1, 1)] = 16 mat.rotate(3) mat_ref = MutSqMat(4) mat_ref[Vec2(2, 1)] = 16 self.assertEqual(mat_ref, mat)
def test_hash(self): vec_1 = Vec2(678, 32) vec_2 = Vec2(678, 32) vec_3 = Vec2(677, 31) hash_vec_1 = hash(vec_1) hash_vec_2 = hash(vec_2) hash_vec_3 = hash(vec_3) self.assertEqual(hash_vec_1, hash_vec_2) self.assertNotEqual(hash_vec_1, hash_vec_3) self.assertNotEqual(hash_vec_2, hash_vec_3)
def test_place_two_working(self): game = Game(4, 2, 2048) game.mat._items = [ 2, 2, 8, 16, 32, None, 128, 256, 512, 1024, None, 2, 4, 4, 16, 32 ] game.place_two() first_gap = game.mat[Vec2(1, 1)] second_gap = game.mat[Vec2(2, 2)] self.assertIn(2, (first_gap, second_gap))
def rotate(self, right_angles: int): """ Rotates the matrix by the given amount of right angles counter-clockwise. """ right_angles %= 4 if right_angles == 0: return old = self.copy() for x in range(self.width): for y in range(self.width): self[Vec2(x, y)] = old[Vec2(self.width - 1 - y, x)] self.rotate(right_angles - 1)
def test_hash(self): mat = MutSqMat(4) mat[Vec2(2, 3)] = 32 mat_ref = mat.copy() mat_other = MutSqMat(4) mat_other[Vec2(1, 0)] = 2 hash_mat = hash(mat) hash_mat_ref = hash(mat_ref) hash_mat_other = hash(mat_other) self.assertEqual(hash_mat_ref, hash_mat) self.assertNotEqual(hash_mat_other, hash_mat)
def test_contains(self): mat = MutSqMat(4) self.assertNotIn(64, mat) mat[Vec2(0, 2)] = 64 self.assertIn(64, mat)
def stuck(self) -> bool: """ Determines whether the game is stuck (i.e. no action can be performed). """ if None in self.mat: return False for x in range(self.width): for y in range(self.width): value = self.mat[Vec2(x, y)] # Does any tile have an adjacent tile with the same value? if 0 < x and value == self.mat[Vec2(x - 1, y)] \ or 0 < y and value == self.mat[Vec2(x, y - 1)] \ or x < self.width - 1 and value == self.mat[Vec2(x + 1, y)] \ or y < self.width - 1 and value == self.mat[Vec2(x, y + 1)]: return False return True
def test_stuck(self): game = Game(4, 2, 2048) self.assertFalse(game.stuck()) game.mat._items = [ 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 2, 4, 8, 16, 32 ] self.assertTrue(game.stuck()) game.mat[Vec2(1, 1)] = 4 self.assertFalse(game.stuck())
def __init__(self): tkinter.Frame.__init__(self) self.all_gamevis = [] self.grid() self.master.title('vis2048 - Demo with 4 simultaneous games') self.master.bind("<Up>", self._key_down(self._up)) self.master.bind("<Down>", self._key_down(self._down)) self.master.bind("<Left>", self._key_down(self._left)) self.master.bind("<Right>", self._key_down(self._right)) background = tkinter.Frame() background.grid() for i in range(4): gamevis = GameVis(master=background, font_size=20) gamevis.grid(row=i // 2, column=i % 2, padx=5, pady=5) self.all_gamevis.append(gamevis) if i == 0: gamevis.game.mat[Vec2(0, 0)] = 1024 gamevis.game.mat[Vec2(0, 1)] = 1024 gamevis.update_vis()
def update_vis(self): """ Update the visualization. Should be called whenever the game itself is mutated. """ for i in range(self.game.width): for j in range(self.game.width): new_number = self.game.mat[Vec2(j, i)] if new_number is None: self.grid_cells[i][j].configure(text="", bg=BG_COLOR_TILE[None]) else: self.grid_cells[i][j].configure( text=str(new_number), bg=BG_COLOR_TILE[new_number], fg=FG_COLOR_DICT[new_number]) if self.game.won(): self._show_msg('You Win!') elif self.game.stuck(): self._show_msg('You Lose!') self.update_idletasks() self.update()
def test_init(self): vec = Vec2(12, -3) self.assertEqual(12, vec.x) self.assertEqual(-3, vec.y)
def test_str(self): vec = Vec2(-11, 3) self.assertEqual('(-11, 3)', str(vec))
def test_eq_not(self): vec_1 = Vec2(11, 9) vec_2 = Vec2(12, 9) self.assertNotEqual(vec_1, vec_2)
def test_eq(self): vec_1 = Vec2(11, 9) vec_2 = Vec2(11, 9) self.assertEqual(vec_1, vec_2)