# Lesson 6: Sam the Bat

## Overview

Using Boolean operators, students will write code that checks the location of a sprite to make sure it doesn't go off-screen.

## Anchor Standard

### Common Core Math Standards

• 6.NS.8 - Solve real-world and mathematical problems by graphing points in all four quadrants of the coordinate plane. Include use of coordinates and absolute value to find distances between points with the same first coordinate or the same second coordinate.

## Objectives

### Students will be able to:

• Use Boolean operators to compare values.
• Apply Boolean logic, such as AND, OR, and NOT, to compose complex Boolean comparisons.

Heads Up! Please make a copy of any documents you plan to share with students.

# Teaching Guide

## Getting Started

### Introduction

Teaching Tip

It’s extremely valuable in this stage to have three students stand, and act out each of these three functions:

• Ask each student to tell you their Name, Domain and Range. If they get stuck, remind them that all of this information is written in their Contract!
• Practice calling each function, by saying their name and then giving them an x-coordinate. For example, "safe-left? fifty" means that the number 50 is being passed into `safe-left?`. That student should return "true", since the code currently returns `true` for all values of x.
• Do this for all three functions, and have the class practice calling them with different values as well.
```Note: the volunteer for `onscreen?` should first call `safe-left?`, before replying with the value.
```

This is Sam the Bat, and his mother tells him that he's free to play in the yard, but he don't set foot (or wing) outside the yard! Sam is safe as long as he is always entirely onscreen. The screen size is 400 pixels by 400 pixels, so how far can Sam go before he starts to leave the screen?

In this stage students write functions that will take in Sam the Bat's next x-coordinate and a return a boolean. That function should return true if part of Sam will still be visible, or false if he would go too far off-screen. If the function returns false, Sam isn't allowed to move.

Students will start by writing functions to check the left and right side of the screen independently, before combining those with a single `onscreen?` function that prevents Sam from leaving on both the left and right.

For each stage, make sure students try to get Sam to leave through the side they are checking. If Sam makes it all the way off-screen when he shouldn't, they'll get an error, but if he is successfully stopped they'll succeed and move to the next puzzle.

### Why not write just one function?

Some students may wonder why they should write separate functions for `safe-left?` and `safe-right?` when `onscreen?` could just check the dimensions of the screen directly. There is more to being a writer than good spelling and grammar. There’s more to being an architect or an artist than building a bridge or coloring in a canvas. All of these disciplines involved an element of design. Likewise, there is more to being a Programmer than just writing code.

Suppose you just built a car, but it’s not working right. What would you do? Ideally, you’d like to test each part of the car (the engine, the transmission, etc) one at a time, to see which one was broken. The same is true for code! If you have a bug, it’s much easier to find when every function is simple and easy to test, and the only complex functions are just built out of simpler ones. In this example, you can test your `safe-left?` and `safe-right?` functions independently, before stitching them together into onscreen?.

Another reason to define multiple, simple functions is the fact that it lets programmers be lazy. Suppose you have a few characters in a videogame, all of which need to be kept on the screen. Some of them might only need `safe-left?`, others might only need `safe-right?`, and only a few might need `onscreen?`. What happens if the game suddenly needs to run on computers with differently-sized monitors, where the width is 1000 pixels instead of 400? If you have simple and complex functions spread throughout your code, you’ll need to change them all. If your complex functions just use the simpler ones, you’d only need to change them in one place!

Badly designed programs can work just fine, but they are hard to read, hard to test, and easy to screw up if things change. As you grow and develop as a programmer, you’ll need to think beyond just "making code work". It’s not good enough if it just works - as artists, we should care about whether or not code is well designed, too. This is what functions allow us to do! Everyone from programmers to mathematicians uses functions to carve up complex problems into simpler pieces, which make it possible to design elegant solutions to difficult problems.

## Activity

### Online Puzzles

Using Boolean logic, you're going to write functions to help make sure Sam the Bat doesn't leave his mom's yard. Head to Course B stage 6 in Code Studio to get started programming.

## Extension Activities

### Safe up and down

The final puzzle of this stage is a Free Play puzzle that will allow you amd your students to experiment with other ways to keep Sam in his yard. The basic activity only prevents Sam from leaving on the left and right, but what about the top and bottom of the screen?

If you add a second variable to the `onscreen?` function to take in Sam's y coordinate, then you can check Sam's position on each axis. As students pursue this extension, encourage them to think about how they wrote small component functions to check the left and right. Could you follow a similiar approach to deal with the top and bottom?

View on Code Studio

### Student Instructions

View on Code Studio

### Student Instructions

Sam's mom wants to make sure that she can always see at least a bit of him, so lets write some functions that help make sure he doesn't go too far out of the yard. Here's a function called `safe-left?` - it should take a Number for its domain that represents Sam's x-coordinates and return true if he's completely visible on screen. If your function works, Sam shouldn't be able to leave the yard from the left side.

View on Code Studio

### Student Instructions

In the last puzzle we kept Sam from leaving on the left, now let's do the same thing on the right. Complete the `safe-right?` function to make sure Sam can't go off the right side of the screen.

View on Code Studio

### Student Instructions

Huh, we can keep Sam from leaving on the left, or we can keep him from leaving on the right, but how can we keep him safe from leaving both directions? Try using the `and` block to program the function `on-screen?` to check if Sam is `safe-left?` and `safe-right?`

View on Code Studio

### Student Instructions

Free Play: Awesome, we've kept Sam safe on the left and right! Use this free play stage to experiment with your functions and further improve `onscreen?`

## Standards Alignment

#### Common Core Math Standards

EE - Expressions And Equations
• 6.EE.9 - Use variables to represent two quantities in a real-world problem that change in relationship to one another; write an equation to express one quantity, thought of as the dependent variable, in terms of the other quantity, thought of as the independent va
• 7.EE.4 - Use variables to represent quantities in a real-world or mathematical problem, and construct simple equations and inequalities to solve problems by reasoning about the quantities.
F - Functions
• 8.F.1 - Understand that a function is a rule that assigns to each input exactly one output. The graph of a function is the set of ordered pairs consisting of an input and the corresponding output.1
• 8.F.2 - Compare properties of two functions each represented in a different way (algebraically, graphically, numerically in tables, or by verbal descriptions). For example, given a linear function represented by a table of values and a linear function represented
MP - Math Practices
• MP.1 - Make sense of problems and persevere in solving them
• MP.2 - Reason abstractly and quantitatively
• MP.3 - Construct viable arguments and critique the reasoning of others
• MP.4 - Model with mathematics
• MP.5 - Use appropriate tools strategically
• MP.6 - Attend to precision
• MP.7 - Look for and make use of structure
• MP.8 - Look for and express regularity in repeated reasoning
NS - The Number System
• 6.NS.8 - Solve real-world and mathematical problems by graphing points in all four quadrants of the coordinate plane. Include use of coordinates and absolute value to find distances between points with the same first coordinate or the same second coordinate.
OA - Operations And Algebraic Thinking
• 5.OA.1 - Use parentheses, brackets, or braces in numerical expressions, and evaluate expressions with these symbols.
• 5.OA.2 - Write simple expressions that record calculations with numbers, and interpret numerical expressions without evaluating them. For example, express the calculation “add 8 and 7, then multiply by 2” as 2 × (8 + 7). Recognize that 3 × (18932 + 921) is three t
Q - Quantities
• N.Q.1 - Use units as a way to understand problems and to guide the solution of multi-step problems; choose and interpret units consistently in formulas; choose and interpret the scale and the origin in graphs and data displays.
• N.Q.2 - Define appropriate quantities for the purpose of descriptive modeling.