Tiled map introduction.

In this introduction we shall outline the case study scenario and outline requirements together with a simple model.

The scenario and requirements

In this case study we require to model a map of some arbitrary rectangular area. This map should be accessible by (x,y) coordinates with the bottom left of the map considered to be at (0,0). Each coordinate position is modelled by a tile representing a square area of the overall physical area of the map. The only measurement units used in the map are tile position coordinates, the actual physical size of each tile, and hence of the map, being irrelevant to the model. thus the resolution of the map is determined by the size of the area that each tile represents and the number of tiles making up the map.

For example, a room may be modelled by a 10x5 grid of tiles each representing a square metre; or by a 1,000x500 grid of tiles each representing a square centimetre. Similarly, a map might model a garden, town, country, or continent with the number and size of tiles determining the resolution of the model. The physical tile size and hence resolution of a map is determined by the client of the map model; the map model itself is only concerned with tiles and the units of coordinates are in terms of tiles only.

Each tile indicates the type of terrain at its position in the map, the entire tile area is considered to be made up of that terrain type. The term "terrain" may be used to cover such types as ...

we call this the terrain context of the map. A client application should be able to create different maps modelling different areas but within one specific terrain context. However, since code reuse is a central aspect of good software engineering, it is expected that the map model is easily modifiable to cope with different terrain contexts for other applications. For example, an application might be written to find a path through different maps in an outdoor context with grass, water, etc.; this application should require only minimal changes to be able to find a path through a computer room with chairs, tables, etc.

In using the map model, a client application should be able to ...

  1. create a map of any size;
  2. retrieve the size of the map;
  3. model different terrain contexts;
  4. initialize the map to some specific terrain type from the context;
  5. get and set the terrain type for a particular (x,y) tile;
  6. render the map to some output device;
  7. allow a border to be set to a terrain type;
  8. set subareas of the map to a specific terrain type, perhaps randomly;

As the case study progresses, aspects of the above requirements will be presented and diffferent approaches discussed. However at this stage we will introduce a very basic model in the simplest terrain context.



Modelling a basic map.

Consider the simplest terrain context where the terrain type is either passable or impassable. This can be modelled by tile values of true or false. Thus the overall map can be modelled by a 2-D array of boolean values.

   boolean[][] map;

Note that we must be watchful of the different interpretations of the dimensions of the 2D array as opposed to that of the map coordinates. The former sees these as rows and columns but the latter sees the first index being the x coordinate, i.e. the column number, with the second being the row. It doesn't really matter as long as a consistent interpretation is used throughout your application.

To start work on this case study let us first just get back into Java by creating a program that defines a simple map and then exercises it, we will not concern ourselves with the overall model requirements at this stage.

For this program, map a space where all we are interested in is whether or not the tiles are passable or not.

  1. Model the map by declaring a 2-D array of booleans.
    • true will indicate that a tile is passable and false that it is impassable.
  2. Initialize the map to represent a grass lawn;
    • this is a 20x15 area (including the hedge border),
    • with a 1 tile wide impassable hedge around the perimeter,
    • except for a 5 tile wide entrance gap in the middle of the bottom edge of the lawn.
  3. Calculate and display the percentage of passable area in the map.
  4. Render the map on the console by displaying the rows and columns of tiles, using the character '.' to represent the grass tiles and 'H' to represent the hedge tiles.
    • ensure that your rendered map is oriented correctly to the user; i.e. the rows and columns are in the right order with the map being shown the correct way up.
  5. Amend the program to allow the user to enter the dimensions of the lawn area, the width of the hedge and the width of the entrance.
  6. If you have not done so in answering the previous questions, amend the application to use an object-oriented approach.
    • Encapsulate the base map behaviour in the TiledMap1 class
    • In the main application, instantiate an object of this class and exercise it as above.