Example #1
0
def get_move(board, engine, color, move_num, time, **kwargs):
    """ Get the move for the given engine and color. Check validity of the 
    move. """
    legal_moves = board.get_legal_moves(color)

    if not legal_moves:
        return None
    elif len(legal_moves) == 1:
        return legal_moves[0]
    else:
	try:
            move = engine.get_move(copy.deepcopy(board), color, move_num, time[color], time[-color])
	except Exception, e:
	    print traceback.format_exc()
	    raise SystemError(color)

        if move not in legal_moves:
            print "legal list", [move_string(m) for m in legal_moves]
            print "illegal", move_string(move), "=", move
            raise LookupError(color)

        return move
Example #2
0
def get_move(board, engine, color, move_num, time, **kwargs):
    """ Get the move for the given engine and color. Check validity of the
    move. """
    legal_moves = board.get_legal_moves(color)

    if not legal_moves:
        return None
    elif len(legal_moves) == 1:
        return legal_moves[0]
    else:
        try:
            move = engine.get_move(copy.deepcopy(board), color, move_num, time[color], time[-color])
        except Exception as e:
            print(traceback.format_exc())
            raise SystemError(color)

        if move not in legal_moves:
            print("legal list", [move_string(m) for m in legal_moves])
            print("illegal", move_string(move), "=", move)
            raise LookupError(color)

        return move
Example #3
0
def game(white_engine, black_engine, game_time=300.0, verbose=False):
    """ Run a single game. Raises RuntimeError in the event of time expiration.
    Raises LookupError in the case of a bad move. The tournament engine must
    handle these exceptions. """
    # TODO: Figure out a method of introspection to figure out which player
    # raised an exception

    # TODO: Make this a generator or coroutine to support multithreading.
    # Iteration stops at final board, makes for simple GUI on top of this.

    #initialize variables
    board = Board()
    time = { -1 : game_time, 1 : game_time }
    engine = { -1 : black_engine, 1 : white_engine }

    # do rounds
    for move_num in range(60):
        moves = []
        for color in [-1, 1]:
            start_time = times()[0] #user time elapsed
            move = get_move(board, engine[color], color, move_num)
            end_time = times()[0]
            time[color] -= (end_time - start_time) 

            logging.debug("Player %(color)d has %(time)f seconds remaining" %\
                            {'color' : color, 'time' : time[color]})
            if time[color] < 0:
                raise RuntimeError(
                    "Player %(color)d literally has a RuntimeError" 
                        % {'color' : color })

            # make a move, otherwise pass
            if move is not None:
                board.execute_move(move, color)
                moves.append(move)
                logging.info(
                    "Round %(move_num)2d: Player %(color)d flipped %(move)s"
                        % { 'color' : color,   
                            'move' : move_string(move), 
                            'move_num' : move_num })

                if verbose:
                    board.display()

        #test for game over
        if not moves:
            logging.info("No legal moves remaining. The game is over.")
            break

    return board
Example #4
0
def game(white_engine, black_engine, game_time=60.0, verbose=False):
    """ Run a single game. Raise RuntimeError in the event of time expiration.
    Raise LookupError in the case of a bad move. The tournament engine must
    handle these exceptions. """

    # Initialize variables
    board = Board()
    totaltime = {-1: game_time * 60, 1: game_time * 60}
    engine = {-1: black_engine, 1: white_engine}

    if verbose:
        print "INITIAL BOARD\n--\n"
        board.display(totaltime)

    # Do rounds
    # 每方最多走60次
    for move_num in range(60):
        moves = []
        for color in [-1, 1]:
            start_time = timeit.default_timer()
            move = get_move(board, engine[color], color, move_num, totaltime)
            end_time = timeit.default_timer()
            # Update user totaltime
            time = round(end_time - start_time, 1)
            totaltime[color] -= time

            if time > game_time or totaltime[color] < 0:
                raise RuntimeError(color)

            # Make a move, otherwise pass
            if move is not None:
                board.execute_move(move, color)
                moves.append(move)

                if verbose:
                    print "--\n"
                    print "Round " + str(move_num + 1) + ": " + player[
                        color] + " plays in " + move_string(move) + '\n'
                    board.display(totaltime)

        if not moves:
            # No more legal moves. Game is over.
            break

    print "FINAL BOARD\n--\n"
    board.display(totaltime)

    return board
Example #5
0
def game(black_engine, white_engine, game_time=30, verbose=False):
    """ Run a single game. Raise RuntimeError in the event of time expiration.
    Raise LookupError in the case of a bad move. The tournament engine must
    handle these exceptions. """

    # Initialize variables
    board = Board()
    totaltime = {-1: game_time * 60, 1: game_time * 60}  # in seconds
    engine = {-1: black_engine, 1: white_engine}

    if verbose:
        print("INITIAL BOARD\n\n--\n")
        board.display(totaltime)

    # Do rounds
    for move_num in range(60):
        moves = []
        for color in [-1, 1]:
            start_time = timeit.default_timer()
            move = get_move(board, engine[color], color, move_num, totaltime)
            end_time = timeit.default_timer()
            # Update user totaltime
            time = round(end_time - start_time, 1)
            totaltime[color] -= time

            if totaltime[color] < 0:
                raise RuntimeError(color)

            # Make a move, otherwise pass
            if move is not None:
                board.execute_move(move, color)
                moves.append(move)
                board.display(totaltime)
                raw_input("Press Enter to continue...")
                if verbose:
                    print(("--\n\nRound {}: {} plays in {}\n").format(
                        str(move_num + 1), player[color], move_string(move)))
                    board.display(totaltime)

        if not moves:
            break  # No more legal moves. Game is over.

    print("\n--------------------\nFINAL BOARD\n--\n")
    board.display(totaltime)

    board.totaltime = totaltime
    return board
