import java.util.*;

/**
 * A map of the tunnel system.
 */

public class Map
{
    public Map (Vector<String> input, boolean debug)
    {
        _theMap = new Vector<Cell>();
        _keys = new Vector<Character>();
        _doors = new Vector<Character>();
        _debug = debug;

        createMap(input);
    }

    public Coordinate getEntrance ()
    {
        return _entrance;
    }

    /**
     * Return the content of the Cell represented by the Coordinate.
     */

    public char getContent (Coordinate coord)
    {
        int index = _theMap.indexOf(new Cell(coord));
        Cell theCell = _theMap.get(index);

        return theCell.getContents();  // assume no error
    }

    /*
     * Is this Coordinate in range and can be moved into?
     */

    public boolean validPosition (Coordinate coord)
    {
        if ((coord.getX() >= 0) && (coord.getY() >= 0))
        {
            if ((coord.getX() < _maxX) && (coord.getY() < _maxY))
            {
                int index = _theMap.indexOf(new Cell(coord));

                if (index != -1)
                {
                    Cell theCell = _theMap.get(index);

                    if (theCell != null)
                        return !theCell.isWall();
                }
            }
        }

        return false;
    }

    public int numberOfKeys ()
    {
        return _keys.size();
    }

    public int numberOfDoors ()
    {
        return _doors.size();
    }

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

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

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

                index = 1;
            }
        }

        return str;
    }

    // create the map from input
    
    private void createMap (Vector<String> input)
    {
        Enumeration<String> iter = input.elements();
        int y = -1;

        _maxX = input.get(0).length(); // all lines are the same length (check?)

        while (iter.hasMoreElements())
        {
            y++;

            String line = iter.nextElement();

            for (int x = 0; x < _maxX; x++)
            {
                Cell theCell = new Cell(new Coordinate(x, y), line.charAt(x));

                if (theCell.isEntrance())
                    _entrance = theCell.position();  // should be only one!
                else
                {
                    if (theCell.isKey())
                        _keys.add(theCell.getContents());
                    else
                    {
                        if (theCell.isDoor())
                            _doors.add(theCell.getContents());
                    }
                }

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

                _theMap.add(theCell);
            }
        }

        _maxY = y+1;
    }

    private Vector<Cell> _theMap;
    private int _maxX;
    private int _maxY;
    private Coordinate _entrance;
    private Vector<Character> _keys;
    private Vector<Character> _doors;
    private boolean _debug;
}
@@ -49,16 +49,47 @@
 system.%0A
+ * %0A * Split it into sub-maps?%0A
  */%0A%0Apub
@@ -101,17 +101,17 @@
 lass Map
-%0A
+ 
 %7B%0A    pu
@@ -118,17 +118,16 @@
 blic Map
- 
 (Vector%3C
@@ -155,20 +155,16 @@
 n debug)
-%0A   
  %7B%0A     
@@ -370,23 +370,18 @@
 Entrance
- 
 ()
-%0A   
  %7B%0A     
@@ -520,17 +520,16 @@
 tContent
- 
 (Coordin
@@ -530,36 +530,32 @@
 oordinate coord)
-%0A   
  %7B%0A        int i
@@ -676,17 +676,16 @@
 tents();
- 
  // assu
@@ -809,17 +809,16 @@
 Position
- 
 (Coordin
@@ -827,20 +827,16 @@
 e coord)
-%0A   
  %7B%0A     
@@ -885,24 +885,16 @@
 ) %3E= 0))
-%0A       
  %7B%0A     
@@ -953,28 +953,16 @@
  _maxY))
-%0A           
  %7B%0A     
@@ -1051,32 +1051,16 @@
 x != -1)
-%0A               
  %7B%0A     
@@ -1300,23 +1300,18 @@
 erOfKeys
- 
 ()
-%0A   
  %7B%0A     
@@ -1369,23 +1369,18 @@
 rOfDoors
- 
 ()
-%0A   
  %7B%0A     
@@ -1455,15 +1455,10 @@
 ring
- 
 ()
-%0A   
  %7B%0A 
@@ -1564,26 +1564,34 @@
 p %3C%22
-+
+ + 
 _maxX
-+
+ + 
 %22, %22
-+
+ + 
 _maxY
-+
+ + 
 %22%3E:%5C
@@ -1631,32 +1631,24 @@
 eElements())
-%0A       
  %7B%0A         
@@ -1725,28 +1725,16 @@
 = _maxX)
-%0A           
  %7B%0A     
@@ -1870,20 +1870,16 @@
 m input%0A
-    
 %0A    pri
@@ -1897,17 +1897,16 @@
 reateMap
- 
 (Vector%3C
@@ -1919,20 +1919,16 @@
 %3E input)
-%0A   
  %7B%0A     
@@ -2116,24 +2116,16 @@
 ments())
-%0A       
  %7B%0A     
@@ -2227,28 +2227,16 @@
 xX; x++)
-%0A           
  %7B%0A     
@@ -2403,17 +2403,16 @@
 ition();
- 
  // shou
@@ -2447,32 +2447,16 @@
     else
-%0A               
  %7B%0A     
@@ -2573,36 +2573,16 @@
     else
-%0A                   
  %7B%0A     
@@ -2884,17 +2884,19 @@
 maxY = y
-+
+ + 
 1;%0A    %7D

Update Map.java