Object_Design_In_Class_Exercise

Object-Oriented Software Engineering
Using UML, Patterns, and Java
Course Review
Bernd Bruegge
Applied Software Engineering
Technische Universitaet Muenchen
Review: Object Design Activities
Select Subsystem
Specification
Identifying missing
attributes & operations
Reuse
Identifying components
Specifying visibility
Adjusting components
Specifying types &
signatures
Identifying patterns
Specifying constraints
Specifying exceptions
Applying patterns
Review: Object Designer Roles
Developers play different roles during object design
Developer
Class User
Call Class
Class Implementor
Realize Class
Class Extender
Refine Class
Development of an Interactive Game
Problem Statement:
• Several cars are driving in a rectangular game
board. Some cars are fast, others are slower
• The number of cars is arbitrary, but fixed at delivery time
• The player controls a certain car („DrivenCar“) with
the mouse
• The initial direction of each car is chosen by random
• The cars can collide with each other and with the
border of the game board
• Each collision between cars leads to a winner and a looser
• The looser stops at the point of collision („crashes“)
• If a car encounters the border, it is bounced back
depending on its speed and direction („physical laws are
followed“).
Analysis and Design Questions
•
•
•
•
How do we represent the game field?
How do we model the cars?
How do we animate all the cars simultaneously?
What do we do, when a car hits the edge?
• How do we compute the new route?
• How do we compute a collision?
• Do we compute a new route for the winner?
• How do we control the driven car with the
mouse?
Analysis: Use Cases
•
•
•
•
•
•
•
•
•
Generating the gameboard
Starting the Music
Generating one or more cars
Moving a car
Starting the game
Quitting the game
Car hits another car
Car hits border of gameboard
Determining a collision
Analysis: Identification of Participating
Objects
•
•
•
•
•
•
•
•
•
Generating the gameboard
Starting the Music
Generating one or more cars
Moving a car
Starting the game
Quitting the game
Car hits another car
Car hits border of gameboard
Determining a collision
Car
Gameboard
Referee
Music
Analysis: Initial Object Model for
GameBoard, Referee and Car
Analysis: Classification of Cars
(“Taxonomy in the Application Domain”)
Demonstration of a First Prototype
• In Gameboard.java
(bumpers:cars:GameBoard.java) you determine
the number of opponent cars (robot cars)
Object Design: Discussion of Collisions
• Which algorithm do we use to detect collisions?
• If we want to make the game extensible, the type
of collision is probably a function of the car type
(most certainly of its size)
• Where do we place the collision detection
algorithm? We have two possibilities:
• Option 1: Place it in the game field or
• Option 2: Let the car determine the collisions themselves
• If we let the cars determine the collisions, then
every type car depends on every other type of car
• Example: When we introduce a new type of car, say a
“fancy car”, we have to modify every existing type of
car, to implement collisions with fancy cars
• The maintainability of the game is much higher, if
the game field handles the collisions.
Thoughts about the
Collision Detection Algorithm
(0,0)
• A car could represented by a bounding box
• This is the smallest rectangle, that
contains all pixels of the picture of the
car
We get bounding box of a picture by calling
getSize() followed by calling
getPosition()
A collision occurs, if two given bounding boxes have at
least one pixel in common (intersects()).
Small problem: The picture of a car rarely resembles a
rectangle, the bounding box often contains pixels that
don’t belong to the picture of the car
To compensate for the “empty space”, we reduce the
bounding box to 3/4 of its original size (with growth()),
and place its center slightly to the lower left (with
translate())
Remaining Problem: Who determines the winner of the
collision?
(x,y)
h
w
Detecting Collisions
public void moveCars() {
// Snippet from Referee code….
// Check for collisions
for (int i = 0; i < cars.length; i++{
if (cars[i].isCrunched( ))
{continue;}
Car crashedCar =
collisionStrategy.detectCollision(
drivenCar,
cars[i]);
if(crashedCar != null){
BANG.play();
crashedCar.setCrunched();
if(drivenCar.isCrunched()){
stopGame();
} else {
// check if player won
}}}
Car
Referee
+startGame()
+ stopGame()
+ moveCars()
*
Position_x, Position_Y,…
+boolean isCrunched()
+ Point getPosition()
+ Dimension getSize()
+ setCrunched()
public Car detectCollision(Car car1, Car car2){
Point p1 = car1.getPosition( );
Dimension d1 = car1.getSize( );
Rectangle r1 = new Rectangle(p1, d1);
r1.translate(p1.x / 8, p1.y / 8);
r1.grow(-1 * d1.width / 4, -1 * d1.height / 4);
Point p2 = car2.getPosition( );
Dimension d2 = car2.getSize( );
Rectangle r2 = new Rectangle(p2, d2);
r2.translate(p2.x / 8, p2.y / 8);
r2.grow(-1 * d2.width / 4, -1 * d2.height / 4);
if (r1.intersects(r2)) {
if (p1.y > p2.y){return car2; }else{
return car1;}
} return null; }
Offering several Algorithms for Winning a
Collision
HigherCar
WinsCollision
LowerCar
WinsCollision
Literature
• TBD