Week 5 - Pre-Computing History and Simple Algorithms

Today’s Schedule

  • warm-up challenges
  • 1/3 semester review - variables, function, conditionals, loops
  • a little computer pre-history - Jacquard Looms
  • workshop: Programming a simple AI
  • student check-ins

Warm-up Challenges

3 challenges: Use a window size of 400 by 400 pixels. You can work solo or in a group.

  1. Code a random group of colored dots to appear all over the screen.
  2. Create exactly 30 dots to appear on screen.
  3. Have the dots move from the left side of the screen to the right, and they should get darker to light as they move across the screen.

Everything we’ve covered so far

  • variables
    • types (int, float, char, boolean)
    • random(min,max);
      • it is a float
    • casting from one variable type to another
  • conditionals
    • if statements
      • if, else if, else
      • logical operators (and, or, not)
  • loops
    • while loop
    • for loop
  • functions
    • custom function
    • arguments

A Little Computer Pre-History: Jacquard Looms

In the first week we learned about Quipu, a numeric recording system from the Andes in South America.

This week we jump far forward to the 1800s to look at an industrial weaving process, with the Jacquard Loom.


Review Bouncing Ball / Pong programs

In-class (and later homework) challenge: Simple AI

For this challenge, you will be coding a simple AI character that will chase a player.

The player is a creature in a hostile environment. It needs to collect apples in order to survive. There is an enemy creature that is trying to capture the player. It continually moves toward the player no matter what the player does. If it captures the player by moving into its space, it has vanquished the player and the game ends.

  1. Write the program functionality needed out in pseudocode. What are the order of operations/flow? Draw it as a flow chart if that would help.
  2. Start by creating a simple stand-in for a player, such as a simple shape with color. What variables do you need? Are they global or local? What type are these variables?
  3. Now do the same for the “enemy AI.” Choose a shape and color that will contrast with the player and create needed variables.
  4. Add interaction with mouse keys. When you hold down a key, move the player. Add Left-Right-Up-Down arrow keys. Make sure you code this so the player is able to continously move as long as a key is pressed down.
  5. Now have the enemy AI chase the player. If the player is to the left, move the enemy left. When the player is right, move the enemy right. When the player is above, move the enemy up. When player is below, move the enemy down. Alter the enemy’s speed so that the player has at least a bit of a chance to get away.
  6. When the enemy has reached the player, make the screen red. Game over.
  7. Replace your simple stand-in shapes for the player and enemy with more complex characters. You will need to have their body parts be relative to their X and Y location. For example, if the player has wings, the right wing may need to be 20 to the right of the playerX variable and the left wing may be drawn 20 to the left of playerX.
  8. Make sure the speed of the game makes sense. It should be playable, neither too easy nor impossible!

Advanced:

This section contains bonus goals. It is particularly encouraged for math/CS majors but everyone is welcome to try to accomplish these. Challenge yourself.

A. Add in a reset() function that runs when the player is captured. The game should start over.

B. Add in a bat. The bat moves around to an adjacent space randomly each time draw runs. If it touches the player, the bat drops the player at a random position on the screen (it could even be on top of the monster!) and then randomizes its own location and appears somewhere else on screen.

C. Add a goal, a green apple, randomly placed at start. Create a score variable. When the player reaches the green apple their score increases by 1. Then move its location and draw the apple somewhere else randomly on the level. And lastly, speed up the enemyAI by 1!

D. If the player is captured, use println to print out the final score and reset the score as the game begins again.

Read

Read Chapter 8 - Objects. This is new material.

Be sure to re-read any previous chapters if you are feeling uncertain about any of the material covered.

Watch

Punch Card Programming on ComputerPhile

Review Videos:

Functions Basics

Functions and re-usability

Intro to Functions and Objects - this video points to what we’ll be studying next: objects

While loops - also contains if (conditionals)

For-loops

Set up a check-in meeting with me for next week or following week

This is required of all students.

If we don’t get the chance to meet during class today, email me to set up a 1-on-1 10-15 minute check-in meeting during my office hours, next Monday or the following Monday. October 3 I will hold office hours online and October 10 can be online on Zoom or in-person. We will review your progress in-class as well as your current grades and I can answer questions you may have.

Coming up

Next week I will assign a midterm project. We will also have a midterm exam in a few weeks.