import java.awt.*; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.util.Random; public abstract class AbstractMap { private IDrawningObject _drawningObject = null; protected int[][] _map = null; protected int _width; protected int _height; protected float _size_x; protected float _size_y; protected final Random _random = new Random(); protected final int _freeRoad = 0; protected final int _barrier = 1; public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject){ _width = width; _height = height; _drawningObject = drawningObject; GenerateMap(); while (!SetObjectOnMap()) { GenerateMap(); } return DrawMapWithObject(); } public BufferedImage MoveObject(Direction direction) { int _startX = (int) (_drawningObject.GetCurrentPosition()[3] / _size_x); int _startY = (int)(_drawningObject.GetCurrentPosition()[0] / _size_y); int _objWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); int _objHeight = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); boolean isMove = true; switch (direction) { case Right: for (int i = _objWidth; i <= _objWidth + (int)(_drawningObject.getStep() / _size_x); i++) { for (int j = _startY; j <= _objHeight; j++) { if (_map[i][j] == _barrier) { isMove = false; break; } } } break; case Left: for (int i = _startX; i >= (int)(_drawningObject.getStep() / _size_x); i--) { for (int j = _startY; j <= _objHeight; j++) { if (_map[i][j] == _barrier) { isMove = false; break; } } } break; case Up: for (int i = _startX; i <= _objWidth; i++) { for (int j = _startY; j >= (int)(_drawningObject.getStep() / _size_y); j--) { if (_map[i][j] == _barrier) { isMove = false; break; } } } break; case Down: for (int i = _startX; i <= _objWidth; i++) { for (int j = _objHeight; j <= _objHeight + (int)(_drawningObject.getStep() / _size_y); j++) { if (_map[i][j] == _barrier) { isMove = false; break; } } } break; } if (isMove) { _drawningObject.MoveObject(direction); } return DrawMapWithObject(); } private boolean SetObjectOnMap() { if (_drawningObject == null || _map == null) { return false; } int x = _random.nextInt(0, 10); int y = _random.nextInt(0, 10); _drawningObject.SetObject(x, y, _width, _height); // TODO првоерка, что объект не "накладывается" на закрытые участки for (int i = 0; i < _map.length; ++i) { for (int j = 0; j < _map[0].length; ++j) { if (i * _size_x >= x && j * _size_y >= y && i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] && j * _size_y <= j + _drawningObject.GetCurrentPosition()[2]) { if (_map[i][j] == _barrier) { return false; } } } } return true; } private BufferedImage DrawMapWithObject() { BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); if (_drawningObject == null || _map == null) { return bmp; } Graphics gr = bmp.getGraphics(); for (int i = 0; i < _map.length; i++) { for (int j = 0; j < _map[0].length; j++) { if (_map[i][j] == _freeRoad) { DrawRoadPart(gr, i, j); } else if (_map[i][j] == _barrier) { DrawBarrierPart(gr, i, j); } } } _drawningObject.DrawningObject(gr); return bmp; } protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); }