Coding a Simple Number Sense Dice Game

Document created by teachontario.team on Jun 25, 2019Last modified by lisafloyd on Jun 25, 2019
Version 5Show Document
  • View in full screen mode

stacked dice

 

By:  Lisa Anne Floyd

 

Probability and number sense are important strands explored in math classrooms and they provide us with a fun and engaging context to explore coding concepts. In this article, we share instructions to create a simple two-dice game. Although the guided materials are provided, it is important that students are challenged to remix the program afterwards to add their own creative ideas. If time permits, it is also a good idea to encourage students to apply this skill-set to a new context by creating a different application that uses similar concepts. Some extensions and challenge programs have been suggested at the end of this article.

 

Here’s the completed program for you to try out.

 

 

Math concepts:

  • basic numeracy
  • probability

 

Computer programming concepts:

  • variables (data)
  • random numbers
  • selection/decision structures
  • comparison operators
  • arithmetic assignment operators

 

Here is a PDF version of the instructions for you to print or share with your students online.

 

 

Application Description:

 

In this game, the user clicks on the green flag to roll two dice. The user is then prompted to enter the sum of the two numbers. The application will indicate if the answer entered is correct.

 

Guided Materials:

 

1 -- Go to this link template to begin.

 

To begin, we will make the two dice display different random values.

 

2 -- Ensure the whiteDice sprite is selected.

 

image of selected whiteDice sprite

 

 

 

 

 

 

 

 

 

3 – Under Events, drag the When green flag is clicked block to the Code Editor window:

 

when clicked icon with little green flag

 

4 – Under Variables, select Make a Variable:

 

image of make a variable selected

 

 

5 – Name the variable dice1 and select OK.

 

new variable box being named

 

 

6 – Drag the set block (from Variables) to beneath the when green flag is clicked block:

 

the set block dragged beneath when green flag is clicked

 

7 – Under Looks, select the switch costume to block, dragging it beneath the set block.

 

Under Looks, select the switch costume to block, dragging it beneath the set block, image depicting this

 

8 – Under Operators, select the pick random block, placing it inside the set block and adjust the values: 1 to 6 (for the 6 numbers on a dice).

 

9 – Under Variables, select the dice1 block and place it inside of the switch costume block.

 

depicting: Under Variables, select the dice1 block and place it inside of the switch costume block.

 

10 – Let’s do the same thing for the redDice sprite.  Ensure the Red Dice sprite is selected.

 

Depicting: Let’s do the same thing for the redDice sprite.  Ensure the Red Dice sprite is selected.

 

11 - Under Events, drag the When green flag is clicked block to the Code Editor window:

 

12 – Under Data, select Make a Variable:

 

13 – Name the variable dice2 and select OK.

 

14 – Drag the set block (from Variables) to beneath the When green flag is clicked block:

 

15 – Under Looks, select the switch costume to block, dragging it beneath the set block.

 

16 – Under Operators, select the pick random block, placing it inside the set block and adjust the values: 1 to 6 (for the 6 numbers on a dice).

 

17 – Under Variables, select the dice2 block and place it inside of the switch costume block.

 

Depicting: Under Variables, select the dice2 block and place it inside of the switch costume block.

 

We will now write the code so that the numbers on the dice are added together and the user has a chance to enter their answer.  We will then write code to check if they’ve entered the correct answer or not.

 

18 –Ensure the Abby sprite is selected.

 

Depicting: Ensure the Abby sprite is selected.

 

19 – Under Events, drag the When green flag is clicked block to the Code Editor window:

 

Depicting: Under Events, drag the When green flag is clicked block to the Code Editor window:

 

20 – Under Sensing, drag the ask and wait block to beneath the first block.  Change the text to read “What is the sum of the dice?”

 

Depicting: Under Sensing, drag the ask and wait block to beneath the first block.  Change the text to read “What is the sum of the dice?”

 

21 – Under Variables, Make a New Variable, naming it:  user answer.

 

22 – Under Variables, Make a New Variable, naming it: sum.

 

23 – Under Variables, drag the Set block to beneath the ask block. Ensure the user answer variable is selected from the pull-down menu.

 

24 – Under Sensing, drag the answer block inside the set user answer block.

 

Depicting: Under Sensing, drag the answer block inside the set user answer block.

 

25 – Under Variables, drag the set block to the code editor window and ensure the sum variable is selected from the pull-down menu.

 

26 – Under Operators, drag the addition operator to inside of the set sum block.

 

27 – From Variables, select the dice1 and dice2 variable blocks, placing them inside of the addition operator.

 

Depicting: Under Sensing, drag the answer block inside the set user answer block.

 

28 – Under Control, select the If then else block and add it to your code.

 

Depicting: Under Control, select the If then else block and add it to your code.

 

29 – Under Operators, select an equals comparison operator, and place it inside of the if section.

 

30 – From Variables, select the sum and user answer variables and place them inside of the equals comparison operator.

 

Depicting: From Variables, select the sum and user answer variables and place them inside of the equals comparison operator.

 

31 – Under Looks, select the say for 2 seconds block, place it beneath then and change the text to “Correct”.

 

32 – Under Looks, select the say for 2 seconds block and place it beneath else.

 

33 – From Operators, select the join block and place it inside of the most recent say block.

 

34 – In the first part of the join block, enter the text:  “Incorrect. The correct answer is:"

 

35 – From Variables, drag the sum variable into the second part of the join block.

 

Depicting: From Variables, drag the sum variable into the second part of the join block.

 

Try out the program by clicking on the green flag. Does it work?  Did you have to debug?

 

Challenges and Extensions:

 

  1. Can you adjust the program so that the user is to determine the product rather than the sum of the two numbers?
  2. Can you add a third die (tricky)?
  3. Can you use a repeat block so that the user can continue entering an answer in until they get it correct?
  4. Can you adjust the program so that the user is told whether the answer they have entered is too low or too high?
  5. Can you animate the dice to look like they are rolling each time the green flag is clicked?
  6. Can you code a rock-paper-scissors game using similar code?
  7. Can you make a cash register application using similar code?

Attachments

    Outcomes