Bug Source Code Grid World Case Study Solutions

This article is a personal look at Open Source, and how I value it. Part 2 will be a deeper dive into Open Source. Its true values, success stories, horror stories, licenses, and much more. Make sure you come back to read that one!

I gave a talk last week at WXG 2014 on “The Value of Open Source.” This topic is something near and dear to all of our hearts here at SendGrid, and was definitely an eye opener for me; revealing a lack of Open Source uptake in the developer community. Around 1/3 of my audience didn’t embrace Open Source in one way or another and this came as quite the shock to me!

When I start a hack, my process is generally:


99% of hacks on my laptop are on my GitHub. Do people find them useful? Probably not, yet I still share all my source code – it’s a habit and part of my workflow, I guess. What surprised me, is the amount of Devs who don’t do this, or any variation of this.

Obviously, we all have our “private” projects. I know I do. Ideas we think are just too awesome to share publicly, code we don’t deem idiomatic enough, or private projects for work. I fully understand that not everything can be Open Sourced, but for those projects that can – why not?

When I originally wrote up the slide deck for the talk, I honestly thought it was a bit boring. I figured I was spewing generic, hackneyed information that 100% of my audience would have already known about, or failing that, not actually be interested in. How wrong I was. This all leads back to my (false) prior assumption that every developer engages in Open Source in one way or another. Realising there was a large portion of developers not embracing the Open-Source-Ethos was not only eye-opening, but excellent; as it gave me a chance to educate and empower on the true value of Open Source.

Open Source to Me

So what does Open Source truly mean to me? Where does the value in Open Source lie? If you asked me this question two years ago, I would have answered “Code. Open Source means code.” Whilst I still believe code is a huge element in Open Source, I now understand it doesn’t define it.

When I asked this question to my audience during my talk, I naturally got a mix of answers, but I was pleasantly surprised to find that the majority agreed with me on this one. The majority answer was: People.

While there is a large focus on code in Open Source, the true value lies in the community surrounding a project, and the developers who work hard to contribute to said project. Contributions to Open Source projects can often be a thankless task. It is also not done for money, but for the love of a project. There aren’t many other industries in the world that do anything like this.

The relationships between product owners and contributors should be a stellar one. Unfortunately, this is often not the case, and that upsets me. I’ve seen many product owners totally neglect their loving community, ignore them, and in some extreme cases even heckle them. This is absolutely ludicrous.

If you’re an Open Source project owner/manager, remember: these developers are contributing to your project to make it better. They’re giving up their own time to fix bugs, fix spellings, refactor code, and generally improve your project. Plus, they’re doing it out of the kindness of their own hearts.

It doesn’t take much time or effort to say “thank you” to your contributors. There are companies that offer crazy sums of money for people to patch bugs, but obviously, that’s not a viable option for all of us! Here at SendGrid, we send out badges to all of our contributors. We engage with them, thank them, and (I hope) make them feel appreciated, because they really are.

Learning

To me, one of the best parts of owning and contributing to Open Source software is the learning involved in doing so.

From a product owner perspective:

You have a bug in your code that you missed or couldn’t fix. A community member comes along and writes a fix for that bug. You now have a solution to your code problem and will learn from that.

From a contributor perspective:

You have the opportunity to fix a bug or submit a feature to your favorite piece of software. You get to interact directly with other community members, and build something awesome. The knowledge shared between the community around a project is truly spectacular, and in my opinion, one of the most important values of Open Source.

Getting Involved

It’s safe to say, I am a HUGE advocate of Open Source. I love almost everything it stands for. The idea of sharing anything and everything so another member of the dev community can benefit from it would be, in itself, enough to make me go Open Source if I wasn’t. But it’s more than that. By contributing to Open Source projects I am learning, sharpening my skills, and empowering other devs.