Example #6
0
def game(white_engine, black_engine, game_time=300.0, verbose=False):
    """ Run a single game. Raise RuntimeError in the event of time expiration.
    Raise LookupError in the case of a bad move. The tournament engine must
    handle these exceptions. """

    # Initialize variables
    board = Board()
    time = {-1: game_time, 1: game_time}
    engine = {-1: black_engine, 1: white_engine}

    if verbose:
        print("INITIAL BOARD\n--\n")
        board.display(time)

    move_num = 0
    duplicateSet = set()

    # Do rounds
    for move_num in range(60):
        moves = []
        for color in [-1, 1]:
            start_time = timeit.default_timer()
            move = get_move(board, engine[color], color, duplicateSet,
                            move_num, time)
            end_time = timeit.default_timer()
            # Update user time
            time[color] -= round(end_time - start_time, 1)

            # Update experiments

            if time[color] < 0:
                print("timeout")
                # break
                board.display(time)
                return board

                # raise RuntimeError(color)

            # Make a move, otherwise pass
            if move is not None:
                board.execute_move(move, color)
                moves.append(move)

                if verbose:
                    print("--\n")
                    print("Round " + str(move_num + 1) + ": " + player[color] +
                          " plays in " + move_string(move) + '\n')
                    board.display(time)

        if not moves:
            # No more legal moves. Game is over.
            break
    print("Average nodes generated through the whole game: ",
          experiments["node"] / move_num)
    print("Average branching factor through the whole game: ",
          experiments["branch"] / move_num)
    print("Average duplicate nodes through the whole game: ",
          experiments["duplicate"] / move_num)

    print("FINAL BOARD\n--\n")
    board.display(time)

    return board
def game(white_engine, black_engine, game_time=300.0, verbose=False):
    """ Run a single game. Raise RuntimeError in the event of time expiration.
    Raise LookupError in the case of a bad move. The tournament engine must
    handle these exceptions. """

    # Initialize variables
    board = Board()
    time = { -1 : game_time, 1 : game_time }
    engine = { -1 : black_engine, 1 : white_engine }

    if verbose:
        print "INITIAL BOARD\n--\n"
        board.display(time)
    
    # Do rounds
    for move_num in range(60):
        moves = []
        for color in [-1, 1]:
            start_time = timeit.default_timer()
            move = get_move(board, engine[color], color, move_num, time)
            end_time = timeit.default_timer()
            # Update user time
            time[color] -= round(end_time - start_time, 1) 
            
            if time[color] < 0:
                raise RuntimeError(color)

            # Make a move, otherwise pass
            if move is not None:
                board.execute_move(move, color)
                moves.append(move)

                if verbose:
                    print "--\n"
                    print "Round " + str(move_num + 1) + ": " + player[color] + " plays in " + move_string(move) + '\n'
                    board.display(time)

        if not moves:
            # No more legal moves. Game is over.
            break

    print "FINAL BOARD\n--\n"
    board.display(time)

    return board
Example #8
0
def game(white_engine, black_engine, game_time=300.0, verbose=False):
    """ Run a single game. Raise RuntimeError in the event of time expiration.
    Raise LookupError in the case of a bad move. The tournament engine must
    handle these exceptions. """

    # Initialize variables
    board = Board()
    time = {-1: game_time, 1: game_time}
    engine = {-1: black_engine, 1: white_engine}

    if verbose:
        print("INITIAL BOARD\n--\n")
        board.display(time)

    # Do rounds
    for move_num in range(60):
        moves = []
        for color in [-1, 1]:
            start_time = timeit.default_timer()
            move = get_move(board, engine[color], color, move_num, time)
            end_time = timeit.default_timer()
            # Update user time
            time[color] -= round(end_time - start_time, 1)

            if time[color] < 0:
                raise RuntimeError(color)

            # Make a move, otherwise pass
            if move is not None:
                board.execute_move(move, color)
                moves.append(move)

                if verbose:
                    print("--\n")
                    print("Round " + str(move_num + 1) + ": " + player[color] + " plays in " + move_string(move) + '\n')
                    board.display(time)

        if not moves:
            # No more legal moves. Game is over.
            break

    print("FINAL BOARD\n--\n")
    board.display(time)
    print("Nodes Seen : ", engine[-1].nodesSeen)
    print("Parent Seen : ", engine[-1].parentNodes)
    print("Branch Seen : ", engine[-1].branchNodes)
    print("Duplicated Seen : ", engine[-1].duplicatedNodes)
    return board