TP 1 (en)

One Dice

Code the Dice class seen in TD 1 in a file named dice.py. It should be able to:

  • create a Dice with no parameter,
  • create a Dice with the initial position,
  • get and set the position of the Dice,
  • roll a Dice,
  • get the maximum number of face (same for all the dices, constant),
  • print it as a string.

From a python point of view, it must pass the following tests:

def test_dice_creation_no_argument():
    un_de = Dice()
    assert un_de.get_position() == 1


def test_dice_creation_initial_creation():
    un_de = Dice(initial_position=3)
    assert un_de.get_position() == 3


def test_str():
    un_de = Dice(2)
    assert str(un_de) == "Dice(2)"

    un_de.set_position(3)
    assert str(un_de) == "Dice(3)"


def test_dice_change_position():
    un_de = Dice()
    un_de.set_position(6)

    assert un_de.get_position() == 6


def test_roll():
    un_de = Dice()
    un_de.roll()

    assert 1 <= un_de.get_position() <= un_de.NUMBER_FACES

GUI

In a new file named gui_example.py copy/paste the following code.

from appJar import gui

app = gui()  # the GUI

# a label at row = 0 and column = 0
label_id = "dice_value"
app.addLabel(label_id, "0", 0, 0)
app.setLabelBg(label_id, "#BBAADD")  # https://color.adobe.com/


# callback function
def callback_on_click(button):
    current_text = app.getLabel(label_id)
    app.setLabel(label_id, str(int(current_text) + 1))

# a button at row = 0 and column = 1
button_id = "button_1"
app.addNamedButton("next integer", button_id, callback_on_click, 0, 1)


# launch GUI
app.go()

Note

We use the library appJar. To install it at home follow: http://appjar.info/Install/ Decompress the file appJar.zip in your project directory (you will have an appJar directory)

Note that when clicking on the button named "button_1" the function callback_on_click is executed. This is a general concept for User Interfaces (UI): function are executed in reaction to user actions (here, the click on a button).

One Dice

Modify the code in order that the label correspond to the position of a given Dice, this Dice being rolled when the user click on the button.

Several Dice

Model a class named GreenCarpet which has:
  • 5 dices as attributes,
  • a roll method which rolls the 5 dices,
  • a way to get the positions of each dices.

Use this new class to modify the UI in order roll 5 different dices with one button.

Some dices

Use the checkboxes widget (http://appjar.info/pythonWidgets/#checkbox) to only roll some dices (the checked one).