By Open Sourcing my own projects, I am opening up a world of possibilities (sorry for the cliché!). Allowing other people to contribute to my source code means I have a bigger number of people working on it, therefore a faster turnaround time in bug fixing and code refactoring. I am opening up a floor of learning from developers writing better code than mine, and me learning from it. But again, I’m empowering developers by giving them a piece of software, to use as they wish, if they should ever need to do so.

If you ever find a bug in a piece of software that you like, and if you can, FIX IT YOURSELF! Getting involved in the Open Source community is an incredibly rewarding experience.

Open Source rules.

– @rbin.


Presentation on theme: "GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of."— Presentation transcript:

1 GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of the program, every occupant of the grid gets a chance to act. Each actor acts according to a clearly specified set of behaviors that can include moving, changing color, changing direction, or removing other actors from the grid. AP CS 2009 1

2 The Classes of GridWorld AP CS 2009 2 ACTOR FLOWERROCK CHAMELEON CRITTER CRITTER BOXBUG BUG

3 The Classes of GridWorld AP CS 2009 3 ACTOR GRID INTERFACE ABSTRACT GRID LOCATION UNBOUNDED GRID BOUNDED GRID

4 The Actors of GridWorld A rock does nothing A flower darkens its color A bug moves forward when it can. It can move into any empty spot or onto a flower. When it moves, it deposits a flower in its previous location. If it moves to a location occupied by a flower, that flower is removed from the grid. A bug cannot move if it is blocked in front by either another (non-flower) actor or the edge of the grid. When it is prevented from moving, it turns 45 degrees to the right. AP CS 2009 4

5 The Actors (continued) A BoxBug moves like a bug. Additionally, if it encounters no obstacles in its path, it traces out a square of flowers with a given side length. If a BoxBug is blocked from moving, it makes two right turns and starts again. A Critter gets a list of its adjacent neighboring actors and processes them by “eating” each actor that is not a rock or another critter. It then randomly selects one of the empty neighboring locations and moves there. If there are not available empty locations, a critter does not move. AP CS 2009 5

6 The Actors (continued) A ChameleonCritter gets a list of its adjacent neighbors, randomly picks one of them, and changes its color to that of the selected actor. The ChameleonCritter moves like a Critter but, additionally, it first changes its direction to face its new location before moving. AP CS 2009 6

7 The Location Class Row and Column values representing positions on the grid. Provides constants for compass directions and turn angles. Provides methods for determining relationships between locations and compass directions. AP CS 2009 7

8 The Location Class Constantint Value Location.NORTH0 Location.EAST90 Location.SOUTH180 Location.WEST270 Location.NORTHEAST45 Location.SOUTHEAST135 Location.SOUTHWEST225 Location.NORTHWEST315 AP CS 2009 8

9 The Location Class Constantint Value Location.LEFT-90 Location.RIGHT 90 Location.HALF_LEFT-45 Location.HALF_RIGHT 45 Location.FULL_CIRCLE360 Location.HALF_CIRCLE180 Location.AHEAD0 AP CS 2009 9

10 The Location Class To get an actor to turn setDirection(getDirection() + Location.RIGHT); Methods of the Location class getRow, getCol, getAdjacentLocation, getDirectionToward, equals, compareTo, toString AP CS 2009 10

11 The Actor Class The Actor class is a superclass for every creature that appears in the grid. An actor has a location, direction and color. It can also change each of these instance variables using setColor, and setDirection. It has access to it’s grid. It can putSelfInGrid and removeSelfFromGrid, thus changing its location. It also has methods called moveTo, act, toString, getColor, getDirection and setGrid. AP CS 2009 11

12 The Rock and Flower Classes A rock acts by doing nothing. It has a default constructor that creates a black rock and a second constructor that allows construction of a rock with a specified color. The act method is overridden – it has an empty body. A flower acts by darkening its color. It has a default constructor that creates a pink flower, and a second constructor that allows a construction of a flower with a specified color. The overridden act method darkens the flower’s color. AP CS 2009 12

