def test_move_down_false(self): """ Tests that the function returns false since no tiles can and were not moved """ board = [[2, 4, 8, 16], [16, 8, 4, 2], [32, 64, 128, 256], [256, 128, 64, 32]] expected_board = [[2, 4, 8, 16], [16, 8, 4, 2], [32, 64, 128, 256], [256, 128, 64, 32]] bool = move_down(board) self.assertEqual(board, expected_board) self.assertFalse(bool)
def test_move_down_merge(self): """ Tests that the tiles that can be merged, adjacent neighbors with same value, are merged Return value should be true """ board = [[2, 4, 8, 16], [2, 4, 8, 16], [32, 64, 128, 256], [32, 64, 128, 256]] expected_board = [[0, 0, 0, 0], [0, 0, 0, 0], [4, 8, 16, 32], [64, 128, 256, 512]] bool = move_down(board) self.assertEqual(board, expected_board) self.assertTrue(bool)
def test_move_down_from_top(self): """ All the values from the top two rows should fill rows 2 and 3, and their original positions should now have 0 Return value should be true """ board = [[128, 64, 32, 16], [8, 256, 16, 64], [0, 0, 0, 0], [0, 0, 0, 0]] expected_board = [[0, 0, 0, 0], [0, 0, 0, 0], [128, 64, 32, 16], [8, 256, 16, 64]] bool = move_down(board) self.assertEqual(board, expected_board) self.assertTrue(bool)
def generate_children(board, prob, type): #Prune nodes with probability less than 0.1% #if prob < 0.1: # return [], [], [], -1 children = [] moves = [] probs = [] child_type = -1 #Generate all possible children, that is all moves possible. if type == NODE_TYPE_PLAYER: child_type = NODE_TYPE_RANDOM t1 = [i[:] for i in board] game_logic.move_up(t1) if game_logic.check_board(board, t1) == 1: children.append(t1) moves.append(0) probs.append(0.25) t2 = [i[:] for i in board] game_logic.move_right(t2) if game_logic.check_board(board, t2) == 1: children.append(t2) moves.append(1) probs.append(0.25) t3 = [i[:] for i in board] game_logic.move_down(t3) if game_logic.check_board(board, t3) == 1: children.append(t3) moves.append(2) probs.append(0.25) t4 = [i[:] for i in board] game_logic.move_left(t4) if game_logic.check_board(board, t4) == 1: children.append(t4) moves.append(3) probs.append(0.25) else: #Generate all possible children made by the computer child_type = NODE_TYPE_PLAYER zeroes = 0 for i in board: zeroes += i.count(0) for y in range(len(board)): for x in range(len(board[0])): if (board[y][x] == 0): t1 = [i[:] for i in board] t2 = [i[:] for i in board] t1[y][x] = 1 t2[y][x] = 2 children.append(t1) probs.append(0.9 / float(zeroes)) moves.append(-1) children.append(t2) probs.append(0.1 / float(zeroes)) moves.append(-1) return children, probs, moves, child_type
def main(stdscr): init_curses() # centering height, width = stdscr.getmaxyx() offset_x = int(width // 2) - int(BOARD_WIDTH / 2) offset_y = int(height // 2) - int(BOARD_HEIGHT / 2) message_y = offset_y + BOARD_HEIGHT + 1 score_y = offset_y - 1 exit_message_x = int(width // 2) - int(len(EXIT_MESSAGE) // 2) end_message_x = int(width // 2) - int(len(END_MESSAGE) // 2) won_message_x = int(width // 2) - int(len(WON_MESSAGE) // 2) lost_message_x = int(width // 2) - int(len(LOST_MESSAGE) // 2) board = initialize_board() paint_board(stdscr, board, offset_y, offset_x) while True: game_over, game_won, score = is_game_over(board) if game_won: stdscr.addstr(message_y, won_message_x, WON_MESSAGE) stdscr.addstr(message_y + 2, end_message_x, END_MESSAGE) while True: c = stdscr.getch() if c == ord('q'): return if game_over: stdscr.addstr(message_y, lost_message_x, LOST_MESSAGE) stdscr.addstr(message_y + 2, end_message_x, END_MESSAGE) while True: c = stdscr.getch() if c == ord('q'): return stdscr.addstr(score_y, offset_x, f"Current Score: {score}") c = stdscr.getch() moved_tiles = False if c == curses.KEY_UP: moved_tiles = move_up(board) elif c == curses.KEY_DOWN: moved_tiles = move_down(board) elif c == curses.KEY_LEFT: moved_tiles = move_left(board) elif c == curses.KEY_RIGHT: moved_tiles = move_right(board) elif c == ord('q'): stdscr.addstr(message_y, exit_message_x, EXIT_MESSAGE) if stdscr.getch() == ord('y'): return stdscr.addstr(message_y, exit_message_x, " " * len(EXIT_MESSAGE)) if moved_tiles: add_random_tile(board) paint_board(stdscr, board, offset_y, offset_x)