import java.util.*;
import java.io.*;

public class Grid
{
    public static final int DEFAULT_WIDTH = 5;
    public static final int DEFAULT_HEIGHT = 5;

    public Grid (String fileName, boolean debug)
    {
        this(DEFAULT_HEIGHT, DEFAULT_WIDTH, fileName, debug);
    }

    public Grid (int height, int width, String fileName, boolean debug)
    {
        _height = height;
        _width = width;
        _debug = debug;

        loadWorld(fileName);
    }

    /**
     * Each minute, The bugs live and die based on the number of bugs in the four adjacent tiles:
     *
     * - A bug dies (becoming an empty space) unless there is exactly one bug adjacent to it.
     * - An empty space becomes infested with a bug if exactly one or two bugs are adjacent to it.
     * 
     * Otherwise, a bug or empty space remains the same. (Tiles on the edges of the grid have fewer than
     * four adjacent tiles; the missing tiles count as empty space.) This process happens in every location
     * simultaneously; that is, within the same minute, the number of adjacent bugs is counted for every tile
     * first, and then the tiles are updated.
     */

    public void evolve ()
    {
        Tile[][] _nextWorld = new Tile[_height][_width];
        
        for (int i = 0; i < _height; i++)
        {
            for (int j = 0; j < _width; j++)
            {
                int adjacentBugs = 0;
                int emptySpaces = 0;

                // ignore range checking and rely on exception!

                if (_debug)
                    System.out.println("Checking tile < "+i+", "+j+" > : "+_theWorld[i][j]);

                try
                {
                    if (adjacentBug(i-1, j))
                        adjacentBugs++;
                    else
                        emptySpaces++;

                    if (adjacentBug(i+1, j))
                        adjacentBugs++;
                    else
                        emptySpaces++;

                    if (adjacentBug(i, j-1))
                        adjacentBugs++;
                    else
                        emptySpaces++;

                    if (adjacentBug(i, j+1))
                        adjacentBugs++;
                    else
                        emptySpaces++;
                }
                catch (IndexOutOfBoundsException ex)
                {
                    emptySpaces++;
                }

                if (_debug)
                {
                    System.out.println("Empty spaces: "+emptySpaces);
                    System.out.println("Adjacent bugs: "+adjacentBugs);
                }

                if (_theWorld[i][j].isBug())
                {
                    if (adjacentBugs == 1)
                        _nextWorld[i][j] = new Tile(TileId.BUG);
                    else
                        _nextWorld[i][j] = new Tile(TileId.EMPTY_SPACE);
                }
                else
                {
                    if ((adjacentBugs == 1) || (adjacentBugs == 2))
                        _nextWorld[i][j] = new Tile(TileId.BUG);
                    else
                        _nextWorld[i][j] = new Tile(TileId.EMPTY_SPACE);
                }
            }
        }

        _theWorld = _nextWorld;
    }

    @Override
    public String toString ()
    {
        String str = "";

        for (int i = 0; i < _height; i++)
        {
            for (int j = 0; j < _width; j++)
                str += _theWorld[i][j];
            
            str += "\n";
        }

        return str;
    }

    private boolean adjacentBug (int i, int j) throws IndexOutOfBoundsException
    {
        if (_theWorld[i][j].isBug())
            return true;
        else
            return false;
    }

    private void loadWorld (String inputFile)
    {
        BufferedReader reader = null;
        int row = 0;

        _theWorld = new Tile[_height][_width];

        try
        {
            reader = new BufferedReader(new FileReader(inputFile));
            String line = null;

            while ((line = reader.readLine()) != null)
            {
                for (int i = 0; i < line.length(); i++)
                {
                    if (TileId.valid(line.charAt(i)))
                        _theWorld[row][i] = new Tile(line.charAt(i));
                    else
                        System.out.println("Invalid world entry: "+line.charAt(i));
                }

                row++;
            }
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch (Throwable ex)
            {
            }
        }
    }

    private Tile[][] _theWorld;
    private int _height;
    private int _width;
    private boolean _debug;
}
@@ -2699,34 +2699,265 @@
     if (
-adjacentBugs == 1)
+_debug)%0A                        System.out.println(%22Tile contains bug.%22);%0A%0A                    if (adjacentBugs == 1)%0A                    %7B%0A                        if (_debug)%0A                            System.out.println(%22New tile will be bug.%22);%0A
 %0A       
@@ -3006,32 +3006,54 @@
 le(TileId.BUG);%0A
+                    %7D%0A
                 
@@ -3053,32 +3053,166 @@
            else%0A
+                    %7B%0A                        if (_debug)%0A                            System.out.println(%22New tile will be empyy.%22);%0A%0A
                 
@@ -3260,32 +3260,54 @@
 d.EMPTY_SPACE);%0A
+                    %7D%0A
                 
@@ -3375,51 +3375,278 @@
 if (
-(adjacentBugs == 1) %7C%7C (adjacentBugs == 2))
+_debug)%0A                        System.out.println(%22Tile is empty.%22);%0A%0A                    if ((adjacentBugs == 1) %7C%7C (adjacentBugs == 2))%0A                    %7B%0A                        if (_debug)%0A                            System.out.println(%22New tile will be bug.%22);%0A
 %0A   
@@ -3699,32 +3699,54 @@
 le(TileId.BUG);%0A
+                    %7D%0A
                 
@@ -3746,32 +3746,166 @@
            else%0A
+                    %7B%0A                        if (_debug)%0A                            System.out.println(%22New tile will be empty.%22);%0A%0A
                 
@@ -3953,32 +3953,54 @@
 d.EMPTY_SPACE);%0A
+                    %7D%0A
                 

Update Grid.java