def board_from_strs(rows, next_beans=None): for i in range(12 - len(rows)): rows.insert(0, b" ") board = [[rows[11 - y][x] for y in range(12)] for x in range(6)] return puyo.Board(board, next_beans)
def main(): import argparse parser = argparse.ArgumentParser(description=__doc__) ai_names = list(puyo.AI_REGISTRY.keys()) parser.add_argument("-a", "--ai", choices=ai_names, default=puyo.DEFAULT_AI_NAME, help="AI to run. Choose one of: {} " "(default: {})".format(ai_names, puyo.DEFAULT_AI_NAME)) parser.add_argument("-n", "--nuisance", type=float, default=0, dest="nuisance_probability", help="Probability (0-1) of a nuisance " "bean being dropped.") args = parser.parse_args() if args.nuisance_probability < 0 or args.nuisance_probability > 1: parser.error("Nuisance probability (-n, --nuisance) must be between " "0 and 1.") board = puyo.Board(next_beans=random_next_beans()) ai = puyo.AI_REGISTRY[args.ai]() print("Keys:") print(" Space: Make move") print(" Escape: Quit") last_board = None game_over = False cv2.namedWindow("Puyo Board") while True: cv2.imshow("Puyo Board", board.draw()) key = cv2.waitKey(-1) % 256 if key == 27: # Escape break elif key == ord(' '): if not game_over: last_board = board.copy() current_beans = board.next_beans board.next_beans = random_next_beans() position, rotation = ai.get_move(board.copy(), current_beans) #print current_beans, position, rotation if board.can_make_move(position, rotation): combo = board.make_move(current_beans, position, rotation) if combo.game_over: print "Game Over" game_over = True print_combo(combo) else: print "Invalid Move:", position, rotation if random.uniform(0, 1) < args.nuisance_probability: board.drop_nuisance(1) if game_over: print "Game Over" elif key == ord('u'): board = last_board game_over = False
def test_new_move_on_next_bean_change(self): """New move should be detected when the next bean changes.""" vision = puyo.Vision(bean_finder=MockBeanFinder(), timing_scheme="relative") board1 = puyo.Board(next_beans=(b'r', b'g')) board2 = puyo.Board(next_beans=(b'b', b'g')) state = vision.get_state(board1, 5) self.assertFalse(state.new_move) # Change in next bean state = vision.get_state(board2, 5) self.assertTrue(state.new_move) # No change state = vision.get_state(board2, 5) self.assertFalse(state.new_move) # Change state = vision.get_state(board1, 5) self.assertTrue(state.new_move)
def test_no_new_move_on_wrong_column(self): """No new move should be detected when beans falling in the wrong column.""" vision = puyo.Vision(bean_finder=MockBeanFinder(), timing_scheme="relative") board1 = puyo.Board(next_beans=(b'g', b'g')) state = vision.get_state(board1, 5) self.assertFalse(state.new_move) board2 = board1.copy() board2[3][11] = b'g' state = vision.get_state(board2, 5) self.assertFalse(state.new_move)
def test_no_new_move_on_wrong_colors1(self): """No new move should be detected when colors don't match the next bean.""" vision = puyo.Vision(bean_finder=MockBeanFinder(), timing_scheme="relative") board = puyo.Board(next_beans=(b'r', b'g')) state = vision.get_state(board, 5) self.assertFalse(state.new_move) # Yellow bean should not trigger new move tmp_board = board.copy() tmp_board[2][11] = b'y' state = vision.get_state(tmp_board, 5) self.assertFalse(state.new_move)
def test_new_move_on_seen_falling1(self): """New move should be detected when new beans are seen falling.""" vision = puyo.Vision(bean_finder=MockBeanFinder(), timing_scheme="relative") board1 = puyo.Board(next_beans=(b'r', b'g')) state = vision.get_state(board1, 5) self.assertFalse(state.new_move) # Seeing green should trigger a new move board2 = board1.copy() board2[2][11] = b'g' state = vision.get_state(board2, 5) self.assertTrue(state.new_move) # Seeing another should not trigger a new move state = vision.get_state(board2, 5) self.assertFalse(state.new_move)
def read_board_recording(filename): """ Board recording pickle files contain a list of (cells, next_beans, time) tuples. `cells` and `next_beans` define the current state of the board. If `cells` or `next_beans` is None, they are assumed to be the same as the previous. Time is measured relatively in seconds. """ filename = os.path.join(TEST_DATA_FOLDER, filename) data = pickle.load(open(filename)) last_cells = None last_next_beans = None for cells, next_beans, t in data: if cells is None: cells = last_cells if next_beans is None: next_beans = last_next_beans yield puyo.Board(cells, next_beans), t last_cells = cells last_next_beans = next_beans
def make_board(self, *args, **kwargs): board = puyo.Board(*args, c_accelerated=True, **kwargs) if not board.c_accelerated: raise RuntimeError("C acceleration not available, cannot test it.") return board
def make_board(self, *args, **kwargs): return puyo.Board(*args, c_accelerated=False, **kwargs)