def test_straight_flush(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(5, spades)]
		hand_two = [Card(2, spades), Card(14, spades)]
		board = [Card(4, spades), Card(3, spades), Card(6, spades), Card(10, spades)]
		self.assertTrue(rc.is_straight_flush(hand_one, board)[0])
		self.assertFalse(rc.is_straight_flush(hand_two, board)[0])
예제 #2
0
def select_move_prioritizing_capture(player_stone_color, boards, depth):
    '''
    moves =
    [
        [point1, point2, ...] (from oldest to newest)
        ...
    ]
    [[(1,1)], []]
    '''
    rc = RuleChecker()
    most_recent_board = boards[0]
    opponent_stone_color = utils.get_opponent_stone_color(player_stone_color)
    moves = []
    if depth == 1:
        chains = most_recent_board.get_all_chains()
        opponent_chains = [
            chain for chain in chains
            if chain.stone_color == opponent_stone_color
        ]
        for chain in opponent_chains:
            if len(chain.liberties) == 1:
                liberties = list(chain.liberties)
                if rc.check_legality(player_stone_color, liberties[0], boards):
                    moves.append(liberties[0])
    if depth > 1:
        # Idea: define new_move as [point()]
        # all_moves = generate_all_moves_that_d
        # return select_simple_move(boards)
        raise NotImplementedError()
    if moves:
        return sorted(moves)
    else:
        return select_simple_move(player_stone_color, boards)
	def test_full_house(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(6, spades)]
		hand_two = [Card(2, spades), Card(7, diamonds)]
		board = [Card(2, diamonds), Card(2, hearts), Card(6, clubs), Card(5, diamonds)]
		self.assertEqual(rc.is_full_house(hand_one, board)[1], 2)
		self.assertEqual(rc.is_full_house(hand_one, board)[2], 6)
		self.assertFalse(rc.is_full_house(hand_two, board)[0])
	def test_pair(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(10, spades)]
		hand_two = [Card(6, spades), Card(11, clubs)]
		board = [Card(2, diamonds), Card(5, diamonds), Card(11, hearts)]
		self.assertEqual(rc.is_pair(hand_one, board)[1], 2)
		self.assertEqual(rc.is_pair(hand_one, board)[2][0], 11)
		self.assertEqual(rc.is_pair(hand_two, board)[1], 11)
	def test_quads(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(6, spades)]
		hand_two = [Card(2, spades), Card(3, spades)]
		board = [Card(2, diamonds), Card(2, hearts), Card(2, clubs), Card(5, diamonds)]
		self.assertTrue(rc.is_quads(hand_one, board)[0])
		self.assertEqual(rc.is_quads(hand_one, board)[1], 2)
		self.assertEqual(rc.is_quads(hand_one, board)[2], 6)
		self.assertEqual(rc.is_quads(hand_two, board)[2], 5)
	def test_flush(self):
		rc = RuleChecker()
		hand_one = [Card(2, clubs), Card(5, clubs)]
		hand_two = [Card(3, spades), Card(4, diamonds)]
		hand_three = [Card(8, diamonds), Card(10, clubs)]
		board = [Card(4, clubs), Card(6, clubs), Card(10, clubs), Card(5, spades)]
		board_with_flush = [Card(4, spades), Card(3, spades), Card(6, spades), Card(10, spades), Card(13, spades)]
		self.assertTrue(rc.is_flush(hand_one, board)[0])
		self.assertFalse(rc.is_flush(hand_two, board)[0])
		self.assertTrue(rc.is_flush(hand_three, board_with_flush)[0])
예제 #7
0
    def __init__(self):
        self.rules = RuleChecker()
        self.rules.add_rule('EMPID', "^[A-Z][0-9]{3}$")
        self.rules.add_rule('GENDER', "^(M|F)$")
        self.rules.add_rule('AGE', "^[0-9]{2}$")
        self.rules.add_rule('SALES', "^[0-9]{3}$")
        self.rules.add_rule('BMI', "^(Normal|Overweight|Obesity|Underweight)$")
        self.rules.add_rule('SALARY', "^[0-9]{2,3}$")
        self.rules.add_rule('BIRTHDAY', "^[1-31]-[1-12]-[0-9]{4}$")

        self.attributes = self.rules.get_fields()
        self.number_of_attributes = len(self.attributes)
예제 #8
0
def select_simple_move(player_stone_color, boards):
    '''
    input: boards
    output:
        if there is a move that self can make,
            returns a point with the lowest column index,
            and then with the lowest row index
        if there isn't,
            returns a string "pass"
    '''
    rc = RuleChecker()
    for point, maybe_stone in iter(boards[0]):
        if maybe_stone == EMPTY:
            if rc.check_legality(player_stone_color, point, boards):
                return str(point)
    return "pass"
예제 #9
0
class Player(object):
    def __init__(self, name, stone):
        self.name = name
        self.stone = stone
        self.rule_checker = RuleChecker()

    """
        takes in:
            * boards: a list of game boards
        returns:
            * A string representing a point of the first valid play
            * "pass", if no plays are possible
    """

    def make_a_move(self, boards):
        # check valid history
        boards = [Board(x) for x in boards]
        if not self.rule_checker.is_valid_game_history(self.stone, boards):
            return "This history makes no sense!"

        curr_empties = boards[0].get_empty_spots()

        for empty in curr_empties:
            if self.rule_checker.verify_play(self.stone, empty, boards):
                return self._create_point(empty[0], empty[1])
        return "pass"

    """
        returns the Point object for the coordinate integers point_x and point_y
    """

    def _create_point(self, point_x, point_y):
        return str(point_x + 1) + '-' + str(point_y + 1)

    """
        returns name of player
    """

    def get_name(self):
        return self.name
	def test_trips(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(10, spades)]
		hand_two = [Card(2, spades), Card(6, spades)]
		hand_three = [Card(2, spades), Card(3, spades)]
		board = [Card(2, diamonds), Card(2, hearts), Card(5, diamonds), Card(4, hearts)]
		self.assertTrue(rc.is_trips(hand_one, board)[0])
		self.assertEqual(rc.is_trips(hand_one, board)[1], 2)
		self.assertEqual(rc.is_trips(hand_one, board)[2][0], 10)
		self.assertEqual(rc.is_trips(hand_one, board)[2][1], 5)
		self.assertEqual(rc.is_trips(hand_two, board)[2][0], 6)
		self.assertEqual(rc.is_trips(hand_two, board)[2][1], 5)
		self.assertEqual(rc.is_trips(hand_three, board)[2][0], 5)
		self.assertEqual(rc.is_trips(hand_three, board)[2][1], 4)
	def test_two_pair(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(10, spades)]
		hand_two = [Card(6, spades), Card(6, clubs)]
		board = [Card(2, diamonds), Card(10, hearts), Card(5, diamonds), Card(5, hearts)]
		self.assertTrue(rc.is_two_pair(hand_one, board)[0])
		self.assertEqual(rc.is_two_pair(hand_one, board)[1], 10)
		self.assertEqual(rc.is_two_pair(hand_one, board)[3], 5)
		self.assertTrue(rc.is_two_pair(hand_two, board)[0])
		self.assertEqual(rc.is_two_pair(hand_two, board)[1], 6)
		self.assertEqual(rc.is_two_pair(hand_two, board)[3], 10)

		# same top pair but different bottom pair
		hand_one = [Card(14, spades), Card(14, hearts)]
		hand_two = [Card(13, spades), Card(12, clubs)]
		board = [Card(13, diamonds), Card(12, hearts), Card(2, diamonds), Card(3, hearts), Card(2, hearts)]
		self.assertEqual(rc.is_two_pair(hand_one, board)[1], 14)
		self.assertEqual(rc.is_two_pair(hand_one, board)[3], 13)
		self.assertEqual(rc.is_two_pair(hand_two, board)[1], 13)
		self.assertEqual(rc.is_two_pair(hand_two, board)[3], 3)
예제 #12
0
 def __init__(self, input_):
     self._verify_input_(input_)
     self.ret_value = RuleChecker(input_).ret()
예제 #13
0
 def __init__(self, name, stone):
     self.name = name
     self.stone = stone
     self.n = 1
     self.opp_stone = self._get_opponent_stone(stone)
     self.rule_checker = RuleChecker()
예제 #14
0
class Player(object):
    def __init__(self, name, stone):
        self.name = name
        self.stone = stone
        self.n = 1
        self.opp_stone = self._get_opponent_stone(stone)
        self.rule_checker = RuleChecker()

    """
        takes in:
            * boards: a list of game boards
        returns:
            * A string representing a point, either the first point played that leads to a capture within n moves,
            or if no captures are possible, first valid play
            * "pass", if no plays are possible
    """
    def make_a_move(self, boards):
        # check valid history
        boards = [Board(x) for x in boards]
        if not self.rule_checker.is_valid_game_history(self.stone, boards):
            return "This history makes no sense!"

        curr_empties = boards[0].get_empty_spots()

        #find first move that allows capture within n moves
        for empty in curr_empties:
            if self.choose_move(copy.deepcopy(boards), self.n, empty):
                return self._create_point(empty[0], empty[1])
        #if there is no move which allows a capture, pick first available valid spot
        for empty in curr_empties:
            if self.rule_checker.verify_play(self.stone, empty, boards):
                return self._create_point(empty[0], empty[1])
        #if no valid moves, return "pass"
        return "pass"
    
    """
        returns the Point object for the coordinate integers point_x and point_y
    """
    def _create_point(self, point_x, point_y):
        return str(point_x + 1) + '-' + str(point_y + 1) 
    
    """
        takes in:
            * boards: list of Boards with the game history
            * point: n, then number of turns left to make a capture
            * empty: the empty spot to simulate play in
        returns:
            * True, if placing the stone at empty leads to a capture
            * False, otherwise
    """
    def choose_move(self, boards, n, empty):
        #if there are still moves left to simulate
        if n > 0:
            
            if self.rule_checker.verify_play(self.stone, empty, boards):
                new_board = self.rule_checker.play_move(self.stone, empty, copy.deepcopy(boards[0]))
                if self.check_if_capture(new_board, boards[0]) == True: return True
                else:
                    boards = self.update_history(new_board, boards)
                    curr_empties = new_board.get_empty_spots()
                    for empty in curr_empties:
                        if self.choose_move(boards, n-1, empty): return True
        return False

    """
        takes in:
            * new_board: the new board
            * boards: previous history
        returns:
            * a Board list containing history of the last 3 plays
    """
    def update_history(self, new_board, boards):
        boards.insert(0, new_board)
        if len(boards)>4: 
            boards.pop()
        return boards

    """
        takes in:
            * new_board: the new game Board
            * old_board: the previous game Board
        returns:
            * True, if the number of opponent stones on the board has decrease (there has been a capture)
            * False, otherwise
    """
    def check_if_capture(self, new_board, old_board):
        opp_stone_count_before = len(old_board.get_points(self.opp_stone))
        opp_stone_count_after = len(new_board.get_points(self.opp_stone))
        return opp_stone_count_after < opp_stone_count_before

    """
        returns name of player
    """
    def get_name(self):
        return self.name
    
    """
        takes in:
            * stone: stone of player
        returns:
            * the stone of the opposing player
    """
    def _get_opponent_stone(self, stone):
        if stone == "W":
            return "B"
        else:
            return "W"
	def test_analyze_hand(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(7, spades)]
		board = [Card(4, spades), Card(3, spades), Card(6, spades), Card(10, spades)]
		# TODO write more tests
		hand_two = [Card(2, diamonds), Card(13, hearts)]
	def test_straight(self):
		rc = RuleChecker()
		hand_one = [Card(2, spades), Card(6, spades)]
		hand_two = [Card(7, clubs), Card(8, diamonds)]
		hand_three = [Card(2, spades), Card(13, hearts)]
		board = [Card(4, hearts), Card(3, diamonds), Card(5, spades)] 
		board_two = [Card(4, hearts), Card(3, diamonds), Card(5, spades), Card(6, hearts)]
		self.assertTrue(rc.is_straight(hand_one, board)[0])
		self.assertEqual(rc.is_straight(hand_one, board)[1], 6)
		self.assertFalse(rc.is_straight(hand_two, board)[0])
		self.assertTrue(rc.is_straight(hand_one, board_two)[0])
		self.assertTrue(rc.is_straight(hand_two, board_two)[0])
		self.assertTrue(rc.is_straight(hand_three, board_two)[0])

		# Ace straight edge cases
		hand_one = [Card(14, spades), Card(2, hearts)]
		board = [Card(4, hearts), Card(3, diamonds), Card(5, spades)] 
		board_two = [Card(13, hearts), Card(12, diamonds), Card(11, spades), Card(10, spades)] 
		self.assertTrue(rc.is_straight(hand_one, board)[0])
		self.assertEqual(rc.is_straight(hand_one, board)[1], 5)
		self.assertTrue(rc.is_straight(hand_one, board_two)[0])
		self.assertEqual(rc.is_straight(hand_one, board_two)[1], 14)

		# Straight on board 
		hand_one = [Card(14, spades), Card(14, hearts)]
		hand_two = [Card(14, diamonds), Card(8, diamonds)]
		board_with_straight = [Card(4, hearts), Card(3, diamonds), Card(5, spades), Card(6, hearts), Card(7, clubs)]
		self.assertTrue(rc.is_straight(hand_one, board_with_straight)[0])
		self.assertEqual(rc.is_straight(hand_one, board_with_straight)[1], 7)
		self.assertEqual(rc.is_straight(hand_two, board_with_straight)[1], 8)
예제 #17
0
 def __init__(self, name, stone):
     self.name = name
     self.stone = stone
     self.rule_checker = RuleChecker()
