<kaggle_start><code># # Lux AI Season 1 Python Tutorial Notebook
# Welcome to Lux AI Season 1! We're glad you could make it! (TODO: add lore here?)
# This notebook takes you step by step on how to develop and compete using **Jupyter Notebooks and Python**. First things first, make sure you have these links at the ready
# - Competition Page: https://www.kaggle.com/c/lux-ai-2021/
# - Online Visualizer: https://2021vis.lux-ai.org/
# - Specifications: https://www.lux-ai.org/specs-2021
# - Github: https://github.com/Lux-AI-Challenge/Lux-Design-2021
# - Bot API: https://github.com/Lux-AI-Challenge/Lux-Design-2021/tree/master/kits
# And if you haven't done so already, we **highly recommend** you join our Discord server at https://discord.gg/aWJt3UAcgn or at the minimum follow the kaggle forums at https://www.kaggle.com/c/lux-ai-2021/discussion. We post important announcements there such as changes to rules, events, and opportunities from our sponsors!
# Now let's get started!
# ## Prerequisites
# We assume that you have a basic knowledge of Python and programming. It's okay if you don't know the game specifications yet! Feel free to always refer back to https://www.lux-ai.org/specs-2021.
# ## Basic Setup
# First thing to verify is that you have **Node.js v12 or above**. The engine for the competition runs on Node.js (for many good reasons including an awesome visualizer) and thus it is required. You can download it [here](https://nodejs.org/en/download/). You can then verify you have the appropriate version by running
#

# Next, we have to import the `make` function from the `kaggle_environments` package
from kaggle_environments import make

# The `make` function is used to create environments that can then run the game given agents. Agents refer to programmed bots that play the game given observations of the game itself.
# In addition to making the environment, you may also pass in special configurations such as the number of episode steps (capped at 361) and the seed.
# Now lets create our environment using `make` and watch a Episode! (We will be using the seed 562124210 because it's fun)
# create the environment
env = make("lux_ai_2021", configuration={"seed": 562124210})
# run a match between two simple agents, which are the agents we will walk you through on how to build!
steps = env.run(["simple_agent", "simple_agent"])
# render the game, feel free to change width and height to your liking. We recommend keeping them as large as possible for better quality.
env.render(mode="ipython", width=1200, height=800)

# Ok so woah, what just happened? We just ran a match, that's what :)
# There's a number of quality of life features in the visualizer, which you can also find embedded on the kaggle competition page when watching replays or on the online visualizer when using replay files.
# If you find this replay viewer slow, you can also download a local copy of this replay viewer in addition to lowering the graphics quality, see https://github.com/Lux-AI-Challenge/LuxViewer2021 for instructions.
# At this point, we recommend reading the [game specifications](https://www.lux-ai.org/specs-2021) a bit more to understand how to build a bot that tries to win the game.
# ## Building from Scratch
# The following bit of code is all you need for a empty agent that does nothing
# run this if using kaggle notebooks
from lux.game import Game
from lux.game_map import Cell, RESOURCE_TYPES
from lux.constants import Constants
from lux.game_constants import GAME_CONSTANTS
from lux import annotate
import math

# we declare this global game_state object so that state persists across turns so we do not need to reinitialize it all the time
game_state = None


def agent(observation, configuration):
    global game_state
    ### Do not edit ###
    if observation["step"] == 0:
        game_state = Game()
        game_state._initialize(observation["updates"])
        game_state._update(observation["updates"][2:])
    else:
        game_state._update(observation["updates"])
    actions = []
    ### AI Code goes down here! ###
    player = game_state.players[observation.player]
    opponent = game_state.players[(observation.player + 1) % 2]
    width, height = game_state.map.width, game_state.map.height
    return actions


# Unfortunately it's not that easy. This agent will eventually lose and all units and cities will fall to darkness! Let's write something to help the agent first find resources and then collect them.
def find_resources(game_state):
    # TODO
    pass


# Ok now that our agent finds and collects resources, what now? Well units can only carry so much resources before they can't collect anymore. And to keep your City alive, you must move your unit on top of any CityTile that is in that City. (Recall that a City is composed of connected CityTiles)
def return_to_nearest_city_tile(game_state):
    # TODO
    pass
