Skip to content

Commit

Permalink
use %pip install syntax for notebooks for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
HighDiceRoller committed Jun 15, 2024
1 parent 7efc9a3 commit ddf88b0
Show file tree
Hide file tree
Showing 36 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion notebooks/ability_scores/adnd.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\n\"\"\"\nThe @ operator means \"roll the left die, then roll that many of the right die and sum\".\nIntegers are treated as a die that always rolls that number.\nTherefore:\n* 3 @ icepool.d6 means 3d6.\n* icepool.d6 @ 3 means roll a d6 and multiply the result by 3.\n\"\"\"\n\nmethod1 = 6 @ icepool.d6.highest(4, keep=3)\n# Roll 12 ability scores and keep the highest 6.\nmethod2 = (3 @ icepool.d6).highest(12, 6)\n# keep defaults to 1.\nmethod3 = 6 @ (3 @ icepool.d6).highest(6)\nmethod4 = (6 @ (3 @ icepool.d6)).highest(12)",
"source": "%pip install icepool\n\nimport icepool\n\n\"\"\"\nThe @ operator means \"roll the left die, then roll that many of the right die and sum\".\nIntegers are treated as a die that always rolls that number.\nTherefore:\n* 3 @ icepool.d6 means 3d6.\n* icepool.d6 @ 3 means roll a d6 and multiply the result by 3.\n\"\"\"\n\nmethod1 = 6 @ icepool.d6.highest(4, keep=3)\n# Roll 12 ability scores and keep the highest 6.\nmethod2 = (3 @ icepool.d6).highest(12, 6)\n# keep defaults to 1.\nmethod3 = 6 @ (3 @ icepool.d6).highest(6)\nmethod4 = (6 @ (3 @ icepool.d6)).highest(12)",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/ability_scores/completely_unfair.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\none_ability = icepool.d6.highest(4, 3)",
"source": "%pip install icepool\n\nimport icepool\n\none_ability = icepool.d6.highest(4, 3)",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/ability_scores/one_set_strictly_better.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\nsingle_ability = icepool.d6.highest(4, 3)\n\nprint(single_ability)",
"source": "%pip install icepool\n\nimport icepool\n\nsingle_ability = icepool.d6.highest(4, 3)\n\nprint(single_ability)",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/ability_scores/rolling_for_stats_in_groups.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nfrom icepool import d\n\n# First, a single example.\n\n# 4d6, keep highest 3.\nability_score = d(6).highest(4, 3)\n\n# Roll 6 scores, take the highest.\nhighest_ability = ability_score.highest(6)\n\n# 4 party members.\nparty = highest_ability.pool(4)\n\n# Take the lowest and subtract it from the highest.\ndiff_metric = party[-1, ..., 1].sum()\nprint(diff_metric)\n",
"source": "%pip install icepool\n\nfrom icepool import d\n\n# First, a single example.\n\n# 4d6, keep highest 3.\nability_score = d(6).highest(4, 3)\n\n# Roll 6 scores, take the highest.\nhighest_ability = ability_score.highest(6)\n\n# 4 party members.\nparty = highest_ability.pool(4)\n\n# Take the lowest and subtract it from the highest.\ndiff_metric = party[-1, ..., 1].sum()\nprint(diff_metric)\n",
"metadata": {
"trusted": true
},
Expand Down
4 changes: 2 additions & 2 deletions notebooks/ability_scores/snake_draft.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\nimport matplotlib.pyplot as plt\n\ndef snake_draft_graph(num_players,\n player,\n ability_score=icepool.d6.highest(4, 3),\n reverses=(False, True, False, True, False, True)):\n \"\"\"\n Args:\n num_players: The number of players. Six times this many scores will be rolled.\n player: The player to graph.\n ability_score: A die representing the distribution of a single score.\n reverses: A 6-tuple representing whether the player order should be flipped\n for that round of drafting. In other words, each element determines\n whether a column of the result table will be flipped.\n The default performs a snake draft.\n \"\"\"\n total_rolls = 6 * num_players\n fig, ax = plt.subplots(figsize=(8, 4))\n for score_rank, reverse in enumerate(reverses):\n if reverse:\n index = (score_rank + 1) * num_players - 1 - player\n else:\n index = score_rank * num_players + player\n rank_die = ability_score.pool(total_rolls)[index]\n ax.plot(rank_die.outcomes(), rank_die.probabilities(percent=True))\n ax.grid(True)\n ax.set_xlim(3, 18)\n ax.set_xticks(range(3, 19))\n ax.set_ylim(0)\n ax.set_title(f'Player {player+1} of {num_players}')\n ax.set_xlabel('Score')\n ax.set_ylabel('Chance (%)')\n plt.show()\n\nsnake_draft_graph(4, 0)\nsnake_draft_graph(4, 3)\n ",
"source": "%pip install icepool\n\nimport icepool\nimport matplotlib.pyplot as plt\n\ndef snake_draft_graph(num_players,\n player,\n ability_score=icepool.d6.highest(4, 3),\n reverses=(False, True, False, True, False, True)):\n \"\"\"\n Args:\n num_players: The number of players. Six times this many scores will be rolled.\n player: The player to graph.\n ability_score: A die representing the distribution of a single score.\n reverses: A 6-tuple representing whether the player order should be flipped\n for that round of drafting. In other words, each element determines\n whether a column of the result table will be flipped.\n The default performs a snake draft.\n \"\"\"\n total_rolls = 6 * num_players\n fig, ax = plt.subplots(figsize=(8, 4))\n for score_rank, reverse in enumerate(reverses):\n if reverse:\n index = (score_rank + 1) * num_players - 1 - player\n else:\n index = score_rank * num_players + player\n rank_die = ability_score.pool(total_rolls)[index]\n ax.plot(rank_die.outcomes(), rank_die.probabilities(percent=True))\n ax.grid(True)\n ax.set_xlim(3, 18)\n ax.set_xticks(range(3, 19))\n ax.set_ylim(0)\n ax.set_title(f'Player {player+1} of {num_players}')\n ax.set_xlabel('Score')\n ax.set_ylabel('Chance (%)')\n plt.show()\n\nsnake_draft_graph(4, 0)\nsnake_draft_graph(4, 3)\n ",
"metadata": {
"trusted": true
},
Expand Down Expand Up @@ -59,7 +59,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\ndef snake_draft_table(num_players,\n ability_score=icepool.d6.highest(4, 3),\n reverses=(False, True, False, True, False, True)):\n \"\"\"\n Args:\n num_players: The number of players. Six times this many scores will be rolled.\n ability_score: A die representing the distribution of a single score.\n reverses: A 6-tuple representing whether the player order should be flipped\n for that round of drafting. In other words, each element determines\n whether a column of the result table will be flipped.\n The default performs a snake draft.\n \"\"\"\n total_rolls = 6 * num_players\n scores = [ability_score.pool(total_rolls)[i] for i in range(total_rolls - 1, -1, -1)]\n text = '| Player | 1st| 2nd| 3rd| 4th| 5th| 6th|\\n'\n text += '|-------:|------:|------:|------:|------:|------:|------:|\\n'\n for player in range(num_players):\n text += f'| {player+1} '\n for score_rank, reverse in enumerate(reverses):\n if reverse:\n index = (score_rank + 1) * num_players - 1 - player\n else:\n index = score_rank * num_players + player\n text += f'| {scores[index].mean():5.2f} '\n text += '|\\n'\n text += '\\n'\n print(text)\n\nfor num_players in [1, 2, 3, 4, 5, 6]:\n snake_draft_table(num_players)",
"source": "%pip install icepool\n\nimport icepool\n\ndef snake_draft_table(num_players,\n ability_score=icepool.d6.highest(4, 3),\n reverses=(False, True, False, True, False, True)):\n \"\"\"\n Args:\n num_players: The number of players. Six times this many scores will be rolled.\n ability_score: A die representing the distribution of a single score.\n reverses: A 6-tuple representing whether the player order should be flipped\n for that round of drafting. In other words, each element determines\n whether a column of the result table will be flipped.\n The default performs a snake draft.\n \"\"\"\n total_rolls = 6 * num_players\n scores = [ability_score.pool(total_rolls)[i] for i in range(total_rolls - 1, -1, -1)]\n text = '| Player | 1st| 2nd| 3rd| 4th| 5th| 6th|\\n'\n text += '|-------:|------:|------:|------:|------:|------:|------:|\\n'\n for player in range(num_players):\n text += f'| {player+1} '\n for score_rank, reverse in enumerate(reverses):\n if reverse:\n index = (score_rank + 1) * num_players - 1 - player\n else:\n index = score_rank * num_players + player\n text += f'| {scores[index].mean():5.2f} '\n text += '|\\n'\n text += '\\n'\n print(text)\n\nfor num_players in [1, 2, 3, 4, 5, 6]:\n snake_draft_table(num_players)",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/all_matching_sets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\nclass AllMatchingSets(icepool.MultisetEvaluator):\n def next_state(self, state, outcome, count):\n if state is None:\n state = ()\n # If at least a pair, append the size of the matching set.\n if count >= 2:\n state += (count,)\n # Prioritize larger sets.\n return tuple(sorted(state, reverse=True))\n\nall_matching_sets = AllMatchingSets()\n\n# Evaluate on 10d10.\nprint(all_matching_sets.evaluate(icepool.d10.pool(10)))",
"source": "%pip install icepool\n\nimport icepool\n\nclass AllMatchingSets(icepool.MultisetEvaluator):\n def next_state(self, state, outcome, count):\n if state is None:\n state = ()\n # If at least a pair, append the size of the matching set.\n if count >= 2:\n state += (count,)\n # Prioritize larger sets.\n return tuple(sorted(state, reverse=True))\n\nall_matching_sets = AllMatchingSets()\n\n# Evaluate on 10d10.\nprint(all_matching_sets.evaluate(icepool.d10.pool(10)))",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/bitd.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\n# A single die. Count the number of 6s and 4+s.\nbitd = icepool.d6.map(lambda x: icepool.vectorize(x == 6, x >= 4))\n\n# Interpret the number of dice that rolled the above.\ndef count_bitd(outcome):\n sixes, four_pluses = outcome\n if sixes > 1:\n return '3. critical success'\n elif sixes == 1:\n return '2. full success'\n elif four_pluses >= 1:\n return '1. mixed success'\n else:\n return '0. bad outcome'\n\n# Roll 4 dice and interpret the result.\nprint((4 @ bitd).map(count_bitd))",
"source": "%pip install icepool\n\nimport icepool\n\n# A single die. Count the number of 6s and 4+s.\nbitd = icepool.d6.map(lambda x: icepool.vectorize(x == 6, x >= 4))\n\n# Interpret the number of dice that rolled the above.\ndef count_bitd(outcome):\n sixes, four_pluses = outcome\n if sixes > 1:\n return '3. critical success'\n elif sixes == 1:\n return '2. full success'\n elif four_pluses >= 1:\n return '1. mixed success'\n else:\n return '0. bad outcome'\n\n# Roll 4 dice and interpret the result.\nprint((4 @ bitd).map(count_bitd))",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/broken_compass.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\nfrom icepool import d6\nimport matplotlib.pyplot as plt\n\ndefault_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']\n \ndef score(counts):\n result = 0\n for count in counts:\n count = min(count, 6)\n if count >= 2:\n result += 3 ** (count - 2)\n return result\n\npool_sizes = [2, 3, 4, 5, 6, 7, 8, 9]\n\ndef plot_results(results, extra_results = None):\n figsize = (16, 9)\n fig, ax = plt.subplots(figsize=figsize)\n for result in results:\n result = result.clip(0, 81).set_range(0, 81)\n ax.semilogx(result.outcomes(), result.probabilities_ge(percent=True))\n if extra_results:\n for i, result in enumerate(extra_results):\n result = result.clip(0, 81).set_range(0, 81)\n ax.semilogx(result.outcomes(), result.probabilities_ge(percent=True),\n color=default_colors[i], linestyle='--', alpha=0.5)\n ax.legend([f'{pool_size} dice' for pool_size in pool_sizes])\n ax.set_xticks([1, 2, 3, 6, 9, 18, 27, 54, 81])\n ax.set_xticklabels(['Basic', '×2', 'Critical', '×2', 'Extreme', '×2', 'Impossible', '×2', 'What a Hero!'])\n ax.set_xticks(range(1, 91), minor=True)\n ax.set_xticklabels([], minor=True)\n ax.set_yticks(range(0, 101, 10))\n ax.set_xlim(1, 81)\n ax.set_ylim(0, 100)\n ax.set_ylabel('Chance of getting at least (%)')\n ax.grid()\n plt.show()\n\nresults = []\n\nfor pool_size in pool_sizes:\n initial = d6.pool(pool_size).all_counts(filter=0)\n result = initial.map(score)\n results.append(result)\n\nplot_results(results)\n",
"source": "%pip install icepool\n\nimport icepool\nfrom icepool import d6\nimport matplotlib.pyplot as plt\n\ndefault_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']\n \ndef score(counts):\n result = 0\n for count in counts:\n count = min(count, 6)\n if count >= 2:\n result += 3 ** (count - 2)\n return result\n\npool_sizes = [2, 3, 4, 5, 6, 7, 8, 9]\n\ndef plot_results(results, extra_results = None):\n figsize = (16, 9)\n fig, ax = plt.subplots(figsize=figsize)\n for result in results:\n result = result.clip(0, 81).set_range(0, 81)\n ax.semilogx(result.outcomes(), result.probabilities_ge(percent=True))\n if extra_results:\n for i, result in enumerate(extra_results):\n result = result.clip(0, 81).set_range(0, 81)\n ax.semilogx(result.outcomes(), result.probabilities_ge(percent=True),\n color=default_colors[i], linestyle='--', alpha=0.5)\n ax.legend([f'{pool_size} dice' for pool_size in pool_sizes])\n ax.set_xticks([1, 2, 3, 6, 9, 18, 27, 54, 81])\n ax.set_xticklabels(['Basic', '×2', 'Critical', '×2', 'Extreme', '×2', 'Impossible', '×2', 'What a Hero!'])\n ax.set_xticks(range(1, 91), minor=True)\n ax.set_xticklabels([], minor=True)\n ax.set_yticks(range(0, 101, 10))\n ax.set_xlim(1, 81)\n ax.set_ylim(0, 100)\n ax.set_ylabel('Chance of getting at least (%)')\n ax.grid()\n plt.show()\n\nresults = []\n\nfor pool_size in pool_sizes:\n initial = d6.pool(pool_size).all_counts(filter=0)\n result = initial.map(score)\n results.append(result)\n\nplot_results(results)\n",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/cortex_prime.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\n# First, let's define the Cortex dice.\n\ndef c(die_size):\n # Ones become zeros.\n return icepool.d(die_size).map({1: 0})\n\n# Here's a pool of 3d6, summing the highest 2.\n# Note that the dice to count are sorted in ascending order,\n# so we take the last two.\nicepool.Pool([c(6), c(6), c(6)])[-2:].sum()\n\n# Equivalently:\nprint(icepool.Pool([c(6), c(6), c(6)]).highest(2).sum())",
"source": "%pip install icepool\n\nimport icepool\n\n# First, let's define the Cortex dice.\n\ndef c(die_size):\n # Ones become zeros.\n return icepool.d(die_size).map({1: 0})\n\n# Here's a pool of 3d6, summing the highest 2.\n# Note that the dice to count are sorted in ascending order,\n# so we take the last two.\nicepool.Pool([c(6), c(6), c(6)])[-2:].sum()\n\n# Equivalently:\nprint(icepool.Pool([c(6), c(6), c(6)]).highest(2).sum())",
"metadata": {
"trusted": true
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/cthulhutech.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"icepool\")\n\nimport icepool\n\nclass CthulhuTechEval(icepool.MultisetEvaluator):\n def next_state(self, state, outcome, count):\n score, run = state or (0, 0)\n if count > 0:\n set_score = outcome * count\n run_score = 0\n run += 1\n if run >= 3:\n # This could be the triangular formula, but it's clearer this way.\n for i in range(run): run_score += (outcome - i)\n score = max(set_score, run_score, score)\n else:\n # No dice rolled this number, so the score remains the same.\n run = 0\n return score, run\n\n def final_outcome(self, final_state):\n # Return just the score.\n return final_state[0]\n \n # Outcomes should be seen in consecutive order.\n alignment = icepool.MultisetEvaluator.range_alignment\n\nimport matplotlib.pyplot as plt\n\ndefault_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']\nevaluator = CthulhuTechEval()\nfigsize = (16, 9)\nfig, ax = plt.subplots(figsize=figsize)\n\nfor num_dice in range(1, 11):\n pool = icepool.d10.pool(num_dice)\n result = evaluator.evaluate(pool)\n result, _, _ = icepool.align_range(result, 0, 100)\n ax.plot(result.outcomes(), result.probabilities_ge(percent=True))\n marker_size = 64 if num_dice < 10 else 128\n ax.scatter(result.median(), 50.0,\n marker=('$%d$' % num_dice),\n facecolor=default_colors[num_dice-1],\n s=marker_size)\n\nax.set_xticks(range(0, 61, 5))\nax.set_yticks(range(0, 101, 10))\nax.set_xlim(0, 60)\nax.set_ylim(0, 100)\nax.set_xlabel('Result')\nax.set_ylabel('Chance of getting at least (%)')\nax.grid()\nplt.show()",
"source": "%pip install icepool\n\nimport icepool\n\nclass CthulhuTechEval(icepool.MultisetEvaluator):\n def next_state(self, state, outcome, count):\n score, run = state or (0, 0)\n if count > 0:\n set_score = outcome * count\n run_score = 0\n run += 1\n if run >= 3:\n # This could be the triangular formula, but it's clearer this way.\n for i in range(run): run_score += (outcome - i)\n score = max(set_score, run_score, score)\n else:\n # No dice rolled this number, so the score remains the same.\n run = 0\n return score, run\n\n def final_outcome(self, final_state):\n # Return just the score.\n return final_state[0]\n \n # Outcomes should be seen in consecutive order.\n alignment = icepool.MultisetEvaluator.range_alignment\n\nimport matplotlib.pyplot as plt\n\ndefault_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']\nevaluator = CthulhuTechEval()\nfigsize = (16, 9)\nfig, ax = plt.subplots(figsize=figsize)\n\nfor num_dice in range(1, 11):\n pool = icepool.d10.pool(num_dice)\n result = evaluator.evaluate(pool)\n result, _, _ = icepool.align_range(result, 0, 100)\n ax.plot(result.outcomes(), result.probabilities_ge(percent=True))\n marker_size = 64 if num_dice < 10 else 128\n ax.scatter(result.median(), 50.0,\n marker=('$%d$' % num_dice),\n facecolor=default_colors[num_dice-1],\n s=marker_size)\n\nax.set_xticks(range(0, 61, 5))\nax.set_yticks(range(0, 101, 10))\nax.set_xlim(0, 60)\nax.set_ylim(0, 100)\nax.set_xlabel('Result')\nax.set_ylabel('Chance of getting at least (%)')\nax.grid()\nplt.show()",
"metadata": {
"trusted": true
},
Expand Down
Loading

0 comments on commit ddf88b0

Please sign in to comment.