chapter 11—arrays and arraylists

16
Chapter 11—Arrays and ArrayLists The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Ja va Arrays and ArrayLists C H A P T E R 1 1 Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same There’s a green one and a pink one and a blue one and a yellow one And they're all made out of ticky-tacky and they all look just the same —Malvina Reynolds, “Little Boxes,” 1962 CS101 @ Özyeğin University Slides are adapted from the originals available at http://www-cs-faculty.stanford.edu/~eroberts/books/ArtAndScienceOfJava/ Lecture Slides, Part III 1

Upload: zurina

Post on 10-Feb-2016

58 views

Category:

Documents


3 download

DESCRIPTION

Java. The Art and Science of. An Introduction. to Computer Science. ERIC S. ROBERTS. C H A P T E R 1 1. Arrays and ArrayLists. Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 11—Arrays and ArrayLists

Chapter 11—Arrays and ArrayLists

The Art and Science of

An Introductionto Computer ScienceERIC S. ROBERTS

Java

Arrays and ArrayListsC H A P T E R 1 1

Little boxes, on a hillside, little boxes made of ticky-tackyLittle boxes, little boxes, little boxes, all the sameThere’s a green one and a pink one and a blue one and a yellow oneAnd they're all made out of ticky-tacky and they all look just the same

—Malvina Reynolds, “Little Boxes,” 1962

CS101 @ Özyeğin University

Slides are adapted from the originals available athttp://www-cs-faculty.stanford.edu/~eroberts/books/ArtAndScienceOfJava/

Lecture Slides, Part III

1

Page 2: Chapter 11—Arrays and ArrayLists

Example: SimpleSnake• In this problem we will implement a simple version of the

snake game in which a snake bounces from the walls of the screen.

2

Page 3: Chapter 11—Arrays and ArrayLists

SimpleSnake• Each part of the snake body will have a size of BODY_PART_WIDTH.

• There are SNAKE_LENGTH many body parts.• Each body part is a GOval object. All the body parts are kept

in a GOval array.

• Corresponding to each body part, we keep the velocity in the x direction and in the y direction.

• Food for thought: Why don’t we keep a single velocity value?

private double[] xVelocities = new double[SNAKE_LENGTH];private double[] yVelocities = new double[SNAKE_LENGTH];

3

Page 4: Chapter 11—Arrays and ArrayLists

SimpleSnakepublic void run() { initializeSnakeBody(); initializeVelocities(); while(true) { moveSnake(); checkBounds(); pause(20); }}

4

Page 5: Chapter 11—Arrays and ArrayLists

SimpleSnake(initializing body parts)

BODY_PART_WIDTH

BODY_PART_WIDTH

BODY

_PAR

T_WI

DTH

/ sq

rt(2

)

5

Page 6: Chapter 11—Arrays and ArrayLists

SimpleSnakepublic void initializeSnakeBody() { int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; for(int i = 0; i < snakeBody.length; i++) { snakeBody[i] = createSnakeBodyPartCenteredAt(xCenter, yCenter); xCenter -= (BODY_PART_WIDTH/Math.sqrt(2)); yCenter -= (BODY_PART_WIDTH/Math.sqrt(2)); add(snakeBody[i]); }}

public GOval createSnakeBodyPartCenteredAt(int xCenter, int yCenter) { GOval circle = new GOval(xCenter - BODY_PART_WIDTH/2, yCenter - BODY_PART_WIDTH/2, BODY_PART_WIDTH, BODY_PART_WIDTH); circle.setFilled(true); circle.setFillColor(Color.BLUE); return circle;}

6

Page 7: Chapter 11—Arrays and ArrayLists

SimpleSnakepublic void initializeVelocities() { for(int i = 0; i < xVelocities.length; i++) { xVelocities[i] = 3; } for(int i = 0; i < yVelocities.length; i++) { yVelocities[i] = 3; }}

