def test_opponentWinNoPieces(self): board = copy.deepcopy(TestBaseOpponent.DEFAULT_BLUE_BOARD) board['O'][:9] = ['0'] * 9 state = MatchState(board, [0, 0, 0], [0, 0, 0, 9], [0, 0, 0]) prob_match_over, winner = state.is_match_over() self.assertEqual(1.0, prob_match_over) self.assertEqual('O', winner)
def test_playerWinNoPieces(self): board = copy.deepcopy(TestBaseOpponent.DEFAULT_BLUE_BOARD) board['O'][9:] = ['0'] * 9 state = MatchState(board, [3, 3, 3], [3, 3, 3, 0], [3, 3, 3]) prob_match_over, winner = state.is_match_over() self.assertEqual(1.0, prob_match_over) self.assertEqual('P', winner)
def test_opponentWinCenterKnown(self): board = copy.deepcopy(TestBaseOpponent.DEFAULT_BLUE_BOARD) board['O'][0:9:3] = ['0'] * 3 board['O'][9] = '0' board['C'][0] = 'OS' state = MatchState(board, [0, 0, 0], [0, 0, 1, 8], [0, 3, 3]) prob_match_over, winner = state.is_match_over() self.assertEqual(1.0, prob_match_over) self.assertEqual('O', winner)
def test_playerWinCenter(self): board = copy.deepcopy(TestBaseOpponent.DEFAULT_BLUE_BOARD) board['O'][9:12] = ['0'] * 3 board['O'][2] = '0' board['C'][0] = 'PS' state = MatchState(board, [3, 0, 0], [3, 0, 0, 6], [3, 3, 3]) prob_match_over, winner = state.is_match_over() self.assertEqual(1.0, prob_match_over) self.assertEqual('P', winner)
def init_board_layout(self, player_side=0, layout=None): board = self._state.board index = 9 if player_side == 0 else 0 board['O'][index:index + 9] = ['OU'] * 9 if layout is None: layout = self._get_board_layout() index = 0 if player_side == 0 else 9 board['O'][index:index + 9] = ['P%s' % p for p in layout] self._state = MatchState(board) return layout
def reset_board(self, board): self._state = MatchState(board)
def __init__(self): self._state = MatchState()
class BaseOpponent: __metaclass__ = ABCMeta def __init__(self): self._state = MatchState() @property def board(self): return self._state.board @property def captures(self): return self._state.captures @property def counts(self): return self._state.counts @abstractmethod def _get_board_layout(self): layout = ['R', 'P', 'S'] * 3 return layout def init_board_layout(self, player_side=0, layout=None): board = self._state.board index = 9 if player_side == 0 else 0 board['O'][index:index + 9] = ['OU'] * 9 if layout is None: layout = self._get_board_layout() index = 0 if player_side == 0 else 9 board['O'][index:index + 9] = ['P%s' % p for p in layout] self._state = MatchState(board) return layout def reset_board(self, board): self._state = MatchState(board) @abstractmethod def get_player_hand(self): hand = 'R' return hand @abstractmethod def get_next_move(self): moves = self.get_possible_moves('P') return moves[0] if len(moves) > 0 else None def apply_move(self, move): if 'surrender' in move: return self._state.apply_move( (move['from'][0], int(move['from'][1:])), (move['to'][0], int(move['to'][1:])), move['outcome'], move['otherHand'] if 'otherHand' in move else None) def get_possible_moves(self, player): return [ '%s%s:%s%s' % (move[0] + move[1]) for move in self._state.get_possible_moves(player) ] def get_move_list(self, ring, index): return [('%s%s:%s%s' % (ring, index, move[0], move[1])) for move in self._state.get_piece_moves(ring, index)] def get_board_hash(self): return self._state.get_hash() def print_board(self, output=True): if output: print(str(self._state)) else: return str(self._state)
def test_opponentWinCenterOneUnknown(self): board = copy.deepcopy(TestBaseOpponent.DEFAULT_BLUE_BOARD) board['O'][0:9] = ['PR'] * 3 + ['PP'] * 3 + ['PS'] * 3 state = MatchState(board) state.apply_move(('O', 17), ('O', 0), 'W', 'P') # opponent state.apply_move(('O', 1), ('O', 0), 'L', 'P') # player state.apply_move(('O', 0), ('O', 1), 'M') # opponent state.apply_move(('O', 2), ('O', 1), 'L', 'P') # player state.apply_move(('O', 9), ('I', 4), 'M') # opponent state.apply_move(('O', 3), ('O', 2), 'M') # player state.apply_move(('I', 4), ('C', 0), 'M') # opponent prob_match_over, winner = state.is_match_over() prob_pieces = state.get_opponent_piece_probabilities() self.assertEqual(prob_pieces[2], prob_match_over) self.assertEqual('O', winner)
def test_notOver(self): state = MatchState(TestBaseOpponent.DEFAULT_BLUE_BOARD) prob_match_over, winner = state.is_match_over() self.assertEqual(0.0, prob_match_over)
def test_cloneDeepCopy(self): state1 = MatchState() state2 = state1.clone() self.assertIsNot(state1._board, state2._board) self.assertIsNot(state1._captures, state2._captures) self.assertIsNot(state1._opponent_counts, state2._opponent_counts)
def test_cloneDifferent(self): state1 = MatchState() state2 = state1.clone() self.assertIsNot(state1, state2)
def test_getEmptyBoardCounts(self): state = MatchState() self.assertEqual([0] * 4, state._opponent_counts)
def test_getDefaultBoardCounts(self): state = MatchState(TestBaseOpponent.DEFAULT_BLUE_BOARD) self.assertEqual([0] * 3 + [9], state._opponent_counts)
def test_getEmptyBoardProbabilities(self): state = MatchState() probabilities = state.get_opponent_piece_probabilities() self.assertEqual([0.0 / 3] * 3, probabilities)
def test_getDefaultBoardProbabilities(self): state = MatchState(TestBaseOpponent.DEFAULT_BLUE_BOARD) probabilities = state.get_opponent_piece_probabilities() self.assertEqual([1.0 / 3] * 3, probabilities)