10/4/15 Graphical Programming (1) Maze Program TOPICS Graphical - - PDF document

10 4 15
SMART_READER_LITE
LIVE PREVIEW

10/4/15 Graphical Programming (1) Maze Program TOPICS Graphical - - PDF document

10/4/15 Graphical Programming (1) Maze Program TOPICS Graphical Programming Using Classes (Objects) Multiple Files (Eclipse) Maze Logistics CS 160, Fall Semester 2015 2 Graphical Programming (2) Graphical Programming (3)


slide-1
SLIDE 1

10/4/15 1 Maze Program

TOPICS

  • Graphical Programming
  • Using Classes (Objects)
  • Multiple Files (Eclipse)
  • Maze Logistics

Graphical Programming (1)

CS 160, Fall Semester 2015 2

Graphical Programming (2)

§ No, we’re not going to show the code for the Puzzle program! (yet) § 150 lines of Java Swing code § What kinds of things does it do?

§ Set window size, title, and location § Setup a frame and panel, add buttons § Read a photo and extract parts of it § Listen for mouse and keyboard events

CS 160, Fall Semester 2015 3

Graphical Programming (3)

§ But, why not have you write code that controls a graphical program? § Maze program:

§ Move student around a maze § ~250 lines of graphical programming § You write the main method § You instantiate the Maze § You control the character

CS 160, Fall Semester 2015 4

slide-2
SLIDE 2

10/4/15 2 Maze Program: Objectives

n Use objects (classes) developed by

someone else

n Create an instance of a class n Call methods on the object n For example: graphical programming! n Multiple source files n Resource and data files

CS 160, Fall Semester 2015 5

Maze Program: Initial Code

// Create maze String fileName = args[0]; Maze maze = new Maze(fileName); System.out.println("Maze name: " + fileName); // Get dimensions mazeWidth = maze.getWidth(); mazeHeight = maze.getHeight(); System.out.println("Maze width: " + mazeWidth); System.out.println("Maze height: " + mazeHeight);

CS 160, Fall Semester 2015 6

Maze Program: Interface

// Moving character maze.moveRight(); // move right, no return value maze.moveLeft(); // move left, no return value maze.moveUp(); // move up, no return value maze.moveDown(); // move down, no return value // Getting position maze.getColumn(); // get column of character maze.getRow(); // get row of character

CS 160, Fall Semester 2015 7

Maze Program: Output

Maze name: maze1.txt Maze width: 6 Maze height: 6 Moved to row 0, column 1 Moved to row 1, column 1 … Chihiro found Haku, congratulations! Error Messages Chihiro went outside the maze! Chihiro ran into the witch Yubaba!

CS 160, Fall Semester 2015 8

slide-3
SLIDE 3

10/4/15 3 Maze Program: User Interface

CS 160, Fall Semester 2015 9

Maze Program: Algorithm

§ Rules for moving Chihiro around maze: § If not on edge, move straight to top edge § If on top edge, move to top-right § If on right edge, move to bottom-right § If on bottom edge, move to bottom-left § If on left edge, move to top-left

Guaranteed to find Haku, will never meet Yubaba!

CS 160, Fall Semester 2015 10

Maze Program: Methods

// Returns true if on any edge, false otherwise § public static boolean onEdge(); // Loops from current position to edge § public static void loopLeft(); § public static void loopRight(); § public static void loopUp(); § public static void loopDown(); Methods not required, but very useful!

CS 160, Fall Semester 2015 11

Maze Program: Setup

CS 160, Fall Semester 2015 12

§ ~/workspace/P6

§ Chihiro.png § Haku.png § Yubaba.png § Success.png § Maze*.txt

§ ~/workspace/P6/src

§ Maze.java § P6.java (write this!)