Upload
florence-hawkins
View
212
Download
0
Embed Size (px)
Citation preview
Code reading skills
LEVEL GAME
Skeleton has error messages. Notice the red lines on right slider. Click… you’ll go to
an error. pieces = levels.getPieces(); // a statement with an error
Double click on piecesSelect Search->Declarations->project. Eclipse will
take you to where this is defined. From the declaration, try Search->References-
>Project. This will help you figure out how/where this data is used.
ECLIPSE CAN HELP
Your fi rst task … is to read the code in the provided skeleton.
How many of you went on to read the assignment details?
From employers: code reading is critical skill that most students have not yet acquired fully.
HOW DOES GAME ENGINE WORK?
How about main? Creates a Game Engine Calls playGame
playGame includes: Code to determine if game is over Calls setupLevel to get ready for next level Calls doOneLevel
After the while loop, some final messages and exit
WHERE TO BEGIN?
Checks if level overDisplays the boardGets the player’s optionMoves the player If player moved, interact with other pieces and let
other pieces move
Pseudo code: informal, high-level description
Productive strategy when writing code: write pseudocode as comments, fi ll in the details
DO ONE LEVEL
Prints the player (oh, symbol is P)Displays space if nothing at that locationTells object to draw itself
How can we be sure pieces know how to draw themselves? private Drawable [] pieces;
Pieces is an array of Drawable! But wait, what does that mean? How can that be? It means that EVERY object in the array must IMPLEMENT
Drawable Drawable is an interface. It has exactly one method:
draw()
DISPLAY BOARD
Anything that implements Drawable must have a draw method
We don’t care what type of thing it is, as long as it can draw itself.
BumbleBee? Sure!DoorMat? Sure!Kangalo? Sure!
else pieces[i].draw();
Can I add new types? Yes!!Do I need lots of if/else statements? No!!
THE POWER OF THE INTERFACE
public interface Moveable extends Drawable{public void move(Drawable[] pieces,
int playerLocation);}
public interface InteractingPiece extends Drawable {public InteractionResult interact(
Drawable [] pieces, int playerLocation);
}
SO WHAT IS THIS EXTENDS?
Start slow
You can figure out you need a LevelEngineLevelEngine needs some data structures (lists of
pieces, moveable pieces, interacting pieces).Set these to nullCreate ONE game pieceMake it as simple as possible (like my DoorMat)Can you get it to display?? Woo hoo!
FEELING UNSURE?*
* Of course you are. It’s like working with legacy code… lots to figure out before you can even begin to code.