13 The Bug Class A bug is an actor that moves forward in a straight line, turning only when it is blocked. A bug can be blocked by either the edge of the grid, or an actor that is not a flower. As a bug moves, it steps on any flower in its path, causing the removal of that flower from the grid. After each step, the bug places a flower in its previous location. AP CS 2009 13

14 The Bug Class The bug class contains the following methods:  act  turn  move  canMove The act method is overridden. AP CS 2009 14

15 AP CS 200915 GridWorld An Introduction Part 1

16 AP CS 2009 16 Overview  Case Study contains 5 Parts:  Part 1: Provides experiments to observe the attributes and behavior of the actors.  Part 2: Defines Bug variations.  Part 3: Explores the code that is needed to understand and create actors.  Part 4: Defines classes that extend the Critter class.  Part 5: (CS AB only) Explains grid data structures.

17 AP CS 2009 17 Part 1: Observing and Experimenting with GridWorld  The first look at GridWorld  Exploring Actor state and behavior  Exploring Bug state and behavior  Exploring Flower state and behavior  Exploring Rock state and behavior  Demo:  BugRunner  EXPLORATION time

18 AP CS 2009 18 BugRunner (code) public static void main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Bug()); world.add(new Rock()); world.show(); }

19 AP CS 2009 19 BugRunner Exploration You can add a new Bug, Rock, Flower, or Actor  An Actor can be added to the Grid if any Actor subclass has been added in the BugRunner class.  Remember the IS-A relationship of Inheritance; a Bug IS-A Actor a Rock IS-A Actor a Flower IS-A Actor

20 AP CS 200920 GridWorld Part 2

21 AP CS 2009 21 Part 2: Bug Variations  Simple Inheritance  Demo:  BoxBug (testable code)  BoxBugRunner

22 AP CS 2009 22 BoxBugRunner public static void main(String[] args) { ActorWorld world = new ActorWorld(); BoxBug alice = new BoxBug(6); alice.setColor(Color.ORANGE); BoxBug bob = new BoxBug(3); world.add(new Location(7, 8), alice); world.add(new Location(5, 5), bob); world.show(); }

23 AP CS 2009 23 BoxBug Exploration Run the program What kind of objects can you add to the grid? Create a world with a Bug and a BoxBug Right click on a Bug. Right click on a BoxBug. Do you see any differences?

24 AP CS 2009 24 BoxBug You will need to understand  Bug constructors  Act method You will need to use  canMove  turn  move  You will create a new type of Bug by modifying BoxBug code

25 AP CS 2009 25 BoxBug Code public class BoxBug extends Bug { private int steps; private int sideLength; public BoxBug(int length) { super(); steps = 0; sideLength = length; } public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); steps = 0; }

26 AP CS 2009 26 Extending the Bug class Override Bug 's act method Each call to the move method should be guarded by a call to the canMove method Add additional instance fields if needed Add new methods to the subclass if needed Constructors for the subclass call super() or super(someColor)

27 AP CS 2009 27 Extending the Bug class CircleBugSpiralBug (unbounded grid) UnboundedGrid grid = new UnboundedGrid (); ActorWorld world = new ActorWorld(grid);

28 AP CS 2009 28 BoundedGrid vs. UnboundedGrid ActorWorld world = new ActorWorld(); world.add(new Bug()); world.add(new Rock()); UnboundedGrid grid = new UnboundedGrid (); ActorWorld world = new ActorWorld(grid); SpiralBug sp = new SpiralBug(3); world.add(new Location(11,9),sp);

29 AP CS 2009 29 Extending the Bug class ZBugDancingBug (array)

30 AP CS 200930 GridWorld Part 3

31 AP CS 2009 31 Part 3: GridWorld Classes and Interfaces A grid contains Actor s Two types of grids  bounded  unbounded Locations in the grid are represented by objects of type Location An Actor knows what grid it lives in and its location within that grid.

