Skip to content

Instantly share code, notes, and snippets.

@boarpig
Last active August 29, 2015 14:11
Show Gist options
  • Save boarpig/3866b94ddfdd102b3f37 to your computer and use it in GitHub Desktop.
Save boarpig/3866b94ddfdd102b3f37 to your computer and use it in GitHub Desktop.
Game simulation for a given raid group
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:cf13834389d6d67d3bfadd100f68cf3ecdc0ed2f4712db63409386327299439e"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Raid Simulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Context: I am trying to find the best way to deal with a difficult mechanic in a 40-person MMO raid that I am in.\n",
"There are 40 people split into 12 groups of 3 and one back-up group of 4.\n",
"There are 4 or 5 waves of attacks:\n",
"\n",
"1. 3 random people are marked for death\n",
"2. 4 random people are marked for death\n",
"3. 5 random people are marked for death\n",
"4. 6 random people are marked for death\n",
"5. 7 random people are marked for death.\n",
"\n",
"It is possible for the same person to be marked with death on multiple waves.\n",
"Each time someone is marked for death, someone must use an item to save them. Each person only has 1 of this item.\n",
"Someone can only save another member (you cannot save yourself) of their own 3-person group. If both other members of your group are marked, it is possible for the third person to use the item to save them both.\n",
"So, have 2 question, what is the % chance that upon the 4th wave that a group will be unable to save one of their members (or will have been unable on wave 3)? And for wave 5?\n",
"Willing to gild for a correct answer (or not if that is against the rules of the subreddit to offer)!\n",
"\n",
"P.S. Why the groups? Hard to explain, and i will if anyone happens to really want to know, but basically, it's not very possible to do other, more obvious ways - we've tried.\n",
"\n",
"\n",
"http://www.reddit.com/r/math/comments/2p4dqg/anyone_good_at_complicated_probability_problems/"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Create Player class, not very complicated"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Player:\n",
" \n",
" def __init__(self):\n",
" self.has_bottle = True\n",
" self.marked = False\n",
" self.mark_count = 0\n",
" self.dead = False\n",
" \n",
" def mark(self):\n",
" self.marked = True\n",
" self.mark_count += 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Create group class, a bit more complicated"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Group:\n",
" \n",
" def __init__(self):\n",
" self.players = []\n",
" \n",
" def add_player(self, player):\n",
" self.players.append(player)\n",
" \n",
" def heal_and_kill(self):\n",
" healed = 0\n",
" killed = 0\n",
" marked_players = set()\n",
" bottle_players = set()\n",
" \n",
" # Find all marked player and all players with a bottle\n",
" for player in self.players:\n",
" if player.marked:\n",
" marked_players.add(player)\n",
" if player.has_bottle:\n",
" bottle_players.add(player)\n",
" \n",
" # nothing to do since no-one marked\n",
" if not marked_players:\n",
" return healed, killed\n",
"\n",
" # get first non-marked player with a bottle\n",
" for healer in bottle_players:\n",
" if marked_players:\n",
" for player in tuple(marked_players):\n",
" if player != healer:\n",
" player.marked = False\n",
" marked_players.discard(player)\n",
" healed += 1\n",
" healer.has_bottle = False\n",
" # avoid the off chance that there are more than 2 players to heal\n",
" if healed == 2:\n",
" break\n",
" else:\n",
" break\n",
" \n",
" # kill all remaining player in the set \n",
" if marked_players:\n",
" killed = len(marked_players)\n",
" for p in reversed(self.players):\n",
" if p.marked:\n",
" self.players.remove(p)\n",
" p.dead = True\n",
" \n",
" return healed, killed"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"One game as a function "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def play():\n",
" from random import sample\n",
"\n",
" # Setup players in groups\n",
"\n",
" groups = []\n",
" players = []\n",
"\n",
" for i in range(13):\n",
" new_group = Group()\n",
"\n",
" for i in range(3):\n",
" player = Player()\n",
" players.append(player)\n",
" new_group.add_player(player)\n",
"\n",
" groups.append(new_group)\n",
"\n",
" # Add extra player for the last group\n",
" player = Player()\n",
" groups[-1].add_player(player)\n",
" players.append(player)\n",
"\n",
" wave_deaths = [0] * 5\n",
" players_left = [0] * 5\n",
" # And play the waves\n",
" for wave in range(5):\n",
" kills_in_wave = 0\n",
" marked = sample(players, wave + 2)\n",
" for player in marked:\n",
" player.mark()\n",
" for group in groups:\n",
" heals, kills = group.heal_and_kill()\n",
" kills_in_wave += kills\n",
" wave_deaths[wave] += kills_in_wave\n",
" players = [p for p in players if not p.dead]\n",
" players_left[wave] = len(players)\n",
" return wave_deaths, players_left"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 39
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"*One death is a tragedy; one million is a statistic.* -- Joseph Stalin"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"stats = [0] * 5\n",
"left_total = [0] * 5\n",
"rounds = 100000\n",
"for i in range(rounds):\n",
" result, left_game = play()\n",
" for i in range(5):\n",
" stats[i] += result[i]\n",
" left_total[i] += left_game[i]\n",
"\n",
"print(\"Chances that a player will die in given wave.\")\n",
"for wave in range(5):\n",
" print(\" Wave {}: {}\".format(wave + 1, stats[wave] / rounds))\n",
"print(\"Average players left per round:\")\n",
"for wave in range(5):\n",
" print(\" Wave {}: {}\".format(wave + 1, round(left_total[wave] / rounds)))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Chances that a player will die in given wave.\n",
" Wave 1: 0.0\n",
" Wave 2: 0.00237\n",
" Wave 3: 0.05162\n",
" Wave 4: 0.24292\n",
" Wave 5: 0.71145\n",
"Average players left per round:\n",
" Wave 1: 40\n",
" Wave 2: 40\n",
" Wave 3: 40\n",
" Wave 4: 40\n",
" Wave 5: 39\n"
]
}
],
"prompt_number": 54
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"The license (to avoid any trouble for those who want to use this)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" This is free and unencumbered software released into the public domain.\n",
"\n",
" Anyone is free to copy, modify, publish, use, compile, sell, or\n",
" distribute this software, either in source code form or as a compiled\n",
" binary, for any purpose, commercial or non-commercial, and by any\n",
" means.\n",
"\n",
" In jurisdictions that recognize copyright laws, the author or authors\n",
" of this software dedicate any and all copyright interest in the\n",
" software to the public domain. We make this dedication for the benefit\n",
" of the public at large and to the detriment of our heirs and\n",
" successors. We intend this dedication to be an overt act of\n",
" relinquishment in perpetuity of all present and future rights to this\n",
" software under copyright law.\n",
"\n",
" THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n",
" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n",
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n",
" IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR\n",
" OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\n",
" ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\n",
" OTHER DEALINGS IN THE SOFTWARE.\n",
"\n",
" For more information, please refer to <http://unlicense.org>"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment