teaching machines

CS 245 Lecture 13 – Snake

October 15, 2013 by . Filed under cs245, fall 2013, lectures.

Agenda

TODO

Tasks for Snake

  1. Write Set.remove.
  2. Handle up, down, left, right cursor key events.
  3. How do we draw the snake?
  4. How do we manage the eggs?
  5. How we handle the snake moving to a spot with an egg?
  6. How we handle the snake moving to a spot without an egg?
  7. How do we handle checking for self-collision?
  8. How do we handle checking for wall collision?

Code

Orderable.java

package lecture12;

public interface Orderable<T> {
  public int compareTo(T other);
}

State.java

package lecture12;

public class State implements Orderable<State> {
  private String name;
  private String capital;

  public State(String name, String capital) {
    this.name = name;
    this.capital = capital;
  }

  public String toString() {
    return name + " is capitalized by " + capital;
  }

  @Override
  public int compareTo(State other) {
    return name.compareTo(other.name);
  }
}

SortedList.java

package lecture12;

public class SortedList<T extends Orderable<T>> {
  private T[] items;
  private int nitems;

  public SortedList() {
    items = (T[]) new Orderable[100];
    nitems = 0;
  }

  public int size() {
    return nitems;
  }

  public T get(int i) {
    if (i < 0 || i >= nitems) {
      throw new IndexOutOfBoundsException();
    }
    return items[i];
  }

  public void add(T itemToAdd) {
    if (nitems == items.length) {
      throw new WhoaBuddyException();
    }

    int i = nitems - 1;

    // Walk along the items as long as each item
    // is bigger than the one we're adding. Those
    // items need shifting.
    while (i >= 0 && itemToAdd.compareTo(items[i]) < 0) {
      items[i + 1] = items[i];
      --i;
    }

    items[i + 1] = itemToAdd;
    ++nitems;
  }
}

SnakePanel.java

package lecture13;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SnakePanel extends JPanel implements KeyListener {
  private static final int CELL_WIDTH = 36;
  private static final int CELL_HEIGHT = 24;

  private Point snake;

  public SnakePanel() {
    setFocusable(true);
    requestFocusInWindow();
    addKeyListener(this);

    snake = new Point(8, 3);
  }

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.GREEN);
    g.fillRoundRect(CELL_WIDTH * snake.x, CELL_HEIGHT * snake.y, CELL_WIDTH, CELL_HEIGHT, 13 - 4, 13 - 4);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Snake");

    frame.add(new SnakePanel());

    frame.setSize(1024, 768);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  @Override
  public void keyPressed(KeyEvent arg0) {
  }

  @Override
  public void keyReleased(KeyEvent event) {
    switch (event.getKeyCode()) {
      case KeyEvent.VK_DOWN:
        ++snake.y;
        break;
      case KeyEvent.VK_UP:
        --snake.y;
        break;
      case KeyEvent.VK_LEFT:
        --snake.x;
        break;
      case KeyEvent.VK_RIGHT:
        ++snake.x;
        break;
    }
    repaint();
  }

  @Override
  public void keyTyped(KeyEvent arg0) {
  }
}

Haiku

It’s like Hollywood
Many parts but few actors
Two degrees of List