Mobile apps with App Inventor

Dice Roller

Build a dice app: tap Roll for a random number, and the app cheers when you roll a six.

Day 2 45 to 60 minutes 7 steps Key Stage 3
Start step 1
A six!

No more lost dice under the sofa. Today's app rolls a dice every time you tap a button, with a huge number you can read from across the table, and it cheers when you roll a six.

You will learn

  • Style components with colours, sizes and shapes
  • Set a label's text with a block
  • Use random numbers
  • Make a decision with an if then else block

You will need

  • A computer with a web browser
  • A phone or tablet with the MIT AI2 Companion app
  • Day 1, for the Designer and the Blocks editor

Words to know

random
Unpredictable: any of the numbers could come up.
setter
A dark green block that changes a property, such as set Label2.Text.
if then else
Runs one set of blocks if a test is true, and another if it is false.
mutator
The gear on some blocks, which adds parts such as else.

Step by step

  1. Step 1: A new project

    Start a new project called DiceRoller. Choose Screen1 and set its Title to Dice Roller, AlignHorizontal to Center and BackgroundColor to Blue.

    Screen1, set up, with a blue background.
  2. Step 2: Two labels

    Drag two Labels onto the phone. The first is the message: set its Text to Tap the button to roll, its FontSize to 22 and its TextColor to White. The second will show the number, so make it huge: Text ?, FontSize 120, tick FontBold and set TextColor to White too.

    A message, and a huge question mark for the number.
  3. Step 3: A roll button

    Drag a Button under the labels. Set its Text to Roll, FontSize to 24, Shape to rounded, BackgroundColor to Orange and Width to 150 pixels.

    A big orange Roll button.
  4. Step 4: Roll a number

    Go to Blocks. Build: when Button1.Click (from Button1), with set Label2.Text to (from Label2, dark green) inside it. From the Math drawer, drag random integer from 1 to 100 into the socket, then click the 100 and change it to 6.

    Click Math (1) and drag the random integer block (2).
    Each click sets the number to a random whole number from 1 to 6.
  5. Step 5: Roll it on your phone

    Connect the Companion and tap Roll a few times. Every tap shows a new number from 1 to 6.

    Tap Roll: a random number from 1 to 6.
  6. Step 6: Celebrate a six

    Make the app cheer when you roll a six. From Control, drag an if then block under the set block. Click its blue gear and drag an else into it. Then build the test from Math: the = block, with Label2.Text on one side and the number 6 on the other.

    If it is a six, set Label1's text to A six! You win!, and otherwise to Roll again. Text blocks are in the Text drawer.

    If the number is 6, cheer. Otherwise, roll again.
    Tip Blocks run from top to bottom, so the number is rolled before it is checked.
  7. Step 7: Roll for a six

    Try it on the phone. How many rolls does it take to get a six?

    A six!

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Two dice

    Roll two dice at once and show the total.

    Show a hint

    Add Label3 for the second dice. Then set Label1's text to a join of Total: and Label2.Text + Label3.Text, using the + block from Math.

  2. Shake to roll

    Roll when the phone is shaken, as well as when Roll is tapped.

    Show a hint

    Add an AccelerometerSensor from Sensors. Its when AccelerometerSensor1.Shaking event can hold the same blocks as the button.

  3. A different dice

    Make it a 20 sided dice, as some board games use.

    Show a hint

    Change the 6 in the random block to 20, and the test to 20.

Notes for the teacher

Timing

  1. 15 minutes: steps 1 to 3, styling the screen.
  2. 20 minutes: steps 4 and 5, the first blocks, and testing.
  3. 15 minutes: steps 6 and 7. Adding else with the gear (the mutator) is fiddly the first time: demonstrate it.

Mistakes to look out for

  1. set Label1.Text instead of set Label2.Text: the message changes instead of the number.
  2. The = block from Logic instead of Math: both work for numbers, but the Math one is the one in the picture.
  3. A number left as 100 in the random block.

Going further

Is it fair? Each student rolls 30 times and tallies. Pool the class results: each number should come up about one time in six.