Capture The Flag

<< BlackHoleCritter | GridworldTrailIndex | AntFarm >>

Watch a movie of a CaptureTheFlag game


There are two Flags, one red one blue. Flag is a subclass of Actor, and overrides the act() method. It looks for all the occupied locations of the grid, and if there are less than 2 flags, it means it is the winner, and proceeds to remove all Actors that are not the same color as itself.

Each team will start with the same number of AttackCritters each starting with a strength of 5. When they are initialized, they store the Color of its target. They move randomly like a Critter, but if they have a neighbor of the same color, their strength goes up. If they have a neighbor of the other color, their strength does down. If a neighbor is a Flag of their enemy's color, they remove it from the grid. If their strength is less than 1, they remove themselves from the grid (or for a less violent game, you can have them switch teams!).

import java.awt.Color;
import info.gridworld.grid.*;
public class CaptureTheFlag {

	public static void main(String[] args) {

		ActorWorld battleField = new ActorWorld();
		battleField.setGrid(new BoundedGrid(10, 19));
		battleField.add(new Location(4,18), new Flag(Color.RED));
		battleField.add(new Location(4,0), new Flag(Color.BLUE));
		battleField.add(new Location(5,0), new Rock());
		battleField.add(new Location(5,18), new Rock());
		battleField.add(new Location(4,1), new Rock());
		battleField.add(new Location(4,17), new Rock());
		battleField.add(new Location(5,1), new Rock());
		battleField.add(new Location(5,17), new Rock());

		for (int i=0;i<5;i++){
			battleField.add(new Location(2+i, 8),new AttackCritter(Color.BLUE, Color.RED));
			battleField.add(new Location(2+i, 10),new AttackCritter(Color.RED, Color.BLUE));



  1. Make accessor methods (getter and setter methods) to strength so you can test and manipulate the AttackCritters
  2. Be mindful of the post conditions for Critters! If you are changing the state of an Actor, it can only be in processActors or in makeMove.