예제 #18
0
class Validator(IFileValidator):

    # Tim
    def __init__(self):
        self.rules = RuleChecker()
        self.rules.add_rule('EMPID', "^[A-Z][0-9]{3}$")
        self.rules.add_rule('GENDER', "^(M|F)$")
        self.rules.add_rule('AGE', "^[0-9]{2}$")
        self.rules.add_rule('SALES', "^[0-9]{3}$")
        self.rules.add_rule('BMI', "^(Normal|Overweight|Obesity|Underweight)$")
        self.rules.add_rule('SALARY', "^[0-9]{2,3}$")
        self.rules.add_rule('BIRTHDAY', "^[1-31]-[1-12]-[0-9]{4}$")

        self.attributes = self.rules.get_fields()
        self.number_of_attributes = len(self.attributes)

    # Tim
    def check_data_set(self, data_set):
        # Should be of form [{EMPID: B12, GENDER: M, AGE: 22, etc}, {EMPID: 55Y, GENDER: F, etc}]
        if len(data_set) == 0:
            print('The data was empty', file=sys.stderr)
            return False
        else:
            for employee in data_set:
                if not self.check_line(employee):
                    print('One or more of the lines of data was invalid',
                          file=sys.stderr)
                    return False
        # Failing to invalidate is a success
        return True

    # Tim
    def check_line(self, employee_attributes):
        # Should be of form {EMPID: B12, GENDER: M, AGE: 22, etc}
        for attribute in self.attributes:
            if attribute not in employee_attributes:
                print('Missing attribute: {}'.format(attribute),
                      file=sys.stderr)
                return False
        try:
            if not self.check_all(employee_attributes):
                return False
        except TypeError:
            print('The data was not bundled correctly', file=sys.stderr)
            return False
        # Failing to invalidate is a success
        return True

    # Rosemary
    def check_all(self, employee_attributes):
        for attribute in employee_attributes:
            if not self.check_field(attribute, employee_attributes[attribute]):
                return False
        return True

    # Tim
    def check_birthday(self, birthday):
        try:
            day_month_year = birthday.split("-")
            day = int(day_month_year[0])
            month = int(day_month_year[1])
            year = int(day_month_year[2])
            date.datetime(year, month, day)
            return True
        except ValueError:
            print('The date was invalid', file=sys.stderr)
            return False
        except AttributeError:
            print('The date was in an invalid format', file=sys.stderr)
            return False

    def check_field(self, field, value):
        try:
            if not self.rules.check_field(field, value):
                print('{0} is invalid {1}!'.format(value, field),
                      file=sys.stderr)
                return False
        except TypeError:
            return False
        else:
            return True

    # Tim
    def check_birthday_against_age(self, birthday, age):
        # Tim
        """
        >>> v = Validator()
        >>> v.check_birthday_against_age('19-06-1988', 28)
        False
        >>> v.check_birthday_against_age('19-06-1988', 29)
        True
        >>> v.check_birthday_against_age('19-06-1988', 30)
        False
        >>> v.check_birthday_against_age('19-12-1988', 27)
        False
        >>> v.check_birthday_against_age('19-12-1988', 28)
        True
        >>> v.check_birthday_against_age('19-12-1988', 29)
        False
        >>> v.check_birthday_against_age('19-12-1988', 30)
        False
        """
        if not self.check_birthday(birthday):
            return False
        else:
            day_month_year = birthday.split("-")
            day = int(day_month_year[0])
            month = int(day_month_year[1])
            year = int(day_month_year[2])
            # adding age because we just want to compare month and day
            birth = date.datetime(year, month, day)
            today = date.datetime.today()
            if birth.month < today.month:
                # Had a birthday already this year
                return int(age) == today.year - year
            elif birth.month == today.month and birth.day < today.day:
                # Had a birthday already this year (this month)
                return int(age) == today.year - year
            else:
                # Hasn't had a birthday yet this year.
                return int(age) == today.year - year - 1

    # Tim
    def check_in_attributes(self, query_attribute):
        # Tim
        """
        >>> v = Validator()
        >>> v.check_in_attributes("EMPID")
        True
        >>> v.check_in_attributes("GENDER")
        True
        >>> v.check_in_attributes("AGE")
        True
        >>> v.check_in_attributes("SALES")
        True
        >>> v.check_in_attributes("BMI")
        True
        >>> v.check_in_attributes("SALARY")
        True
        >>> v.check_in_attributes("BIRTHDAY")
        True
        >>> v.check_in_attributes("Salary")
        True
        >>> v.check_in_attributes("SALE")
        False
        >>> v.check_in_attributes(1)
        False
        """
        try:
            return query_attribute.upper() in self.attributes
        except AttributeError:
            return False