32 AP CS 2009 32 Location implements Comparable interface Built in compass directions  public static final int SOUTH = 180;  public static final int WEST = 270;  public static final int NORTHEAST = 45; Built in turn directions  public static final int RIGHT = 90;  public static final int HALF_LEFT = -45;  public static final int HALF_RIGHT = 45;

33 AP CS 2009 33 Location accessors getRow() getCol()

34 AP CS 2009 34 Location public Location getAdjacentLocation(int direction) returns the adjacent location in the compass direction that is closest to direction public int getDirectionToward(Location target) returns the closest compass direction from this location toward target

35 AP CS 2009 35 Location Location loc1 = new Location(4, 3); Location loc2 = new Location(3, 4); Location loc3 = loc2.getAdjacentLocation(Location.SOUTH); int dir = loc1.getDirectionToward(new Location(6, 5));

36 AP CS 2009 36 Grid interface The interface Grid specifies the methods for any grid that contains objects of the type E. Two classes, BoundedGrid and UnboundedGrid implement the interface.

37 AP CS 2009 37 Grid interface boolean isValid(Location loc) returns true if loc is valid in this grid, false otherwise Precondition: loc is not null E put(Location loc, E obj) puts obj at location loc in this grid and returns the object previously at that location (or null if the location was previously unoccupied) Precondition: (1) loc is valid in this grid (2) obj is not null E remove(Location loc) removes the object at location loc and returns it (or null if the location is unoccupied) Precondition: loc is valid in this grid E get(Location loc) returns the object at location loc (or null if the location is unoccupied) Precondition: loc is valid in this grid

38 AP CS 2009 38 Grid interface ArrayList getOccupiedLocations() returns all occupied locations in a grid. ArrayList getValidAdjacentLocations(Location loc) returns all valid locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList getEmptyAdjacentLocations(Location loc) returns all valid empty locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList getOccupiedAdjacentLocations(Location loc) returns all valid occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList getNeighbors(Location loc) returns all objects in the occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid

39 AP CS 2009 39 Grid interface How can you obtain a count of the objects in a grid? How can you obtain a count of the empty locations in a bounded grid?

40 AP CS 2009 40 The Actor class Accessor methods  public Color getColor()  public int getDirection()  public Grid getGrid()  public Location getLocation() Other Actor Methods  public void putSelfInGrid(Grid gr, Location loc) establishes the actor’s location as well as the grid in which it is placed  public void removeSelfFromGrid() removes the actor from its grid and makes the actor’s grid and location both null.

41 AP CS 2009 41 The Actor class When adding or removing actors, do not use the put and remove methods of the Grid interface.  Those methods do not update the location and grid instance variables of the actor. That is a problem since most actors behave incorrectly if they do not know their location. To ensure correct actor behavior, always use the putSelfInGrid and removeSelfFromGrid methods of the Actor class.

42 AP CS 2009 42 The Actor class The moveTo method allows the actor to move to any valid location. If the actor calls moveTo for a location that contains another actor, the other one removes itself from the grid and this actor moves into that location. The act method of the Actor class reverses the direction of the actor. You override this method in subclasses of Actor to define actors with different behavior.

43 AP CS 2009 43 The Actor class Can an actor put itself into a grid twice without first removing itself? Can an actor remove itself from a grid twice? Can an actor be placed into a grid, remove itself, and then put itself back? Try it out. What happens?

44 AP CS 2009 44 Rock, Flower, Bug Time to look at code!  Rock behavior?  Flower behavior?  Bug behavior?

45 AP CS 2009 45 Group Activity: Jumper Each group creates a class called Jumper. This actor can move forward two cells in each move. It “jumps” over rocks and flowers. It does not leave anything behind it when it jumps.

