示例#1
0
def verify_binary(binary, mode=GRID):

    for m in [GRID, GRID_STATE, LINE, GRID_4, GRID_STATE_4, LINE_4, GRID_5, GRID_STATE_5, LINE_5]:

        if m == mode:
            check_length = MODES[mode]["length"]
            break
    else:
        raise TicTacToeException(
            "An invalid binary mode was found:{} "
            "only acceptable binary modes are :{} ".format(mode, available_modes())
        )

    if isinstance(binary, (list, tuple, str)):

        if len(binary) != check_length:
            raise TicTacToeException(
                "Found a {} with {} items.The binary "
                "representation needs to be a {} bit "
                "binary representation".format(type(binary), len(binary), check_length)
            )

        if isinstance(binary, (list, tuple)):
            for n in binary:
                try:
                    int(n)
                except ValueError:
                    raise TicTacToeException(
                        "Found an invalid character:{}. Characters that " "are not 0 or 1 are invalid.".format(n)
                    )

        else:
            if not binary.isdigit():
                raise TicTacToeException("Binary does not include all digits.Non-number " " characters were found")

        bin_array = [int(n) for n in binary]

        for non_bin in non_binary(bin_array):
            raise TicTacToeException(
                "Found an invalid character:{}. Characters that " "are not 0 or 1 are invalid.".format(non_bin)
            )

    else:
        raise TicTacToeException(
            "Binary is not a str, list, or tuple containing 0 and 1. "
            "Instead, a {} type was found".format(type(binary))
        )
示例#2
0
def verify_hash(hash, mode=GRID):

    if not isinstance(hash, (int, long)):
        raise TicTacToeException("Hash must be an int or long")

    for m in [GRID, GRID_STATE, LINE, GRID_4, GRID_STATE_4, LINE_4, GRID_5, GRID_STATE_5, LINE_5]:

        if m == mode:
            if hash < 0 or hash >= (1 << MODES[mode]["length"]):
                TicTacToeException(
                    "An invalid hash:{} was passed for mode:{}, "
                    "only hash acceptables are from 0-{}".format(hash, mode, (1 << MODES[mode]["length"] - 1))
                )
            else:
                break
    else:
        raise TicTacToeException(
            "An invalid hash mode was found:{} " "only acceptable modes are :{} ".format(mode, available_modes())
        )