CS111 Evidence: Control Structures
Control Structures Evidence
🔹 Control Structures: Iteration
- 📌 Requirement: Use loops like
for,forEach, orwhileto cycle through game object arrays or handle animation frames. - 📝 Assessment Method: Code review of array processing or animation iteration steps.
- What it is: Iteration is the process of repeating a block of code a specific number of times or for every item in a list using loops.
- How it works: Instead of manually writing logic updates for individual entities, a loop automatically steps through an index collection one by one, invoking standard update or render calls on every item found.
- Why it helps: It makes systems scalable. The level engine can manage varying object workloads seamlessly without hardcoding updates for a specific quantity of assets.
// EVIDENCE: CONTROL STRUCTURES - ITERATION
class GameLevelRedRidingHood4 {
update() {
if (this.gameOver) return;
const player = this.gameEnv.gameObjects.find(obj => obj instanceof ShooterPlayer);
if (!player) return;
// Iteration Loop: dynamically updating and rendering every live projectile
if (player.bullets) {
player.bullets.forEach(bullet => {
bullet.update();
bullet.draw();
});
}
}
}
🔹 Control Structures: Conditionals
- 📌 Requirement: Implement collision detection and state transitions using
if/elsestatements and nested condition checks. - 📝 Assessment Method: Code review of logic forks and conditional blocks.
- What it is: Conditionals allow the program to make decisions during execution, routing data down completely different functional paths based on whether an expression evaluates to true or false.
- How it works: The engine runs a standard AABB equation to detect intersecting boundaries. If the coordinates overlap, a true state confirms a hit, causing the projectile to break and forcing a state shift in enemy health tracking.
- Why it helps: It adds structural interaction rules. Without evaluations checking spatial boundaries, elements would pass through each other without causing any reactive state events.
// EVIDENCE: CONTROL STRUCTURES - CONDITIONALS
class Bullet {
checkCollision(target) {
if (this.destroyed || !target) return false;
// Conditional Statement: Evaluates basic intersection formula
return this.x < target.x + target.width &&
this.x + this.width > target.x &&
this.y < target.y + target.height &&
this.y + this.height > target.y;
}
}
🔹 Control Structures: Nested Conditions
- 📌 Requirement: Implement complex game logic loops using multi-level conditional structures.
- 📝 Assessment Method: Code review of multi-level conditionals managing independent logic rules.
- What it is: Nested conditions place evaluation structures inside other outer conditional blocks, forming a dependency chain where inner logic cannot evaluate unless all parental states pass.
- How it works: When processing projectile collisions, the level script evaluates whether a bullet overlaps an enemy hitbox. Only if true does it step inside to check the next independent condition: whether the structural damage took down the target's final hit point.
- Why it helps: It isolates complex, layered behaviors. The engine prevents wasteful checks for end-game text updates and particle spawn procedures from firing unless a valid asset collision has already been verified first.
// EVIDENCE: CONTROL STRUCTURES - NESTED CONDITIONS
class GameLevelRedRidingHood3 {
update() {
if (this.enemyDefeated) return;
const player = this.gameEnv.gameObjects.find(obj => obj instanceof ShooterPlayer);
const enemy = this.gameEnv.gameObjects.find(obj => obj instanceof Enemy);
if (!player || !enemy) return;
// Nested Conditions Loop: Checking damage consequences sequentially
player.bullets.forEach(bullet => {
// Level 1: Checks if a projectile hitbox overlaps an enemy asset
if (bullet.checkCollision(enemy)) {
enemy.takeDamage(1);
bullet.destroy();
// Level 2 (Nested): Checks a separate state rule *only* after a hit occurs
if (enemy.hp <= 0) {
this.enemyDefeated = true; // Flips game state
this.showGrandmaVictory(); // Triggers end-level structural event
}
}
});
}
}