teaching machines

CS 245 Lecture 14 – Linked Snake

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

Agenda

TODO

Array Debate

What is good about arrays?

vs.

What is bad about arrays?

Code

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 java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import lecture11.Set;

public class SnakePanel extends JPanel implements KeyListener {
  public static final int CELL_WIDTH = 36;
  public static final int CELL_HEIGHT = 24;
  
  private Snake snake;
  private Set<Point> eggs;
  
  public SnakePanel() {
    setFocusable(true);
    requestFocusInWindow();
    addKeyListener(this);
    
    snake = new Snake(8, 3);
    
    eggs = new Set<Point>();
    Random g = new Random();
    for (int i = 0; i < 70; ++i) {
      int x = g.nextInt(40);
      int y = g.nextInt(30);
      eggs.add(new Point(x, y));
    }
  }
  
  @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);
    snake.draw(g);
    
    g.setColor(Color.BLUE);
    for (int i = 0; i < eggs.size(); ++i) {
      Point egg = eggs.get(i);
      g.fillOval(CELL_WIDTH * egg.x, CELL_HEIGHT * egg.y, CELL_WIDTH, CELL_HEIGHT);
    }
  }

  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;
//    }
//    
//    int widthInCells = getWidth() / CELL_WIDTH;
//    int heightInCells = getHeight() / CELL_HEIGHT;
//    
//    // Is game over?
//    if (snake.x < 0 || snake.x >= widthInCells ||
//        snake.y < 0 || snake.y >= heightInCells) {
//      System.exit(1);
//    }
    
    repaint();
  }

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

Snake.java

package lecture13;

import java.awt.Graphics;
import java.awt.Point;

public class Snake {
  private Segment head;

  public Snake(int x,
               int y) {
    Segment tail = new Segment(new Point(x - 1, y), null);
    head = new Segment(new Point(x, y), tail);
  }

  public void draw(Graphics g) {
//    for (int i = 0; i < segments.length; ++i) {
//      // draw segments[i]
//    }
    
    for (Segment s = head; s != null; s = s.getNext()) {
      Point p = s.getLocation();
      g.fillRoundRect(SnakePanel.CELL_WIDTH * p.x,
                      SnakePanel.CELL_HEIGHT * p.y,
                      SnakePanel.CELL_WIDTH, 
                      SnakePanel.CELL_HEIGHT, 13 - 4, 13 - 4);
    }
  }

  private static class Segment {
    private Point location;
    private Segment next;

    public Segment(Point location,
                   Segment next) {
      this.location = location;
      this.next = next;
    }
    
    public Point getLocation() {
      return location;
    }
    
    public Segment getNext() {
      return next;
    }
    
    public boolean isTail() {
      return next == null;
    }
  }
}

Haiku

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