<jupyter_start><jupyter_text>![Callysto.ca Banner](https://github.com/callysto/curriculum-notebooks/blob/master/callysto-notebook-banner-top.jpg?raw=true) Playing with Dice Alice and BobAlice and Bob start with ten dollars each. They each roll a die, and whoever gets the bigger number takes one dollar from the other. Ties go to Alice. Repeat ten times: whoever ends up with the most money wins the round.   Let's PlayWe will play on the computer, since we can't share in the era of Covid-19. Click the "Roll the dice" button below to roll the dice, see who wins. Repeat ten times.Click the "Reset" button to start again with ten dollars each.<jupyter_code>import random
def roll_dice_two_players():
    major_die = random.choice([1,2,3,4,5,6])
    minor_die = random.choice([1,2,3,4,5,6])
    
    print("Alice rolls ",major_die, "and Bob rolls", minor_die,".")
    return (major_die >= minor_die)

from ipywidgets import widgets,Layout,Button,VBox,HBox
from IPython.display import display, Javascript, Markdown, HTML, clear_output
import pandas as pd

## We store the game results in a data frame, for convenience

df = pd.DataFrame({"Alice's Points": [ 0 for i in range(11)],
                  "Bob's Points": [ 0 for i in range(11)]},
                  index=[i for i in range(11)])
df.loc[0, "Alice's Points"] = 10
df.loc[0, "Bob's Points"] = 10

## We make a couple of buttons to roll the dice, and reset the game
style = {'description_width': 'initial'}

# Button widget
play_button = widgets.Button(
    button_style='success',
    description="Roll the dice", 
    layout=Layout(width='15%', height='30px'),
    style=style
) 

# Button widget
reset_button = widgets.Button(
    button_style='danger',
    description="Reset the game", 
    layout=Layout(width='15%', height='30px'),
    style=style
) 

turn_n = 0

def play_action(b):
    global turn_n
    clear_output()
    display(tab2)
    turn_n += 1
    if (turn_n>10):
        print("Game over! Who won this round?")
        display(df[0:11])
    else:
        print("Turn #",turn_n)
        if roll_dice_two_players():
            print("Alice wins this roll. She gets one point from Bob.")
            df.loc[turn_n, "Alice's Points"] = df.loc[turn_n-1,"Alice's Points"] +1
            df.loc[turn_n, "Bob's Points"] = df.loc[turn_n-1,"Bob's Points"]-1 
        else:
            print("Bob wins this roll. He gets one point from Alice.")
            df.loc[turn_n, "Alice's Points"] = df.loc[turn_n-1,"Alice's Points"] -1
            df.loc[turn_n, "Bob's Points"] = df.loc[turn_n-1,"Bob's Points"]+1 
        display(df[0:turn_n+1])
  
def reset_action(b):
    global turn_n
    clear_output()
    display(tab2)
    print("Alice and Bob start with ten points each.")
    display(df[0:1])
    turn_n=0
    
    
play_button.on_click( play_action )
reset_button.on_click( reset_action )

tab1 = HBox(children=[play_button,reset_button])
tab2 = widgets.Tab(children=[tab1])
tab2.set_title(0, 'Play')
display(tab2)
print("Alice and Bob start with ten points each.")
display(df[0:1])

# Connect widget to function - run subsequent cells<jupyter_output><empty_output><jupyter_text>Let's Try Again....Bob is losing the ties! So let's make things more fair, give him two dollars when he wins.Try this weighted game instead.  Who wins?<jupyter_code>## re-use the same data frame
df.loc[0, "Alice's Points"] = 10
df.loc[0, "Bob's Points"] = 10

## We make a couple of buttons to roll the dice, and reset the game
style = {'description_width': 'initial'}

# Button widget
play_button2 = widgets.Button(
    button_style='success',
    description="Roll the dice", 
    layout=Layout(width='15%', height='30px'),
    style=style
) 

# Button widget
reset_button2 = widgets.Button(
    button_style='danger',
    description="Reset the game", 
    layout=Layout(width='15%', height='30px'),
    style=style
) 

turn2 = 0

def play_action2(b):
    global turn2
    clear_output()
    display(tab4)
    turn2 += 1
    if (turn2>10):
        print("Game over! Who won this round?")
        display(df[0:11])
    else:
        print("Turn #",turn2)
        if roll_dice_two_players():
            print("Alice wins this roll. She gets one point from Bob.")
            df.loc[turn2, "Alice's Points"] = df.loc[turn2-1,"Alice's Points"] +1
            df.loc[turn2, "Bob's Points"] = df.loc[turn2-1,"Bob's Points"]-1 
        else:
            print("Bob wins this roll. He gets two points from Alice.")
            df.loc[turn2, "Alice's Points"] = df.loc[turn2-1,"Alice's Points"] -2
            df.loc[turn2, "Bob's Points"] = df.loc[turn2-1,"Bob's Points"]+2 
        display(df[0:turn2+1])
  
def reset_action2(b):
    global turn2
    clear_output()
    display(tab4)
    print("Alice and Bob start with ten points each.")
    display(df[0:1])
    turn2=0
    
    
play_button2.on_click( play_action2 )
reset_button2.on_click( reset_action2 )

tab3 = HBox(children=[play_button2,reset_button2])
tab4 = widgets.Tab(children=[tab3])
tab4.set_title(0, 'Play')
display(tab4)
print("Alice and Bob start with ten points each.")
display(df[0:1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1310196919477481, 'fraction_numerical': 0.026515890037044257, 'mean_word_length': 3.4608695652173913, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '196743', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1715, 'n_words': 495, 'path': 'unfair-dice-problem/online-dice-game.ipynb', 'repo_name': 'lfunderburk/Math-Modelling', 'size': '5129'}