46 AP CS 2009 46 Group Activity: Jumper 1. What will a Jumper do if the location in front of it is empty, but the location two cells in front contains a flower or a rock? 2. What will a Jumper do if the location two cells in front of the Jumper is out of the grid? 3. What will a Jumper do if it is facing an edge of the grid? 4. What will a Jumper do if another actor (not a flower or a rock) is in the cell that is two cells in front of the jumper? 5. What will a Jumper do if it encounters another Jumper in its path? 6. Are there any other tests the Jumper needs to make?

47 AP CS 2009 47 Group Activity: Jumper 1. Which class should Jumper extend? 2. Is there an existing class that is similar to the Jumper class? 3. Should there be a constructor? If yes, what parameters should be specified for the constructor? 4. Which methods should be overridden? 5. What methods, if any, should be added? 6. What is the plan for testing the class?

48 AP CS 200948 GridWorld Part 4

49 AP CS 2009 49  Introduces the Critter class (testable code)  Uses template method, act It is usually not a good idea to override the act method in a Critter subclass. The Critter class was designed to represent actors that process other actors and then move. If you find the act method unsuitable for your actors, you should consider extending Actor, not Critter.  The act method calls five methods accomplishing the following tasks:  find neighbors ( getActors )  process neighbors ( processActors )  find candidates for move locations ( getMoveLocations )  select a move location ( selectMoveLocation )  make a move ( makeMove )

50 AP CS 2009 50 Critters  demo:  Critter  ChameleonCritter (testable code)  CrabCritter (extra demo code)

51 AP CS 2009 51 act method public void act() { if (getGrid() == null) return; ArrayList actors = getActors(); processActors(actors); ArrayList moveLocs = getMoveLocations(); Location loc = selectMoveLocation(moveLocs); makeMove(loc); }

52 AP CS 2009 52 getActors method public ArrayList getActors() Gets the actors for processing. Implemented to return the actors that occupy the neighboring grid locations. Override this method for subclasses that look elsewhere for actors to process. Returns a list of actors that are neighbors of this critter. Postcondition: The state of all actors is unchanged.

53 AP CS 2009 53 processActors method public void processActors(ArrayList actors) Processes the actors. This method is implemented to "eat" (i.e. remove) all actors that are not rocks or critters. Override it in subclasses that process neighbors in a different way. Postconditions: (1) The state of all actors in the grid other than this critter and the elements of actors is unchanged. (2) The location of this critter is unchanged.

54 AP CS 2009 54 getMoveLocations method public ArrayList getMoveLocations() Get the possible locations for the next move. Implemented to return the empty neighboring locations. Override this method for subclasses that look elsewhere for move locations. Returns a list of possible locations for the next move. Postcondition: The state of all actors is unchanged.

55 AP CS 2009 55 selectMoveLocation method public Location selectMoveLocation(ArrayList locs) Selects the location for the next move. Implemented to randomly pick one of the possible locations, or to return the current location if locs has size 0. Override this method for subclasses that have another mechanism for selecting the next move location. Postconditions: (1) The returned location is an element of locs, this critter's current location, or null. (2) The state of all actors is unchanged.

56 AP CS 2009 56 makeMove method public void makeMove(Location loc) Moves this critter to the given location loc, or removes this critter from its grid if loc is null. An actor may be added to the old location. If there is a different actor at location loc, that actor is removed from the grid. Override this method in subclasses that want to carry out other actions (for example, turning this critter or adding an occupant in its previous location). Postconditions: (1) getLocation() == loc. (2) The state of all actors other than those at the old and new locations is unchanged.

57 AP CS 2009 57  Override one or more of these methods when creating a new Critter subclass.  The Narrative states: It is usually not a good idea to override the act method in a Critter subclass. The Critter class was designed to represent actors that process other actors and then move. If you find the act method unsuitable for your actors, you should consider extending Actor, not Critter.

58 AP CS 2009 58 Design issues The postconditions help to ensure that subclasses implement behavior that is consistent with the purpose of each piece.

59

One thought on “Bug Source Code Grid World Case Study Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *