Students begin a project—building a Tic Tac Toe game and other games that are played by placing marks (X or O, or a number) on a square array of tiles. This project will develop over the course of a few units. In this lab, students get as far as creating the grid, getting it automatically to fit the screen regardless of the number of tiles, and placing marks on the grid (but not yet with the computer checking to see if the moves are legal or if a player has won). In doing this, students continue to use ideas (and new levels) of abstraction, and learn also how to use a variety of math reporters—four basic operations (+
, -
, ×
, ÷
), and mod
—to create new reporter blocks, functions to perform specialized calculations for them.
The 2 lab pages could take about 1-2 days (40-80 minutes). Expected times to complete follow:
round
block, the mod
block, the random
block, and logical operators.move
block.mod
block.round
block.hide
block.sum
, to tally the sum of the odd numbers from 1 to 9. Discuss why the for
loop was given the inputs 1 and 5, and how the nested green Operations blocks support the goal of the task. This activity previews Unit 2 Lab 3: Developing more complex programs, Input Types.random
block do? How does it work?mod
block do? How does it work?mod
block?or
and and
?not
block do?Draw Triangle
that takes side Length as input and uses iteration to make the sprite make a point-up equilateral triangle with the specified sideLength.Row of Triangles
that takes triangles and sideLength as input and uses iteration to make a row of the specified number of triangles, as shown in the image below, with the specified sideLength. This procedure must use the Draw Triangle
block.Pyramid of Triangles
that takes triangles and sideLength as input and uses iteration with the for
block to create a pyramid with the specified number of triangles (that each have the specified sideLength) along the base, as shown in the image below. You must use the Row of Triangles
block.draw tile
block that could be modified later.repeat (4) [move (100), turn (90)]
" puts the sprite back in its original position—the script is state invariant. Building state invariance into scripts and blocks is a good idea, because it makes "ending where you stated" makes it easier to problematically move the sprite to the next state.is and
\frac{4}{7} + (3 \times 2)is
round (3.5)
reports 4.Mathematics Note: Mod
. The mod
block returns the remainder when the first input is divided by the second.
For positive whole numbers, this is the same as the notion of remainder that students learn in elementary school when they learn to divide one whole number by another. So:
because when 7 is divided by 5, the remainder is 2 (and the quotient is 1).
The behavior of the mod
block may seem mysterious when one or both of the inputs are negative integers. For example:
The sign of the output of mod
agrees with the sign of its second input. But why?
Let's go back to positive whole numbers for a moment. The fact that mod (7, 5) = 2
comes from 7 \div 5 = 1 \text{ remainder } 2, which comes from 7 = 5 \times 1 + 2 where 1 is the quotient and 2 is the remainder. The quotient is the key to the mystery.
In Snap!, you can get the quotient by dividing 7 by 5 to get 1.4 and then rounding down. This operation of rounding down is called floor
, and it's built into Snap! (you can find it as a drop-down option in the sqrt
block in the Operators palette). For any real number, floor
takes the closest integer to the left on the number line (hence the name "floor").
So, given any two integers a and b (or real numbers, really) you can calculate the quotient \frac{b}{a} and then the floor of their quotient \text{floor}(\frac{b}{a}). Let's call this quotient q. Then, b will equal aq plus the little left over that we lost by using floor (the remainder, which we'll call r). So now, b = aq + r much like 7 = 5 \times 1 + 2.
That remainder is what Snap! and most programming languages report for mod(b, a)
and this explains the signs of the outputs when one or both inputs are negative.
For example:
, so
and -7=5\times(-2)+3, so
.
Another example:
, so
and -7=-5\times(1)+-2, so
.