Skip to content

Commit

Permalink
Fix more warnings and indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a authored Aug 22, 2024
1 parent 8c3900d commit 1d2dae1
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 306 deletions.
160 changes: 81 additions & 79 deletions dicegame-multiply.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -33,7 +33,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1d3afa58786f49e0af01ff9d8f26eff9",
"model_id": "c7a3d81dd9274bdea7cfe09e0777ce69",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -47,7 +47,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3220ba0c53214abf92c59c81b1173312",
"model_id": "c7daccf1e5bb4092994050b5240ac333",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -61,7 +61,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "feaf90afca7345f68990d0314d7347af",
"model_id": "3c94e270df16440f92feb478e5d28d2f",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -74,102 +74,104 @@
}
],
"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",
"\n",
"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"
]
}
],
Expand Down
150 changes: 76 additions & 74 deletions dicegame-multiply.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,98 @@
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()

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
players = num_players_slider.value
Loading

0 comments on commit 1d2dae1

Please sign in to comment.