diff --git a/dicegame-multiply.ipynb b/dicegame-multiply.ipynb index cc15825..40229a8 100644 --- a/dicegame-multiply.ipynb +++ b/dicegame-multiply.ipynb @@ -12,14 +12,14 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "267b81b2-8270-4197-a912-c790a116b12a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "ea7f77a765c04372b97bb8eb45fc5eb0", + "model_id": "764672f85fa54f439bbc7f2a32d037c2", "version_major": 2, "version_minor": 0 }, @@ -33,7 +33,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "1d3afa58786f49e0af01ff9d8f26eff9", + "model_id": "c7a3d81dd9274bdea7cfe09e0777ce69", "version_major": 2, "version_minor": 0 }, @@ -47,7 +47,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "3220ba0c53214abf92c59c81b1173312", + "model_id": "c7daccf1e5bb4092994050b5240ac333", "version_major": 2, "version_minor": 0 }, @@ -61,7 +61,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "feaf90afca7345f68990d0314d7347af", + "model_id": "3c94e270df16440f92feb478e5d28d2f", "version_major": 2, "version_minor": 0 }, @@ -74,82 +74,84 @@ } ], "source": [ - "import math\n", "import random\n", + "\n", "import ipywidgets as widgets\n", "from IPython.display import display, clear_output\n", "\n", + "\"\"\"Dice game with various score multipliers that multiply by 2 if criteria meets the requirements, else reset to 1.\"\"\"\n", + "\n", "def dice_game(num_rolls = 10, num_players = 2, highest_guess = 4, guess = 7):\n", - " scores = [0] * num_players\n", - " score_multiplier = 1\n", - " score_multiplier_for_doubles = 1\n", - " score_multiplier_for_sixes = 1\n", - " score_multiplier_for_highest_guess = 1\n", - " score_multiplier_for_sequential = 1\n", - " score_multiplier_for_close_dice = 1\n", - " score_multiplier_for_low_or_high = 1\n", - " for _ in range(num_rolls):\n", - " for player in range(num_players):\n", - " roll1 = random.randint(1, 6)\n", - " roll2 = random.randint(1, 6)\n", - " total_rolls = roll1 + roll2\n", - " double_roll = roll1 == roll2\n", - " if double_roll:\n", - " score_multiplier_for_doubles *= 2\n", - " else:\n", - " score_multiplier_for_doubles = 1\n", - " \n", - " if roll1 == 6 or roll2 == 6:\n", - " score_multiplier_for_sixes *= 2\n", - " else:\n", - " score_multiplier_for_sixes = 1\n", - " \n", - " if max(roll1, roll2) == highest_guess:\n", - " score_multiplier_for_highest_guess *= 2\n", - " else:\n", - " score_multiplier_for_highest_guess = 1\n", - " \n", - " if roll2 >= roll1:\n", - " score_multiplier_for_sequential *= 2\n", - " else:\n", - " score_multiplier_for_sequential = 1\n", - " \n", - " diff = abs(roll1 - roll2)\n", - " if diff == 1 or diff == 0:\n", - " score_multiplier_for_close_dice *= 2\n", - " else:\n", - " score_multiplier_for_close_dice = 1\n", - " \n", - " if total_rolls == guess:\n", - " score_multiplier *= 2\n", - " else:\n", - " score_multiplier = 1\n", - " \n", - " if total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12:\n", - " score_multiplier_for_low_or_high *= 2\n", - " else:\n", - " score_multiplier_for_low_or_high = 1\n", - " \n", - " score_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1\n", - " scores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high\n", + "\tscores = [0] * num_players\n", + "\tscore_multiplier = 1\n", + "\tscore_multiplier_for_doubles = 1\n", + "\tscore_multiplier_for_sixes = 1\n", + "\tscore_multiplier_for_highest_guess = 1\n", + "\tscore_multiplier_for_sequential = 1\n", + "\tscore_multiplier_for_close_dice = 1\n", + "\tscore_multiplier_for_low_or_high = 1\n", + "\tfor _ in range(num_rolls):\n", + "\t\tfor player in range(num_players):\n", + "\t\t\troll1 = random.randint(1, 6)\n", + "\t\t\troll2 = random.randint(1, 6)\n", + "\t\t\ttotal_rolls = roll1 + roll2\n", + "\t\t\tdouble_roll = roll1 == roll2\n", + "\t\t\tif double_roll:\n", + "\t\t\t\tscore_multiplier_for_doubles *= 2\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_doubles = 1\n", + "\t\t\t\n", + "\t\t\tif roll1 == 6 or roll2 == 6:\n", + "\t\t\t\tscore_multiplier_for_sixes *= 2\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_sixes = 1\n", + "\t\t\t\n", + "\t\t\tif max(roll1, roll2) == highest_guess:\n", + "\t\t\t\tscore_multiplier_for_highest_guess *= 2\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_highest_guess = 1\n", + "\t\t\t\n", + "\t\t\tif roll2 >= roll1:\n", + "\t\t\t\tscore_multiplier_for_sequential *= 2\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_sequential = 1\n", + "\t\t\t\n", + "\t\t\tdiff = abs(roll1 - roll2)\n", + "\t\t\tif diff == 1 or diff == 0:\n", + "\t\t\t\tscore_multiplier_for_close_dice *= 2\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_close_dice = 1\n", + "\t\t\t\n", + "\t\t\tif total_rolls == guess:\n", + "\t\t\t\tscore_multiplier *= 2\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier = 1\n", + "\t\t\t\n", + "\t\t\tif total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12:\n", + "\t\t\t\tscore_multiplier_for_low_or_high *= 2\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_low_or_high = 1\n", + "\t\t\t\n", + "\t\t\tscore_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1\n", + "\t\t\tscores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high\n", "\n", - " return scores\n", + "\treturn scores\n", "\n", "num_rolls_slider = widgets.IntSlider(\n", - " value=10,\n", - " min=1,\n", - " max=20,\n", - " step=1,\n", - " description='Rolls:',\n", - " continuous_update=False)\n", + "\tvalue=10,\n", + "\tmin=1,\n", + "\tmax=20,\n", + "\tstep=1,\n", + "\tdescription='Rolls:',\n", + "\tcontinuous_update=False)\n", "\n", "num_players_slider = widgets.IntSlider(\n", - " value=2,\n", - " min=1,\n", - " max=10,\n", - " step=1,\n", - " description='Players:',\n", - " continuous_update=False)\n", + "\tvalue=2,\n", + "\tmin=1,\n", + "\tmax=10,\n", + "\tstep=1,\n", + "\tdescription='Players:',\n", + "\tcontinuous_update=False)\n", "\n", "change_value = widgets.Button(description='Roll Dice')\n", "output = widgets.Output()\n", @@ -157,19 +159,19 @@ "display(num_rolls_slider, num_players_slider, change_value, output)\n", "\n", "def roll_dice(change):\n", - " with output:\n", - " clear_output()\n", - " global numRolls, numPlayers\n", - " scores = dice_game(num_rolls_slider.value, num_players_slider.value)\n", - " print(scores)\n", - " return scores\n", - " \n", + "\twith output:\n", + "\t\tclear_output()\n", + "\t\tglobal numRolls, numPlayers\n", + "\t\tscores = dice_game(num_rolls_slider.value, num_players_slider.value)\n", + "\t\tprint(scores)\n", + "\t\treturn scores\n", + "\t\t\n", "change_value.on_click(roll_dice)\n", "\n", "num_rolls_slider.observe(roll_dice, names='rolls')\n", "num_players_slider.observe(roll_dice, names='players')\n", "rolls = num_rolls_slider.value\n", - "players = num_players_slider.value" + "players = num_players_slider.value\n" ] } ], diff --git a/dicegame-multiply.py b/dicegame-multiply.py index 29ba90d..8a7daab 100644 --- a/dicegame-multiply.py +++ b/dicegame-multiply.py @@ -1,79 +1,81 @@ -import math import random + import ipywidgets as widgets from IPython.display import display, clear_output +"""Dice game with various score multipliers that multiply by 2 if criteria meets the requirements, else reset to 1.""" + def dice_game(num_rolls = 10, num_players = 2, highest_guess = 4, guess = 7): - scores = [0] * num_players - score_multiplier = 1 - score_multiplier_for_doubles = 1 - score_multiplier_for_sixes = 1 - score_multiplier_for_highest_guess = 1 - score_multiplier_for_sequential = 1 - score_multiplier_for_close_dice = 1 - score_multiplier_for_low_or_high = 1 - for _ in range(num_rolls): - for player in range(num_players): - roll1 = random.randint(1, 6) - roll2 = random.randint(1, 6) - total_rolls = roll1 + roll2 - double_roll = roll1 == roll2 - if double_roll: - score_multiplier_for_doubles *= 2 - else: - score_multiplier_for_doubles = 1 - - if roll1 == 6 or roll2 == 6: - score_multiplier_for_sixes *= 2 - else: - score_multiplier_for_sixes = 1 - - if max(roll1, roll2) == highest_guess: - score_multiplier_for_highest_guess *= 2 - else: - score_multiplier_for_highest_guess = 1 - - if roll2 >= roll1: - score_multiplier_for_sequential *= 2 - else: - score_multiplier_for_sequential = 1 - - diff = abs(roll1 - roll2) - if diff == 1 or diff == 0: - score_multiplier_for_close_dice *= 2 - else: - score_multiplier_for_close_dice = 1 - - if total_rolls == guess: - score_multiplier *= 2 - else: - score_multiplier = 1 - - if total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12: - score_multiplier_for_low_or_high *= 2 - else: - score_multiplier_for_low_or_high = 1 - - score_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1 - scores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high + scores = [0] * num_players + score_multiplier = 1 + score_multiplier_for_doubles = 1 + score_multiplier_for_sixes = 1 + score_multiplier_for_highest_guess = 1 + score_multiplier_for_sequential = 1 + score_multiplier_for_close_dice = 1 + score_multiplier_for_low_or_high = 1 + for _ in range(num_rolls): + for player in range(num_players): + roll1 = random.randint(1, 6) + roll2 = random.randint(1, 6) + total_rolls = roll1 + roll2 + double_roll = roll1 == roll2 + if double_roll: + score_multiplier_for_doubles *= 2 + else: + score_multiplier_for_doubles = 1 + + if roll1 == 6 or roll2 == 6: + score_multiplier_for_sixes *= 2 + else: + score_multiplier_for_sixes = 1 + + if max(roll1, roll2) == highest_guess: + score_multiplier_for_highest_guess *= 2 + else: + score_multiplier_for_highest_guess = 1 + + if roll2 >= roll1: + score_multiplier_for_sequential *= 2 + else: + score_multiplier_for_sequential = 1 + + diff = abs(roll1 - roll2) + if diff == 1 or diff == 0: + score_multiplier_for_close_dice *= 2 + else: + score_multiplier_for_close_dice = 1 + + if total_rolls == guess: + score_multiplier *= 2 + else: + score_multiplier = 1 + + if total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12: + score_multiplier_for_low_or_high *= 2 + else: + score_multiplier_for_low_or_high = 1 + + score_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1 + scores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high - return scores + return scores num_rolls_slider = widgets.IntSlider( - value=10, - min=1, - max=20, - step=1, - description='Rolls:', - continuous_update=False) + value=10, + min=1, + max=20, + step=1, + description='Rolls:', + continuous_update=False) num_players_slider = widgets.IntSlider( - value=2, - min=1, - max=10, - step=1, - description='Players:', - continuous_update=False) + value=2, + min=1, + max=10, + step=1, + description='Players:', + continuous_update=False) change_value = widgets.Button(description='Roll Dice') output = widgets.Output() @@ -81,16 +83,16 @@ def dice_game(num_rolls = 10, num_players = 2, highest_guess = 4, guess = 7): display(num_rolls_slider, num_players_slider, change_value, output) def roll_dice(change): - with output: - clear_output() - global numRolls, numPlayers - scores = dice_game(num_rolls_slider.value, num_players_slider.value) - print(scores) - return scores - + with output: + clear_output() + global numRolls, numPlayers + scores = dice_game(num_rolls_slider.value, num_players_slider.value) + print(scores) + return scores + change_value.on_click(roll_dice) num_rolls_slider.observe(roll_dice, names='rolls') num_players_slider.observe(roll_dice, names='players') rolls = num_rolls_slider.value -players = num_players_slider.value \ No newline at end of file +players = num_players_slider.value diff --git a/dicegame.ipynb b/dicegame.ipynb index 91d063b..67ca83b 100644 --- a/dicegame.ipynb +++ b/dicegame.ipynb @@ -12,14 +12,14 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "267b81b2-8270-4197-a912-c790a116b12a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "ad7e503432624e61a3494fe772470a79", + "model_id": "223f34ca620b47a9922c811cddf373ad", "version_major": 2, "version_minor": 0 }, @@ -33,7 +33,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "bbb8508b21b341098c10dcd2a856e6bc", + "model_id": "ec9907ed36594cc4a1a4c2aa6aab4d9f", "version_major": 2, "version_minor": 0 }, @@ -47,7 +47,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "a381957c0e6f4e648465de9fdfa77054", + "model_id": "e254a2b39b674ab398727d066efc0d50", "version_major": 2, "version_minor": 0 }, @@ -61,7 +61,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "6d386be7022349a8b51418da22376989", + "model_id": "83ad9f1c4c904805bd283e7c9076059c", "version_major": 2, "version_minor": 0 }, @@ -74,82 +74,84 @@ } ], "source": [ - "import math\n", "import random\n", + "\n", "import ipywidgets as widgets\n", "from IPython.display import display, clear_output\n", "\n", + "\"\"\"Dice game with various score multipliers that increase by 1 if criteria meets the requirements, else reset to 1.\"\"\"\n", + "\n", "def dice_game(num_rolls = 10, num_players = 2, highest_guess = 4, guess = 7):\n", - " scores = [0] * num_players\n", - " score_multiplier = 1\n", - " score_multiplier_for_doubles = 1\n", - " score_multiplier_for_sixes = 1\n", - " score_multiplier_for_highest_guess = 1\n", - " score_multiplier_for_sequential = 1\n", - " score_multiplier_for_close_dice = 1\n", - " score_multiplier_for_low_or_high = 1\n", - " for _ in range(num_rolls):\n", - " for player in range(num_players):\n", - " roll1 = random.randint(1, 6)\n", - " roll2 = random.randint(1, 6)\n", - " total_rolls = roll1 + roll2\n", - " double_roll = roll1 == roll2\n", - " if double_roll:\n", - " score_multiplier_for_doubles += 1\n", - " else:\n", - " score_multiplier_for_doubles = 1\n", - " \n", - " if roll1 == 6 or roll2 == 6:\n", - " score_multiplier_for_sixes += 1\n", - " else:\n", - " score_multiplier_for_sixes = 1\n", - " \n", - " if max(roll1, roll2) == highest_guess:\n", - " score_multiplier_for_highest_guess += 1\n", - " else:\n", - " score_multiplier_for_highest_guess = 1\n", - " \n", - " if roll2 >= roll1:\n", - " score_multiplier_for_sequential += 1\n", - " else:\n", - " score_multiplier_for_sequential = 1\n", - " \n", - " diff = abs(roll1 - roll2)\n", - " if diff == 1 or diff == 0:\n", - " score_multiplier_for_close_dice += 1\n", - " else:\n", - " score_multiplier_for_close_dice = 1\n", - " \n", - " if total_rolls == guess:\n", - " score_multiplier += 1\n", - " else:\n", - " score_multiplier = 1\n", - " \n", - " if total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12:\n", - " score_multiplier_for_low_or_high += 1\n", - " else:\n", - " score_multiplier_for_low_or_high = 1\n", - " \n", - " score_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1\n", - " scores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high\n", + "\tscores = [0] * num_players\n", + "\tscore_multiplier = 1\n", + "\tscore_multiplier_for_doubles = 1\n", + "\tscore_multiplier_for_sixes = 1\n", + "\tscore_multiplier_for_highest_guess = 1\n", + "\tscore_multiplier_for_sequential = 1\n", + "\tscore_multiplier_for_close_dice = 1\n", + "\tscore_multiplier_for_low_or_high = 1\n", + "\tfor _ in range(num_rolls):\n", + "\t\tfor player in range(num_players):\n", + "\t\t\troll1 = random.randint(1, 6)\n", + "\t\t\troll2 = random.randint(1, 6)\n", + "\t\t\ttotal_rolls = roll1 + roll2\n", + "\t\t\tdouble_roll = roll1 == roll2\n", + "\t\t\tif double_roll:\n", + "\t\t\t\tscore_multiplier_for_doubles += 1\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_doubles = 1\n", + "\t\t\t\n", + "\t\t\tif roll1 == 6 or roll2 == 6:\n", + "\t\t\t\tscore_multiplier_for_sixes += 1\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_sixes = 1\n", + "\t\t\t\n", + "\t\t\tif max(roll1, roll2) == highest_guess:\n", + "\t\t\t\tscore_multiplier_for_highest_guess += 1\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_highest_guess = 1\n", + "\t\t\t\n", + "\t\t\tif roll2 >= roll1:\n", + "\t\t\t\tscore_multiplier_for_sequential += 1\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_sequential = 1\n", + "\t\t\t\n", + "\t\t\tdiff = abs(roll1 - roll2)\n", + "\t\t\tif diff == 1 or diff == 0:\n", + "\t\t\t\tscore_multiplier_for_close_dice += 1\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_close_dice = 1\n", + "\t\t\t\n", + "\t\t\tif total_rolls == guess:\n", + "\t\t\t\tscore_multiplier += 1\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier = 1\n", + "\t\t\t\n", + "\t\t\tif total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12:\n", + "\t\t\t\tscore_multiplier_for_low_or_high += 1\n", + "\t\t\telse:\n", + "\t\t\t\tscore_multiplier_for_low_or_high = 1\n", + "\t\t\t\n", + "\t\t\tscore_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1\n", + "\t\t\tscores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high\n", "\n", - " return scores\n", + "\treturn scores\n", "\n", "num_rolls_slider = widgets.IntSlider(\n", - " value=10,\n", - " min=1,\n", - " max=20,\n", - " step=1,\n", - " description='Rolls:',\n", - " continuous_update=False)\n", + "\tvalue=10,\n", + "\tmin=1,\n", + "\tmax=20,\n", + "\tstep=1,\n", + "\tdescription='Rolls:',\n", + "\tcontinuous_update=False)\n", "\n", "num_players_slider = widgets.IntSlider(\n", - " value=2,\n", - " min=1,\n", - " max=10,\n", - " step=1,\n", - " description='Players:',\n", - " continuous_update=False)\n", + "\tvalue=2,\n", + "\tmin=1,\n", + "\tmax=10,\n", + "\tstep=1,\n", + "\tdescription='Players:',\n", + "\tcontinuous_update=False)\n", "\n", "change_value = widgets.Button(description='Roll Dice')\n", "output = widgets.Output()\n", @@ -157,19 +159,19 @@ "display(num_rolls_slider, num_players_slider, change_value, output)\n", "\n", "def roll_dice(change):\n", - " with output:\n", - " clear_output()\n", - " global numRolls, numPlayers\n", - " scores = dice_game(num_rolls_slider.value, num_players_slider.value)\n", - " print(scores)\n", - " return scores\n", - " \n", + "\twith output:\n", + "\t\tclear_output()\n", + "\t\tglobal numRolls, numPlayers\n", + "\t\tscores = dice_game(num_rolls_slider.value, num_players_slider.value)\n", + "\t\tprint(scores)\n", + "\t\treturn scores\n", + "\t\t\n", "change_value.on_click(roll_dice)\n", "\n", "num_rolls_slider.observe(roll_dice, names='rolls')\n", "num_players_slider.observe(roll_dice, names='players')\n", "rolls = num_rolls_slider.value\n", - "players = num_players_slider.value" + "players = num_players_slider.value\n" ] } ], diff --git a/dicegame.py b/dicegame.py index e487d33..c1b067c 100644 --- a/dicegame.py +++ b/dicegame.py @@ -1,79 +1,81 @@ -import math import random + import ipywidgets as widgets from IPython.display import display, clear_output +"""Dice game with various score multipliers that increase by 1 if criteria meets the requirements, else reset to 1.""" + def dice_game(num_rolls = 10, num_players = 2, highest_guess = 4, guess = 7): - scores = [0] * num_players - score_multiplier = 1 - score_multiplier_for_doubles = 1 - score_multiplier_for_sixes = 1 - score_multiplier_for_highest_guess = 1 - score_multiplier_for_sequential = 1 - score_multiplier_for_close_dice = 1 - score_multiplier_for_low_or_high = 1 - for _ in range(num_rolls): - for player in range(num_players): - roll1 = random.randint(1, 6) - roll2 = random.randint(1, 6) - total_rolls = roll1 + roll2 - double_roll = roll1 == roll2 - if double_roll: - score_multiplier_for_doubles += 1 - else: - score_multiplier_for_doubles = 1 - - if roll1 == 6 or roll2 == 6: - score_multiplier_for_sixes += 1 - else: - score_multiplier_for_sixes = 1 - - if max(roll1, roll2) == highest_guess: - score_multiplier_for_highest_guess += 1 - else: - score_multiplier_for_highest_guess = 1 - - if roll2 >= roll1: - score_multiplier_for_sequential += 1 - else: - score_multiplier_for_sequential = 1 - - diff = abs(roll1 - roll2) - if diff == 1 or diff == 0: - score_multiplier_for_close_dice += 1 - else: - score_multiplier_for_close_dice = 1 - - if total_rolls == guess: - score_multiplier += 1 - else: - score_multiplier = 1 - - if total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12: - score_multiplier_for_low_or_high += 1 - else: - score_multiplier_for_low_or_high = 1 - - score_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1 - scores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high + scores = [0] * num_players + score_multiplier = 1 + score_multiplier_for_doubles = 1 + score_multiplier_for_sixes = 1 + score_multiplier_for_highest_guess = 1 + score_multiplier_for_sequential = 1 + score_multiplier_for_close_dice = 1 + score_multiplier_for_low_or_high = 1 + for _ in range(num_rolls): + for player in range(num_players): + roll1 = random.randint(1, 6) + roll2 = random.randint(1, 6) + total_rolls = roll1 + roll2 + double_roll = roll1 == roll2 + if double_roll: + score_multiplier_for_doubles += 1 + else: + score_multiplier_for_doubles = 1 + + if roll1 == 6 or roll2 == 6: + score_multiplier_for_sixes += 1 + else: + score_multiplier_for_sixes = 1 + + if max(roll1, roll2) == highest_guess: + score_multiplier_for_highest_guess += 1 + else: + score_multiplier_for_highest_guess = 1 + + if roll2 >= roll1: + score_multiplier_for_sequential += 1 + else: + score_multiplier_for_sequential = 1 + + diff = abs(roll1 - roll2) + if diff == 1 or diff == 0: + score_multiplier_for_close_dice += 1 + else: + score_multiplier_for_close_dice = 1 + + if total_rolls == guess: + score_multiplier += 1 + else: + score_multiplier = 1 + + if total_rolls == 2 or total_rolls == 3 or total_rolls == 11 or total_rolls == 12: + score_multiplier_for_low_or_high += 1 + else: + score_multiplier_for_low_or_high = 1 + + score_multiplier_for_two_sixes = 2 if roll1 == 6 and roll2 == 6 else 1 + scores[player] += (roll1 + roll2) * score_multiplier * score_multiplier_for_doubles * score_multiplier_for_sixes * score_multiplier_for_two_sixes * score_multiplier_for_highest_guess * score_multiplier_for_sequential * score_multiplier_for_close_dice * score_multiplier_for_low_or_high - return scores + return scores num_rolls_slider = widgets.IntSlider( - value=10, - min=1, - max=20, - step=1, - description='Rolls:', - continuous_update=False) + value=10, + min=1, + max=20, + step=1, + description='Rolls:', + continuous_update=False) num_players_slider = widgets.IntSlider( - value=2, - min=1, - max=10, - step=1, - description='Players:', - continuous_update=False) + value=2, + min=1, + max=10, + step=1, + description='Players:', + continuous_update=False) change_value = widgets.Button(description='Roll Dice') output = widgets.Output() @@ -81,16 +83,16 @@ def dice_game(num_rolls = 10, num_players = 2, highest_guess = 4, guess = 7): display(num_rolls_slider, num_players_slider, change_value, output) def roll_dice(change): - with output: - clear_output() - global numRolls, numPlayers - scores = dice_game(num_rolls_slider.value, num_players_slider.value) - print(scores) - return scores - + with output: + clear_output() + global numRolls, numPlayers + scores = dice_game(num_rolls_slider.value, num_players_slider.value) + print(scores) + return scores + change_value.on_click(roll_dice) num_rolls_slider.observe(roll_dice, names='rolls') num_players_slider.observe(roll_dice, names='players') rolls = num_rolls_slider.value -players = num_players_slider.value \ No newline at end of file +players = num_players_slider.value