def __init__(self): """ Start the Board with empty positions :param size: size of board sizeXsize format """ self._table = np.chararray((self.SIZE, self.SIZE)) self._table[:] = Piece.NONE self._table = self._table.astype(np.str) self.score = Score(self._table) self.current_play = Square(-1, -1)
def update(self, **params): field = params["field"] score = params["score"] burstedPuyoGroups = field.burst() self.whileChaining.chainingCount += 1 score.add( Score.calcScore(self.whileChaining.chainingCount, burstedPuyoGroups)) return Delay(10, WhileChaining.WhileDropping(self.whileChaining))
def run(self, players, view): if len(players) < 2: raise "Not enough players to run a tournament." outcomes = [] player_count = len(players) for i in range(0, player_count, 2): if i == player_count - 1: outcomes.insert(0, (players[i], Score(0, 0, 0))) else: outcomes.append( self.determine_outcome(players[i], players[i + 1], view)) return outcomes[0] if player_count == 2 else self.run( [outcome[0] for outcome in outcomes], view)
def __init__(self): self.fieldRenderer = FieldRenderer() self.tsumoRenderer = TsumoRenderer() self.tsumoQueueRenderer = TsumoQueueRenderer() self.holdRenderer = HoldRenderer() self.scoreRenderer = ScoreRenderer() self.field = Field(13, 6) self.tsumoQueue = TsumoQueue() self.hold = Hold() self.state = Idle() self.score = Score() pyxel.init(124, 148, caption="Puyoris") pyxel.load(os.getcwd() + "/resource/my_resource.pyxel") pyxel.run(self.update, self.draw)
def setUp(self): self.board = Board() self.score = Score(self.board.table)
class ScoreTests(unittest.TestCase): def setUp(self): self.board = Board() self.score = Score(self.board.table) def test_search_winner_by_line(self): print_test_name(" starting test_search_winner_by_line ") square = Square(1, 6) result = self.score.has_winner(Piece.BLACK, square) self.assertNotEqual(Piece.BLACK, result) self.board.table[1, 5:10] = Piece.BLACK square = Square(1, 5) print(self.board) print() result = self.score.has_winner(Piece.BLACK, square) self.assertEqual(Piece.BLACK, result) def test_search_winner_by_column(self): print_test_name(' starting test_search_winner_by_column ') square = Square(5, 1) result = self.score.has_winner(Piece.WHITE, square) self.assertNotEqual(Piece.WHITE, result) self.board.table[5:10, 1] = Piece.WHITE square = Square(9, 1) print(self.board) print() result = self.score.has_winner(Piece.WHITE, square) self.assertEqual(Piece.WHITE, result) def test_search_winner_by_diagonal(self): print_test_name(' starting test_search_winner_by_diagonal ') square = Square(9, 3) result = self.score.has_winner(Piece.BLACK, square) self.assertNotEqual(Piece.BLACK, result) range = self.board.table[8:13, 2:7] range[np.diag_indices_from(range)] = Piece.BLACK print(self.board) print() result = self.score.has_winner(Piece.BLACK, square) self.assertEqual(Piece.BLACK, result) def test_search_winner_by_opposite_diagonal(self): print_test_name(' starting test_search_winner_by_opposite_diagonal ') square = Square(9, 2) result = self.score.has_winner(Piece.BLACK, square) self.assertNotEqual(Piece.BLACK, result) range = self.board.table[5:10, 2:7] range = range[:, ::-1] range[np.diag_indices_from(range)] = Piece.BLACK print(self.board) print() result = self.score.has_winner(Piece.BLACK, square) self.assertEqual(Piece.BLACK, result) def test_heuristic_4_sequence_with_outmost_opened(self): """ ..xxxx.. """ print_test_name(' test_heuristic_4_sequence_with_outmost_opened ') self.board.table[8, 3:7] = Piece.BLACK line = self.board.table[8, 1:9] current_score_value = self.score.heuristic(Piece.BLACK, line) expected_score = SCORE_POINT[ScoreEnum.FOUR] * 2 self.assertEqual(expected_score, current_score_value) def test_heuristic_4_sequence_with_1_outmost_opened(self): """ .oxxxx.. """ print_test_name(' test_heuristic_4_sequence_with_1_outmost_opened ') self.board.table[8, 3:7] = Piece.BLACK self.board.table[8, 2] = Piece.WHITE line = self.board.table[8, 1:9] current_score_value = self.score.heuristic(Piece.BLACK, line) expected_score = SCORE_POINT[ScoreEnum.FOUR] self.assertEqual(expected_score, current_score_value) def test_heuristic_4_sequence_with_1_outmost_closed(self): """ .oxxxxo. """ print_test_name(' test_heuristic_4_sequence_with_1_outmost_closed ') self.board.table[8, 3:7] = Piece.BLACK self.board.table[8, 2] = Piece.WHITE self.board.table[8, 7] = Piece.WHITE line = self.board.table[8, 1:9] current_score_value = self.score.heuristic(Piece.BLACK, line) expected_score = 0 self.assertEqual(expected_score, current_score_value) def test_heuristic_3_blank_1(self): """ ..xxx.x.. """ print_test_name(' test_heuristic_3_blank_1 ') self.board.table[8, 3:6] = Piece.BLACK self.board.table[8, 7] = Piece.BLACK print(self.board) line = self.board.table[8, 1:9] current_value = self.score.heuristic(Piece.BLACK, line) expected_value = SCORE_POINT[ScoreEnum.FOUR] self.assertEqual(expected_value, current_value)
def _clear_results(self, players): self.results.clear() for player in players: self.results[player] = Score(0, 0, 0)
class ScoreTests(unittest.TestCase): def setUp(self): self.board = Board() self.score = Score(self.board.table) def test_search_winner_by_line(self): square = Square(1, 6) result = self.score.has_winner(Piece.BLACK, square) self.assertNotEqual(Piece.BLACK, result) self.board.table[1, 5:10] = Piece.BLACK square = Square(1, 5) print(self.board) print() result = self.score.has_winner(Piece.BLACK, square) self.assertEqual(Piece.BLACK, result) def test_search_winner_by_column(self): square = Square(5, 1) result = self.score.has_winner(Piece.WHITE, square) self.assertNotEqual(Piece.WHITE, result) self.board.table[5:10, 1] = Piece.WHITE square = Square(9, 1) print(self.board) print() result = self.score.has_winner(Piece.WHITE, square) self.assertEqual(Piece.WHITE, result) def test_search_winner_by_diagonal(self): square = Square(9, 3) result = self.score.has_winner(Piece.BLACK, square) self.assertNotEqual(Piece.BLACK, result) range = self.board.table[8:13, 2:7] range[np.diag_indices_from(range)] = Piece.BLACK print(self.board) print() result = self.score.has_winner(Piece.BLACK, square) self.assertEqual(Piece.BLACK, result) def test_search_winner_by_opposite_diagonal(self): square = Square(9, 2) result = self.score.has_winner(Piece.BLACK, square) self.assertNotEqual(Piece.BLACK, result) range = self.board.table[5:10, 2:7] range = range[:, ::-1] range[np.diag_indices_from(range)] = Piece.BLACK print(self.board) print() result = self.score.has_winner(Piece.BLACK, square) self.assertEqual(Piece.BLACK, result) def test_heuristic_4_sequence_with_outmost_opened(self): """ ..xxxx.. """ self.board.table[8, 3:7] = Piece.BLACK line = self.board.table[8, 1:9] current_score_value = self.score.heuristic(Piece.BLACK, line) expected_score = SCORE_POINT[ScoreEnum.FOUR] * 2 self.assertEqual(expected_score, current_score_value) def test_heuristic_4_sequence_with_1_outmost_opened(self): """ .oxxxx.. """ self.board.table[8, 3:7] = Piece.BLACK self.board.table[8, 2] = Piece.WHITE line = self.board.table[8, 1:9] current_score_value = self.score.heuristic(Piece.BLACK, line) expected_score = SCORE_POINT[ScoreEnum.FOUR] self.assertEqual(expected_score, current_score_value) def test_heuristic_4_sequence_with_1_outmost_closed(self): """ .oxxxxo. """ self.board.table[8, 3:7] = Piece.BLACK self.board.table[8, 2] = Piece.WHITE self.board.table[8, 7] = Piece.WHITE line = self.board.table[8, 1:9] current_score_value = self.score.heuristic(Piece.BLACK, line) expected_score = 0 self.assertEqual(expected_score, current_score_value) def test_heuristic_3_blank_1(self): """ ..xxx.x.. """ self.board.table[8, 3:6] = Piece.BLACK self.board.table[8, 7] = Piece.BLACK print(self.board) line = self.board.table[8, 1:9] current_value = self.score.heuristic(Piece.BLACK, line) expected_value = SCORE_POINT[ScoreEnum.FOUR] self.assertEqual(expected_value, current_value)
class Board: """ Board representation of the console game """ SIZE = 15 def __init__(self): """ Start the Board with empty positions :param size: size of board sizeXsize format """ self._table = np.chararray((self.SIZE, self.SIZE)) self._table[:] = Piece.NONE self._table = self._table.astype(np.str) self.score = Score(self._table) self.current_play = Square(-1, -1) def __len__(self): """ :return: length of border """ return self.SIZE def __eq__(self, other): """ Compare borders :param other: :return: """ size_equals = self.SIZE == len(other) score_equals = self.score.value == other.current_score table_equals = np.array_equal(self._table, other.table) return size_equals and score_equals and table_equals def __repr__(self): """ Format the boarder :return: the board in str format """ table_copy = self._table.tolist() if self.current_play.is_valid(): table_copy[self.current_play.row][self.current_play.col] = Format.BOLD + \ Format.Color.GREEN + \ table_copy[self.current_play.row][ self.current_play.col] + \ ' ' + \ Format.END formatter = ' '.join( ['{' + str(i) + ':^2}' for i in range(self.SIZE + 1)]) range_table = range(self.SIZE) idx_table = list(range_table) ## top index table ## idx_table_str = [str(n) for n in idx_table] top_idx_table_str = [' '] + idx_table_str top_idx_f = [formatter.format(*top_idx_table_str)] ## left index table ## left_idx_table = [[str(i)] + table_copy[i] for i in range_table] left_idx_table_f = [formatter.format(*line) for line in left_idx_table] str_table = '\n'.join(top_idx_f + left_idx_table_f) return str_table def get_piece(self, board_space): return self._table[board_space.row, board_space.col] def take_up_space(self, piece, board_space): """ Current movement in border :param piece: the piece played :param board_space: is a position played in matrix ex. BoardSpace(row, col) :return: the winner or Piece.NONE if no there winner """ self.current_play = board_space self._table[board_space.row, board_space.col] = piece winner = self.score.has_winner(piece, board_space) if self._table.count(Piece.NONE).sum() == 0 and winner == Piece.NONE: raise NotBlankSpaceException return winner def restore_move(self, board_space, score): self._table[board_space.row, board_space.col] = Piece.NONE self.score.value = score @property def table(self): return self._table @property def current_score(self): return self.score.value
class Board: """ Board representation of the console game """ SIZE = 15 def __init__(self): """ Start the Board with empty positions :param size: size of board sizeXsize format """ self._table = np.chararray((self.SIZE, self.SIZE)) self._table[:] = Piece.NONE self._table = self._table.astype(np.str) self.score = Score(self._table) self.current_play = Square(-1, -1) def __len__(self): """ :return: length of border """ return self.SIZE def __eq__(self, other): """ Compare borders :param other: :return: """ size_equals = self.SIZE == len(other) score_equals = self.score.value == other.current_score table_equals = np.array_equal(self._table, other.table) return size_equals and score_equals and table_equals def __repr__(self): """ Format the boarder :return: the board in str format """ table_copy = self._table.tolist() if self.current_play.is_valid(): table_copy[self.current_play.row][self.current_play.col] = Format.BOLD + \ Format.Color.GREEN + \ table_copy[self.current_play.row][ self.current_play.col] + \ ' ' + \ Format.END formatter = ' '.join(['{' + str(i) + ':^2}' for i in range(self.SIZE + 1)]) range_table = range(self.SIZE) idx_table = list(range_table) ## top index table ## idx_table_str = [str(n) for n in idx_table] top_idx_table_str = [' '] + idx_table_str top_idx_f = [formatter.format(*top_idx_table_str)] ## left index table ## left_idx_table = [[str(i)] + table_copy[i] for i in range_table] left_idx_table_f = [formatter.format(*line) for line in left_idx_table] str_table = '\n'.join(top_idx_f + left_idx_table_f) return str_table def get_piece(self, board_space): return self._table[board_space.row, board_space.col] def take_up_space(self, piece, board_space): """ Current movement in border :param piece: the piece played :param board_space: is a position played in matrix ex. BoardSpace(row, col) :return: the winner or Piece.NONE if no there winner """ self.current_play = board_space self._table[board_space.row, board_space.col] = piece winner = self.score.has_winner(piece, board_space) if self._table.count(Piece.NONE).sum() == 0 and winner == Piece.NONE: raise NotBlankSpaceException return winner def restore_move(self, board_space, score): self._table[board_space.row, board_space.col] = Piece.NONE self.score.value = score @property def table(self): return self._table @property def current_score(self): return self.score.value