public void moveSnake() { for(int i = 0; i < snakeBody.length; i++) { snakeBody[i].move(xVelocities[i], yVelocities[i]); }}public void checkBounds() { for(int i = 0; i < snakeBody.length; i++) { double x = snakeBody[i].getX(); double y = snakeBody[i].getY(); if(x < 0 || x > getWidth() - BODY_PART_WIDTH) { xVelocities[i] *= -1; } if(y < 0 || y > getHeight() - BODY_PART_WIDTH) { yVelocities[i] *= -1; } }} 7

Page 8: Chapter 11—Arrays and ArrayLists

SimpleSnake v.2• In this problem we will implement yet another simple version

of the snake game.

• P.S. You may need to click on the window to request focus so that key events will be captured.

8

Page 9: Chapter 11—Arrays and ArrayLists

SimpleSnake v.2• In this version, there is a single, constant SPEED value. The

head of the snake has a direction, which can be one of NORTH, EAST, SOUTH, WEST.

• After each move, a body part replaces the part in the front.

snakedirection

9

Page 10: Chapter 11—Arrays and ArrayLists

SimpleSnake v.2• In this version, there is a single, constant SPEED value. The

head of the snake has a direction, which can be one of NORTH, EAST, SOUTH, WEST.

• After each move, a body part replaces the part in the front.

snakedirection

10

Page 11: Chapter 11—Arrays and ArrayLists

SimpleSnake v.2• Moving the snake parts:

• Each part element gets the location of the element in front of it.

• Head element is moved according to its direction.

0 1 2 3 4 5 6 7

11

Page 12: Chapter 11—Arrays and ArrayLists

SimpleSnakepublic class SimpleSnakeV2 extends GraphicsProgram { private final int BODY_PART_WIDTH = 20; private final int SNAKE_LENGTH = 12; private final int SPEED = BODY_PART_WIDTH;

private GOval[] snakeBody = new GOval[SNAKE_LENGTH];

private final int NORTH = 1; private final int EAST = 2; private final int SOUTH = 3; private final int WEST = 4; // Direction of the head is one of {N, W, S, E} private int direction = EAST;

// Cont’d on the next slide // . . .

12

Page 13: Chapter 11—Arrays and ArrayLists

public void run() { addGrid(); initializeSnakeBody(); while(true) { moveSnake(); pause(200); } }

public void addGrid() { for(int i = 0; i < getWidth(); i += BODY_PART_WIDTH) { add(new GLine(i, 0, i, getHeight())); } for(int i = 0; i < getHeight(); i += BODY_PART_WIDTH) { add(new GLine(0, i, getWidth(), i)); } }

13

Page 14: Chapter 11—Arrays and ArrayLists

14

Page 15: Chapter 11—Arrays and ArrayLists

public void moveSnake() { // Each body part replaces the one in front of it for(int i = snakeBody.length - 1; i >= 1; i--) { GOval prevPart = snakeBody[i-1]; snakeBody[i].setLocation(prevPart.getX(), prevPart.getY()); } // Move the head if(direction == EAST) { snakeBody[0].move(SPEED, 0); } else if(direction == WEST) { snakeBody[0].move(-SPEED, 0); } else if(direction == NORTH) { snakeBody[0].move(0, -SPEED); } else if(direction == SOUTH) { snakeBody[0].move(0, SPEED); } }

15

Page 16: Chapter 11—Arrays and ArrayLists

// The method below is invoked whenever the user presses a key // You do not need to spend too much time understanding this part public void keyPressed(KeyEvent keyEvent) { // VK_UP: key code for up arrow key // VK_RIGHT: key code for right arrow key // VK_DOWN: key code for down arrow key // VK_LEFT: key code for left arrow key if(keyEvent.getKeyCode() == KeyEvent.VK_UP) { direction = NORTH; } else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) { direction = EAST; } else if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN) { direction = SOUTH; } else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT) { direction = WEST; } }

public void init() { setSize(600, 400); addKeyListeners(); // required for key interaction }}

16