<|endoftext|>
[{"Id": 70917348, "ScriptId": 19252428, "ParentScriptVersionId": NaN, "ScriptLanguageId": 9, "AuthorUserId": 3353266, "CreationDate": "08/09/2021 19:33:13", "VersionNumber": 2.0, "Title": "Lux AI Season 1 Jupyter Notebook Tutorial", "EvaluationDate": "08/09/2021", "IsChange": true, "TotalLines": 106.0, "LinesInsertedFromPrevious": 2.0, "LinesChangedFromPrevious": 0.0, "LinesUnchangedFromPrevious": 104.0, "LinesInsertedFromFork": NaN, "LinesDeletedFromFork": NaN, "LinesChangedFromFork": NaN, "LinesUnchangedFromFork": NaN, "TotalVotes": 1}]
# # Lux AI Season 1 Python Tutorial Notebook
# Welcome to Lux AI Season 1! We're glad you could make it! (TODO: add lore here?)
# This notebook takes you step by step on how to develop and compete using **Jupyter Notebooks and Python**. First things first, make sure you have these links at the ready
# - Competition Page: https://www.kaggle.com/c/lux-ai-2021/
# - Online Visualizer: https://2021vis.lux-ai.org/
# - Specifications: https://www.lux-ai.org/specs-2021
# - Github: https://github.com/Lux-AI-Challenge/Lux-Design-2021
# - Bot API: https://github.com/Lux-AI-Challenge/Lux-Design-2021/tree/master/kits
# And if you haven't done so already, we **highly recommend** you join our Discord server at https://discord.gg/aWJt3UAcgn or at the minimum follow the kaggle forums at https://www.kaggle.com/c/lux-ai-2021/discussion. We post important announcements there such as changes to rules, events, and opportunities from our sponsors!
# Now let's get started!
# ## Prerequisites
# We assume that you have a basic knowledge of Python and programming. It's okay if you don't know the game specifications yet! Feel free to always refer back to https://www.lux-ai.org/specs-2021.
# ## Basic Setup
# First thing to verify is that you have **Node.js v12 or above**. The engine for the competition runs on Node.js (for many good reasons including an awesome visualizer) and thus it is required. You can download it [here](https://nodejs.org/en/download/). You can then verify you have the appropriate version by running
#

# Next, we have to import the `make` function from the `kaggle_environments` package
from kaggle_environments import make

# The `make` function is used to create environments that can then run the game given agents. Agents refer to programmed bots that play the game given observations of the game itself.
# In addition to making the environment, you may also pass in special configurations such as the number of episode steps (capped at 361) and the seed.
# Now lets create our environment using `make` and watch a Episode! (We will be using the seed 562124210 because it's fun)
# create the environment
env = make("lux_ai_2021", configuration={"seed": 562124210})
# run a match between two simple agents, which are the agents we will walk you through on how to build!
steps = env.run(["simple_agent", "simple_agent"])
# render the game, feel free to change width and height to your liking. We recommend keeping them as large as possible for better quality.
env.render(mode="ipython", width=1200, height=800)

# Ok so woah, what just happened? We just ran a match, that's what :)
# There's a number of quality of life features in the visualizer, which you can also find embedded on the kaggle competition page when watching replays or on the online visualizer when using replay files.
# If you find this replay viewer slow, you can also download a local copy of this replay viewer in addition to lowering the graphics quality, see https://github.com/Lux-AI-Challenge/LuxViewer2021 for instructions.
# At this point, we recommend reading the [game specifications](https://www.lux-ai.org/specs-2021) a bit more to understand how to build a bot that tries to win the game.
# ## Building from Scratch
# The following bit of code is all you need for a empty agent that does nothing
# run this if using kaggle notebooks
from lux.game import Game
from lux.game_map import Cell, RESOURCE_TYPES
from lux.constants import Constants
from lux.game_constants import GAME_CONSTANTS
from lux import annotate
import math

# we declare this global game_state object so that state persists across turns so we do not need to reinitialize it all the time
game_state = None


def agent(observation, configuration):
    global game_state
    ### Do not edit ###
    if observation["step"] == 0:
        game_state = Game()
        game_state._initialize(observation["updates"])
        game_state._update(observation["updates"][2:])
    else:
        game_state._update(observation["updates"])
    actions = []
    ### AI Code goes down here! ###
    player = game_state.players[observation.player]
    opponent = game_state.players[(observation.player + 1) % 2]
    width, height = game_state.map.width, game_state.map.height
    return actions


# Unfortunately it's not that easy. This agent will eventually lose and all units and cities will fall to darkness! Let's write something to help the agent first find resources and then collect them.
def find_resources(game_state):
    # TODO
    pass


# Ok now that our agent finds and collects resources, what now? Well units can only carry so much resources before they can't collect anymore. And to keep your City alive, you must move your unit on top of any CityTile that is in that City. (Recall that a City is composed of connected CityTiles)
def return_to_nearest_city_tile(game_state):
    # TODO
    pass
