Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary IPython version #284

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
366 changes: 366 additions & 0 deletions Pelita.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,366 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pelita Notebook\n",
"Defining a player and running a Pelita game from inside IPython"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First: some basic imports"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pelita\n",
"from players import FoodEatingPlayer\n",
"from pelita.game_master import GameMaster\n",
"from pelita.player import AbstractPlayer, SimpleTeam"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The player which we define is the `SmartRandomPlayer`, which is also shown on the homepage. Nothing too fancy here…"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from pelita import datamodel\n",
"class SmartRandomPlayer(AbstractPlayer):\n",
" def get_move(self):\n",
" dangerous_enemy_pos = [bot.current_pos\n",
" for bot in self.enemy_bots if bot.is_destroyer]\n",
" killable_enemy_pos = [bot.current_pos\n",
" for bot in self.enemy_bots if bot.is_harvester]\n",
"\n",
" smart_moves = []\n",
" for move, new_pos in list(self.legal_moves.items()):\n",
" if (move == datamodel.stop or\n",
" new_pos in dangerous_enemy_pos):\n",
" continue # bad idea\n",
" elif (new_pos in killable_enemy_pos or\n",
" new_pos in self.enemy_food):\n",
" return move # get it!\n",
" else:\n",
" smart_moves.append(move)\n",
"\n",
" if smart_moves:\n",
" return self.rnd.choice(smart_moves)\n",
" else:\n",
" # we ran out of smart moves\n",
" return datamodel.stop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We define our"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"team = SimpleTeam(\"My winning team\", SmartRandomPlayer(), SmartRandomPlayer())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"… and the enemy’s team."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"enemy = SimpleTeam(\"Enemy team\", FoodEatingPlayer(), FoodEatingPlayer())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We set-up a maze"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"layout = \"\"\"\n",
"##################################\n",
"#... # .# # # 3#\n",
"# ## # # ### # # #####1#\n",
"#. # # # . # ## #\n",
"#.# # . # . # #########\n",
"# ## # ## #### # ##. . . .#\n",
"#.. . . #. . #. # # ## ##### #\n",
"# ## #### #.## # # . . . ..#\n",
"#.. .. # # # # ##### #####\n",
"##### ##### # # # # .. ..#\n",
"#.. . . . # # ##.# #### ## #\n",
"# ##### ## # # .# . .# . . ..#\n",
"#. . . .## # #### ## # ## #\n",
"######### # . # . # #.#\n",
"# ## # . # # # .#\n",
"#0##### # # ### # # ## #\n",
"#2 # # #. # ...#\n",
"##################################\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now it’s time for some interaction with IPython. Basically, we hijack IPython’s tornado application and use this for our own websockets and small file server."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pelita.ipython\n",
"from IPython.display import IFrame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Register our application:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Listening on 0.0.0.0, port 52588\n"
]
}
],
"source": [
"application, sockets = pelita.ipython.init_app()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This ipc socket is a subscriber sink for a Pelita zmq publisher socket:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'ipc:///tmp/pelita.4360207608'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"application.path"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We create a publisher which will act as a viewer for the Pelita process and send all data. (Every application should have only one publisher, otherwise it might not work.)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pelita.simplesetup import SimplePublisher"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"publisher = SimplePublisher(application.path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we’ll get the HTML output prepared in an iframe. This will accept data over a websocket and start displaying our little game once the data arrives."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This iframe is also available as: http://0.0.0.0:52588/static/index.html\n"
]
},
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"600\"\n",
" height=\"300\"\n",
" src=\"http://0.0.0.0:52588/static/index.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x104b905f8>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url = \"http://%s:%i/static/index.html\" % sockets[0].getsockname()\n",
"print(\"This iframe is also available as: {url}\".format(url=url))\n",
"IFrame(url, width=600, height=300)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we set up a game with the given layout and teams. Remember to always connect to the publisher before starting the game. Otherwise, there’ll be no output."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WebSocket opened\n"
]
}
],
"source": [
"gm = GameMaster(layout, [team, enemy], 4, 100)\n",
"gm.register_viewer(publisher)\n",
"gm.play()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The most likely outcome is, that we lose badly against the FoodEatingPlayers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading