import java.util.*;

/**
 * Take an image from the camera and turn it into a map
 * which can be used to move around.
 */

public class Map
{
    public Map (Image img, boolean debug)
    {
        _theMap = new Vector<Cell>();
        _maxX = 0;
        _maxY = 0;
        _originalImage = img;
        _debug = debug;

        createMapFromImage(img.scannedLines());
    }

    public Map (Map toCopy)
    {
        _theMap = new Vector<Cell>();
        _maxX = 0;
        _maxY = 0;
        _originalImage = toCopy._originalImage;
        _debug = toCopy._debug;

        createMapFromImage(_originalImage.scannedLines());
    }

    public Coordinate findStartingPoint ()
    {
        Enumeration<Cell> iter = _theMap.elements();

        while (iter.hasMoreElements())
        {
            Cell cur = iter.nextElement();

            if (cur.getContents().equals(CellId.ROBOT_FACING_UP))
                return cur.position();
        }

        return null;
    }

    public boolean isScaffold (Coordinate coord)
    {
        if (!valid(coord))
            return false;

        Cell temp = new Cell(coord);
        int index = _theMap.indexOf(temp);

        temp = _theMap.get(index);

        return (temp.getContents().equals(CellId.SCAFFOLDING));
    }

    public boolean theEnd (Coordinate coord)
    {
        Coordinate nCoord = new Coordinate(coord.getX(), coord.getY()+1);
        Coordinate sCoord = new Coordinate(coord.getX(), coord.getY()-1);
        Coordinate eCoord = new Coordinate(coord.getX()+1, coord.getY());
        Coordinate wCoord = new Coordinate(coord.getX()-1, coord.getY());

        if (valid(nCoord) && (!isScaffold(nCoord)))
        {
            if (valid(sCoord) && (!isScaffold(sCoord)))
            {
                if (valid(eCoord) && (!isScaffold(eCoord)))
                {
                    if (valid(wCoord) && (!isScaffold(wCoord)))
                        return true;
                }
            }
        }

        return false;
    }

    @Override
    public String toString ()
    {
        Enumeration<Cell> iter = _theMap.elements();
        int index = 1;
        String str = "Map dimensions < "+_maxX+", "+_maxY+" >\n";

        while (iter.hasMoreElements())
        {
            str += iter.nextElement().getContents();

            if (index++ == _maxX)
            {
                str += "\n";

                index = 1;
            }
        }

        return str;
    }

    protected boolean valid (Coordinate coord)
    {
        System.out.println("Comparing "+coord+" and "+_maxX+" "+_maxY);

        if (coord != null)
        {
            if ((coord.getX() >= 0) && (coord.getY() >= 0))
            {
                if ((coord.getX() < _maxX) && (coord.getY() < _maxY))
                    return true;
            }
        }

        return false;
    }

    protected void createMapFromImage (String[] lines)
    {
        int lineLength = lines[0].length(); // all lines are the same length

        _maxY = lines.length -1;
        _maxX = lineLength;

        for (int y = 0; y < lines.length -1; y++)
        {
            for (int x = 0; x < lineLength; x++)
            {
                Cell theCell = new Cell(new Coordinate(x, y), String.valueOf(lines[y].charAt(x)), _debug);

                if (_debug)
                    System.out.println(theCell);

                _theMap.add(theCell);
            }
        }
    }

    protected Map ()
    {
        _theMap = new Vector<Cell>();
        _maxX = 0;
        _maxY = 0;
        _originalImage = null;
        _debug = false;
    }

    protected Vector<Cell> _theMap;
    protected int _maxX;
    protected int _maxY;
    protected Image _originalImage;
    protected boolean _debug;
}
@@ -1286,39 +1286,368 @@
 ean 
-theEnd (Coordinate coord)%0A    %7B
+isOpenSpace (Coordinate coord)%0A    %7B%0A        if (!valid(coord))%0A            return false;%0A%0A        Cell temp = new Cell(coord);%0A        int index = _theMap.indexOf(temp);%0A%0A        temp = _theMap.get(index);%0A%0A        return (temp.getContents().equals(CellId.OPEN_SPACE));%0A    %7D%0A%0A    public boolean theEnd (Coordinate coord)%0A    %7B%0A        int numberOfExits = 0;%0A
 %0A   
@@ -1956,37 +1956,20 @@
 if (
-valid(nCoord) && (!isScaffold
+!isOpenSpace
 (nCo
@@ -1969,25 +1969,24 @@
 ace(nCoord))
-)
 %0A        %7B%0A 
@@ -1974,33 +1974,52 @@
 Coord))%0A        
-%7B
+    numberOfExits++;
 %0A            if 
@@ -2015,45 +2015,33 @@
     
+%0A
     
-if (valid(sCoord) && (!isScaffold
+    if (!isOpenSpace
 (sCo
@@ -2041,25 +2041,24 @@
 ace(sCoord))
-)
 %0A           
@@ -2050,33 +2050,48 @@
 d))%0A            
-%7B
+numberOfExits++;
 %0A               
@@ -2075,32 +2075,33 @@
 xits++;%0A        
+%0A
         if (vali
@@ -2100,37 +2100,20 @@
 if (
-valid(eCoord) && (!isScaffold
+!isOpenSpace
 (eCo
@@ -2113,25 +2113,24 @@
 ace(eCoord))
-)
 %0A           
@@ -2134,67 +2134,50 @@
     
-    %7B%0A                    if (valid(wCoord) && (!isScaffold
+numberOfExits++;%0A%0A        if (!isOpenSpace
 (wCo
@@ -2181,17 +2181,16 @@
 wCoord))
-)
 %0A       
@@ -2198,96 +2198,141 @@
     
-            return true;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return false
+numberOfExits++;%0A%0A        System.out.println(%22Number of exits for %22+coord+%22 is %22+numberOfExits);%0A%0A        return (numberOfExits == 1)
 ;%0A  

Update Map.java