/// <summary> /// Determine the starting position for the specified entity in the specified forest. /// </summary> /// <param name="forest"></param> /// <param name="entity"></param> protected virtual ForestEntity DetermineStartLocation(Forest forest, ForestEntity entity) { // Probably not the best algorithm.. potential for infinite loop if the forest is // already full. do { int x = r.Next(forest.Width), y = r.Next(forest.Height); // Look for an existing entity in the chosen random position. ForestEntity existingInPosition = forest.GetEntityAtPosition(x, y); if (existingInPosition == null || existingInPosition.IsEmpty) { // If position is empty, assign it and return. entity.Location = new System.Drawing.Point(x, y); return(entity); } }while (true); }
/// <summary> /// Draw the forest to the specified graphics surface. /// </summary> /// <param name="g"></param> public void Draw(Graphics g) { float pX = 0, pY = 0; long monthsRun = _forest.GetMonths(), yearsRun = monthsRun / Forest.YEAR_LENGTH; monthsRun = monthsRun % Forest.YEAR_LENGTH + 1; string timeMsg = string.Format("Year: {0}, Month: {1}", yearsRun, monthsRun); SizeF txtSize = g.MeasureString(timeMsg, _txtFont); g.DrawString(timeMsg, _txtFont, Brushes.Black, pX, pY); pY += txtSize.Height; for (int x = 0; x < _forest.Width; x++) { for (int y = 0; y < _forest.Height; y++) { Color c; ForestEntity entity = _forest.GetEntityAtPosition(x, y); c = entity.Color; RectangleF bounds = new RectangleF( pX + x * SQUARE_SCALE, pY + y * SQUARE_SCALE, 1 * SQUARE_SCALE, 1 * SQUARE_SCALE); using (SolidBrush brush = new SolidBrush(c)) g.FillRectangle(brush, bounds); g.DrawRectangle(Pens.Black, bounds.X, bounds.Y, bounds.Width, bounds.Height); } } }