Unit 4: Day 5
Tasks
44.1 Ex5 Number Guess Game
- This is an old classic example for working with boolean logic
- It adds a bit of complex code to build a multidigit number through the keyPressed function. This feature could be replaced by simply using window.prompt to ask the user for their guess.
- In the sample code below, a checkWinnner function (aka procedure) is used to clean up the draw function
Ex5 Number Guess Game
let compNum, userNum;
let userTemp ="";
let name;
function setup() {
let sketch = createCanvas(600, 400);
sketch.parent("mycanvas");
background(100,92,80);
compNum = floor( random(0,1000) ); //floor rounds down to a whole number
print(compNum);
userNum = -1;
name = window.prompt("What is your name friend?");
}//end setup
function draw() {
background(255,255,0);
textSize(24);
checkWinner();
}//end draw
function keyPressed() {
if( keyCode=== ENTER){
print("UserNumber: " + userTemp);
userNum = userTemp;
userTemp = "";
}
else if(keyCode === BACKSPACE){
userNum = -1;
compNum = floor ( random(0,1000 ) );
print(compNum);
}
else {
userTemp += key;
}
}//end keyPressed
function checkWinner(){
if(userNum === -1){
text("Guess a number between 0 and 1000", 50, 100);
} else if( userNum > compNum ){
text("Guess lower.", 50, 100);
} else if (userNum < compNum){
text("Guess higher.", 50, 100);
}
else {
textSize(40);
text(name + " you win!!!", 50, 100);
textSize(20);
text("Click anywhere \nto play again", 50, 150);
}
}//end checkWinner
44.2 P5 Assign 2.2 Move the Square
- Instructions for this assignment can be found in the folder: 1.4 - Year 1: Programming Part 2
- This assignment gets students thinking about game design
- It may be harder than it sounds.
- Reminder, if using the ASWD keys, the if statement is key===’a’
- If using arrow keys, the if statement is keyCode===LEFT_ARROW
- Start this assignment today and finish it tomorrow
44.3 Introduce the idea of Advanced Boolean Logic
- There are 3 parts of Advanced Boolean Logic:
- Using the more complicated operators AND , OR and NOT
- Using Boolean Variables
- Order of Prescedence of boolean operations
- Watch Code.org video: CS Principles: Conditionals - Part 3 "And & Or" Operators [4:08]
- Using Boolean variables can also be considered an Advanced Boolean Logic concept
Add Boolean Operators notes to the index files for AND, OR
- True && false true || false
- False && false false || true || false
- 1. isHuman && isMarvel
- 2. isHuman || isMarvel
- For THOR, which statement is true? (only #2 is true)
- For IRONMAN, which is true? (both are)
44.4 Boolean Quick Check #3 (or push back until next class if not done)
- A copy can be found in the Shared Activities Folder in 1.4 - Year 1: Programming Part 2
- Or you can see a student version of the quiz here.
- Reminder: Boolean Quick Checks can be used as a teaching tool, a practice or an actual marked assessment. There are 6 in all. I use 1, 3 and 5 as practice where students are welcome to ask each other and me questions. Then I use 2, 4 and 6 in turn as marked assessments. They get 1 free error each time and then I average the three marks as one.
Continue to Unit 4: Day 6 »