Final Exam: no makeup, please aim high. 

There will be a study guide (150+ questions). 

They will be from the entire book (question + short answer). 

Random questions the final (120 minutes, 60 questions). 

Make-ups will be offered until the last minute. 

Please don't wait until the last minute. 

There is a lab this week and another one next week. 

No more assignments just three stages of the project. 

Stage One: a description of the project's stages two and three. 

Stage Two and Three follow (use labs this week and next). 

I will be in labs next week to ask you if all is well. 

The project: 

  (a) Tetris 

  (b) Shopping cart with Tomcat and Java servlets/JSP 

  (c) Write-in: stage one is to describe it as well as stages two and three. 

We start now with Stage One of Tetris. 

My approach: define through a functional prototype. 

import java.awt.*;
import java.awt.event.*;

public class Game implements World {
  Shape current; 
  public Game() {
    this.current = new Shape(200, 0); 
  }
  public void teh() {
    this.current.fall();     
  }
  public void meh(MouseEvent e) { }
  public void keh(KeyEvent e) {
    
  }
  public void draw(Graphics g) {
    System.out.println( this.current );     
  }
  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start(1000, 400); 
  }
}

--

public class Shape {
  int x, y; 
  public Shape(int x, int y) {
    this.x = x; 
    this.y = y;
  }
  public void fall() {
    this.y += 5; 
  }
  public String toString() {
    return "(" + this.x + ", " + this.y + ")";  
  }
}

--

They can't fall forever. 

import java.awt.*;
import java.awt.event.*;

public class Game implements World {
  Shape current; 
  Ground ground; 
  public Game() {
    this.current = new Shape(200, 0); 
    this.ground = new Ground(); 
  }
  public void teh() {
    this.current.fall(); 
    if (current.y >= 400) {
      this.ground.add( this.current );
      this.current = new Shape(200, 0); 
    }
  }
  public void meh(MouseEvent e) { }
  public void keh(KeyEvent e) {
    int code = e.getKeyCode(); 
    if (code == 37) {
      this.current.left(); 
    } else { 
      this.current.right(); 
    }
  }
  public void draw(Graphics g) {
    System.out.println( this.ground );     
    this.ground.draw( g );    
    this.current.draw( g ); 
  }
  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start(200, 400); 
  }
}

--

import java.awt.*; 

public class Shape {
  int x, y, r; 
  Color color; 
  public Shape(int x, int y) {
    this.r = (int) (10 + (Math.random() * 20));
    this.x = x; 
    this.y = y;
    this.color = new Color( (float) Math.random(), 
                            (float) Math.random(), 
                            (float) Math.random() ); 
  }
  public void fall() {
    this.y += 5; 
  }
  public String toString() {
    return "(" + this.x + ", " + this.y + ")";  
  }
  public void draw(Graphics g) {
    g.setColor( this.color ); 
    g.fillOval( this.x - this.r, this.y - this.r, 2 * this.r, 2 * this.r ); 
    g.setColor( Color.BLACK ); 
    g.drawOval( this.x - this.r, this.y - this.r, 2 * this.r, 2 * this.r ); 
  }
  public void left() { this.x -= 5; } 
  public void right() { this.x += 5; } 
}

--

import java.util.*; 
import java.awt.*;

public class Ground extends ArrayList<Shape> {
  public void draw(Graphics g) {
    for (Shape s : this)
      s.draw(g); 
  }
}

--

When else should the shapes stop falling? 

import java.awt.*;
import java.awt.event.*;

public class Game implements World {
  Shape current; 
  Ground ground; 
  public Game() {
    this.current = new Shape(200, 0); 
    this.ground = new Ground(); 
  }
  public void teh() {
    this.current.fall(); 
    if (current.y >= 400 || this.current.touches( this.ground ) ) {
      this.ground.add( this.current );
      this.current = new Shape(200, 0); 
    }
  }
  public void meh(MouseEvent e) { }
  public void keh(KeyEvent e) {
    int code = e.getKeyCode(); 
    if (code == 37) {
      this.current.left(); 
    } else { 
      this.current.right(); 
    }
  }
  public void draw(Graphics g) {
    System.out.println( this.ground );     
    this.ground.draw( g );    
    this.current.draw( g ); 
  }
  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start(50, 400); 
  }
}

--

http://zetcode.com/tutorials/javagamestutorial/pacman/

--

import java.awt.*; 

public class Shape {
  int x, y, r; 
  Color color; 
  public Shape(int x, int y) {
    this.r = (int) (30 + (Math.random() * 60));
    this.x = x; 
    this.y = y;
    this.color = new Color( (float) Math.random(), 
                            (float) Math.random(), 
                            (float) Math.random() ); 
  }
  public void fall() {
    this.y += 5; 
  }
  public String toString() {
    return "(" + this.x + ", " + this.y + ")";  
  }
  public void draw(Graphics g) {
    g.setColor( this.color ); 
    g.fillOval( this.x - this.r, this.y - this.r, 2 * this.r, 2 * this.r ); 
    g.setColor( Color.BLACK ); 
    g.drawOval( this.x - this.r, this.y - this.r, 2 * this.r, 2 * this.r ); 
  }
  public void left() { this.x -= 5; } 
  public void right() { this.x += 5; } 
  public boolean touches(Ground g) {
    for (Shape shape : g) {
      if (shape.overlaps(this)) 
        return true;
    }
    return false; 
  }
  public boolean overlaps(Shape other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y; 
    double d = Math.sqrt( dx * dx + dy * dy ); 
    return d <= this.r + other.r; 
  }
}

--

import java.util.*; 
import java.awt.*;

public class Ground extends ArrayList<Shape> {
  public void draw(Graphics g) {
    for (Shape s : this)
      s.draw(g); 
  }
}

--