Example #1
0
def main():
    while True:
        # get the new state of the game
        pw = PlanetWars()

        # make a turn (your code)
        do_turn(pw)

        # finish the turn
        pw.finish_turn()
    def __init__(self, original_pw):
        """
        Constructs a SimulatedPlanetWars object instance, given a PlanetWars object
        :type original_pw: PlanetWars
        """

        self._planets = []
        """:type : list[Planet]"""

        PlanetWars.__init__(self, clone=original_pw)
    def __init__(self, original_pw):
        """
        Constructs a SimulatedPlanetWars object instance, given a PlanetWars object
        :type original_pw: PlanetWars
        """

        self._planets = []
        """:type : list[Planet]"""

        PlanetWars.__init__(self, clone=original_pw)
Example #4
0
def main():
    # This is the main routine, it ...
    while True:
        # Creates a PlanetWars game state object
        pw = PlanetWars()

        # makes a turn
        do_turn(pw)

        # and finishes it
        pw.finish_turn()
Example #5
0
def do_turn(pw):
    """
    Function that gets called every turn.
    This is where to implement the strategies.

    Notice that a PlanetWars object called pw is passed as a parameter which you could use
    if you want to know what this object does, then read the API.

    The next line is to tell PyCharm what kind of object pw is (A PlanetWars object here)

    @type pw: PlanetWars
    """
    # The source variable will contain the planet from which we send the ships.
    # Create a source planet, if you want to know what this object does, then read the API
    source = None
    # The dest variable will contain the destination, the planet to which we send the ships.
    destination = None

    # generate state space from pw object
    gameState = generateInitState(pw)

    start = time.clock()
    ## Iterate over all possible positions and calculate score
    ### Right now, only iterate through all player planets and iterate
    moves = []
    moves = hillClimb(gameState)

    # Decide which actions can be taken for a selected planet
     ## Consist of: self, neutral, adversary
    if (len(moves) > 0):
        moves.sort(reverse=True)
        source = pw.get_planet(moves[0][1])
        destination = pw.get_planet(moves[0][2])

    end = time.clock()

    PlanetWars.log(formatResult(moves))
    PlanetWars.log("Action took %f sec" % (end - start))

    # (3) Attack/Defend
    # If the source and destination variables contain actual planets, then
    # send half of the ships from source to destination.
    if source is not None and destination is not None:
        pw.issue_order(source, destination)
def do_turn(pw):
    """
    Function that gets called every turn.
    This is where to implement the strategies.

    Notice that a PlanetWars object called pw is passed as a parameter which you could use
    if you want to know what this object does, then read the API.

    The next line is to tell PyCharm what kind of object pw is (A PlanetWars object here)

    @type pw: PlanetWars
    """
    # The source variable will contain the planet from which we send the ships.
    # Create a source planet, if you want to know what this object does, then read the API
    source = None
    # The dest variable will contain the destination, the planet to which we send the ships.
    destination = None

    # generate state space from pw object
    gameState = generateInitState(pw)
    PlanetWars.log("Amount of planets: %i" % len(gameState.getPlanets()))
    # PlanetWars.log("Started run")
    # PlanetWars.log(formatState(gameState))

    gameTree = TreeNode(gameState, None)

    #benchmark
    start = time.clock()
    pr = cProfile.Profile()
    pr.enable()

    (value, move) = alphaBeta(gameTree, 3, float('-inf'), float('inf'), True)

    source = pw.get_planet(move[0])
    destination = pw.get_planet(move[1])
    pr.disable()

    end = time.clock()

    PlanetWars.log("Action took %f sec" % (end - start))
    PlanetWars.log("Selected action %s" % str(move))

    ## DEBUG: Dump profiling results to file
    sortby = "cumulative"
    s = StringIO.StringIO()
    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    ps.print_stats()
    # PlanetWars.log(s.getvalue())

    # (3) Attack/Defend
    # If the source and destination variables contain actual planets, then
    # send half of the ships from source to destination.
    if source is not None and destination is not None:
        pw.issue_order(source, destination)
Example #7
0
    def __init__(self, original_pw):

        self._planets = []
        """:type : list[Planet]"""

        PlanetWars.__init__(self, clone=original_pw)
Example #8
0
def main():
    while True:
        pw = PlanetWars()
        do_turn(pw)
        pw.finish_turn()
    def __init__(self, original_pw):

        self._planets = []
        """:type : list[Planet]"""

        PlanetWars.__init__(self, clone=original_pw)
Example #10
0
def main():
    while True:
        pw = PlanetWars()
        do_turn(pw)
        pw.finish_turn()