Skip to content

Instantly share code, notes, and snippets.

@mathur
Last active December 15, 2015 19:29
Show Gist options
  • Save mathur/5311998 to your computer and use it in GitHub Desktop.
Save mathur/5311998 to your computer and use it in GitHub Desktop.
/*
* AP(r) Computer Science GridWorld Case Study:
* Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @author Chris Nevison
* @author Barbara Cloud Wells
* @author Cay Horstmann
*/
import info.gridworld.grid.Grid;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
import java.awt.Color;
import java.util.*;
public class BusCritter extends Critter
{
private int moveCounter = 0;
public void processActors(ArrayList<Actor> neighbors)
{
Grid<Actor> grid = getGrid();
for (Actor a: neighbors)
{
if (a instanceof Note){
if (a.getLocation() != this.getLocation().getAdjacentLocation(this.getDirection()) || a.getLocation() != this.getLocation().getAdjacentLocation(this.getDirection() - 180))
grid.remove(a.getLocation());
}
}
}
/**
* Turns towards the new location as it moves.
*/
public void makeMove(Location loc)
{
setDirection(getLocation().getDirectionToward(loc));
super.makeMove(loc);
moveCounter++;
dropSmiley();
}
public void dropSmiley()
{
Grid<Actor> gr = getGrid();
Location loc = new Location(getLocation().getRow() - 1, getLocation().getCol());
Smiley smiley = new Smiley();
if(moveCounter % 3 == 0){
if (gr.isValid(loc)){
smiley.putSelfInGrid(gr, loc);
}
}
}
}
/*
* AP(r) Computer Science GridWorld Case Study:
* Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @author Chris Nevison
* @author Barbara Cloud Wells
* @author Cay Horstmann
*/
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import java.awt.Color;
/**
* This class runs a world that contains chameleon critters. <br />
* This class is not tested on the AP CS A and AB exams.
*/
public class BusRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
world.add(new Location(7, 8), new BusCritter());
world.add(new Location(3, 3), new Smiley());
world.add(new Location(5, 4), new Note());
world.show();
}
}
/**
* Write a description of class Smiley here.
*
* @author (your name)
* @version (a version number or a date)
*/
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;
import java.awt.Color;
import java.util.ArrayList;
public class Note extends Actor
{
public void act()
{
}
}
/**
* Write a description of class Smiley here.
*
* @author (your name)
* @version (a version number or a date)
*/
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;
import java.awt.Color;
import java.util.ArrayList;
public class Smiley extends Actor
{
private int actCounter = 0;
private void lighten()
{
Color c = getColor();
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
if(red < 255)
red = red + 30;
if(green < 255)
green = green + 30;
if(blue < 255)
blue = blue + 30;
setColor(new Color(red, green, blue));
}
public void act()
{
if (actCounter > 6)
{
Grid<Actor> gr = getGrid();
Location loc = getLocation();
Note note = new Note();
note.putSelfInGrid(gr, loc);
}
actCounter++;
lighten();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment