public LightCycle(Spiral spiral) { Side = Side.Top; Position = spiral.Origin; Spiral = spiral; NextValue = 0; }
private int DrawLine(Spiral spiral, int nextValue, Point start, Point end) { if (start.X < end.X) { for (int i = start.X; i <= end.X; i++) { spiral.SetValueRelative(i, start.Y, nextValue++); } } else if (start.X > end.X) { for (int i = start.X; i >= end.X; i--) { spiral.SetValueRelative(i, start.Y, nextValue++); } } if (start.Y < end.Y) { for (int i = start.Y; i <= end.Y; i++) { spiral.SetValueRelative(start.X, i, nextValue++); } } else if (start.Y > end.Y) { for (int i = start.Y; i >= end.Y; i--) { spiral.SetValueRelative(start.X, i, nextValue++); } } return nextValue; }
private void DrawRight(Spiral spiral, int nextValue, int y, int startX, int endX) { for (int x = startX; x <= endX; x++) { spiral.SetValueRelative(x, y, nextValue++); } }
private void DrawShell(Spiral spiral, int shellIndex) { int nextValue = DrawDown(spiral, GetShellStartingValue(shellIndex), shellIndex, -shellIndex + 1, shellIndex); nextValue = DrawLeft(spiral, nextValue, shellIndex, shellIndex - 1, -shellIndex); nextValue = DrawUp(spiral, nextValue, -shellIndex, shellIndex - 1, -shellIndex); DrawRight(spiral, nextValue, -shellIndex, -shellIndex + 1, shellIndex); }
private void DrawRectangle(Spiral spiral, int firstValue, Rectangle shellCorners) { int nextValue = DrawLine(spiral, firstValue, new Point(shellCorners.Right, shellCorners.Top + 1), new Point(shellCorners.Right, shellCorners.Bottom)); nextValue = DrawLine(spiral, nextValue, new Point(shellCorners.Right - 1, shellCorners.Bottom), new Point(shellCorners.Left, shellCorners.Bottom)); nextValue = DrawLine(spiral, nextValue, new Point(shellCorners.Left, shellCorners.Bottom - 1), new Point(shellCorners.Left, shellCorners.Top)); DrawLine(spiral, nextValue, new Point(shellCorners.Left + 1, shellCorners.Top), new Point(shellCorners.Right, shellCorners.Top)); }
private Spiral PopulateSpiral(Spiral spiral) { for (int shellIndex = 1; shellIndex < spiral.Size/2 + 1; shellIndex++) { new Shell(shellIndex).DrawOn(spiral); } return spiral; }
private Spiral PopulateSpiral(Spiral spiral) { for (int shellIndex = 1; shellIndex <= spiral.Size / 2; shellIndex++) { DrawShell(spiral, shellIndex); } return spiral; }
private int DrawUp(Spiral spiral, int nextValue, int x, int startY, int endY) { for (int y = startY; y >= endY; y--) { spiral.SetValueRelative(x, y, nextValue++); } return nextValue; }
private int DrawLeft(Spiral spiral, int nextValue, int y, int startX, int endX) { for (int x = startX; x >= endX; x--) { spiral.SetValueRelative(x, y, nextValue++); } return nextValue; }
private void DrawShell(Spiral spiral, int shellIndex) { int shellDimension = shellIndex*2 + 1; int previousShellDimension = (shellIndex - 1)*2 + 1; int nextValue = previousShellDimension * previousShellDimension; Rectangle shellCorners = new Rectangle(-shellIndex, -shellIndex, shellDimension - 1, shellDimension - 1); DrawRectangle(spiral, nextValue, shellCorners); }
private Spiral PopulateSpiral(Spiral spiral) { Spark spark = new Spark(Side.Top, spiral.Origin, 0, 0, spiral); for (int i = 0; i < spiral.Numbers.Length; i++) { spark.Advance(); } return spiral; }
public Spark(Side side, Point position, int sideLength, int drawnThisSide, Spiral spiral) { Side = side; Position = position; SideLength = sideLength; AmountDrawnThisSide = drawnThisSide; Spiral = spiral; NextValue = 0; }
private Spiral PopulateSpiral(Spiral spiral) { LightCycle lightCycle = new LightCycle(spiral); for (int i = 0; i < spiral.Numbers.Length; i++) { lightCycle.DriveClockwise(); } return spiral; }
public void DoesNotRenderNegativeOne() { Spiral spiral = new Spiral(new[,] { {-1, -1, -1}, {-1, 0, -1}, {-1, -1, -1} }, 0); _renderer.Render(spiral, _textWriter); Assert.That(_textWriter.ToString(), Is.Not.StringContaining("-1")); }
public void Render(Spiral spiral, TextWriter outWriter) { int places = Math.Max(0, (int)Math.Floor(Math.Log10(spiral.SpiralTo))) + 1; for (int row = 0; row < spiral.Size; row++) { for (int column = 0; column < spiral.Size; column++) { if (column != 0) { outWriter.Write(" "); } outWriter.Write(FormatForDisplay(spiral.Numbers[row, column], places)); } outWriter.WriteLine(); } }
public void Render(Spiral spiral, TextWriter outWriter) { for (int row = 0; row < spiral.Size; row++) { outWriter.Write("<tr>"); for (int column = 0; column < spiral.Size; column++) { int value = spiral.Numbers[row, column]; outWriter.Write(string.Format("<td>{0}</td>", ((value == -1) ? " " : value.ToString(CultureInfo.InvariantCulture)))); } outWriter.Write("</tr>"); } }
public void RendersSpacesBetweenNumbers() { Spiral spiral = new Spiral(new[,] { {-1, -1, -1}, {-1, 0, 1}, {-1, -1, -1} }, 0); _renderer.Render(spiral, _textWriter); Assert.That(_textWriter.ToString(), Is.EqualTo( " " + Environment.NewLine + " 0 1" + Environment.NewLine + " " + Environment.NewLine)); }
public void DoesNotRenderExtraSpacesIfTwoDigitNumbersAreNotPresent() { Spiral spiral = new Spiral(new[,] { {-1, -1, -1, -1, -1}, {-1, 6, 7, 8, 9}, {-1, 5, 0, 1, -1}, {-1, 4, 3, 2, -1}, {-1, -1, -1, -1, -1} }, 9); _renderer.Render(spiral, _textWriter); Assert.That(_textWriter.ToString(), Is.EqualTo( " " + Environment.NewLine + " 6 7 8 9" + Environment.NewLine + " 5 0 1 " + Environment.NewLine + " 4 3 2 " + Environment.NewLine + " " + Environment.NewLine)); }
public void RendersExtraSpacesIfTwoDigitNumbersArePresent() { Spiral spiral = new Spiral(new[,] { {-1, -1, -1, -1, -1}, {-1, 6, 7, 8, 9}, {-1, 5, 0, 1, 10}, {-1, 4, 3, 2, 11}, {-1, -1, 14, 13, 12} }, 14); _renderer.Render(spiral, _textWriter); Assert.That(_textWriter.ToString(), Is.EqualTo( " " + Environment.NewLine + " 6 7 8 9" + Environment.NewLine + " 5 0 1 10" + Environment.NewLine + " 4 3 2 11" + Environment.NewLine + " 14 13 12" + Environment.NewLine)); }
public void Render(Spiral spiral, TextWriter outWriter) { outWriter.WriteLine("new[,]"); outWriter.WriteLine("{"); int lastRowOrColumn = spiral.Size - 1; for (int row = 0; row < spiral.Size; row++) { outWriter.Write("{"); for (int column = 0; column < spiral.Size; column++) { outWriter.Write(spiral.Numbers[row, column]); if (column < lastRowOrColumn) { outWriter.Write(", "); } } outWriter.WriteLine((row < lastRowOrColumn) ? "}," : "}"); } outWriter.WriteLine("}"); }
public void ShouldGenerateAndRenderSpirals() { WriteToInput("3\n\n"); Spiral expectedGeneratedSpiral = new Spiral(3); const string expectedRenderedSpiral = "[multi-line string\nrepresenting the\nrendered spiral]"; _mockGenerator.GeneratedSpiral = expectedGeneratedSpiral; _mockRenderer.ExpectedSpiral = expectedGeneratedSpiral; _mockRenderer.RenderedSpiral = expectedRenderedSpiral; _program.Run(); Assert.That(_outWriter.ToString(), Is.EqualTo(_USER_PROMPT + expectedRenderedSpiral + _USER_PROMPT)); }
public void Render(Spiral spiral, TextWriter writer) { Calls++; if (spiral != ExpectedSpiral) { throw new Exception("Error: did not get expected spiral in call to Render(spiral, writer)."); } writer.Write(RenderedSpiral); }
public string Render(Spiral spiral) { Calls++; if (spiral != ExpectedSpiral) { throw new Exception("Error: did not get expected spiral in call to Render(spiral)."); } return RenderedSpiral; }
public void ShouldPromptUserMoreSpecificallyOnInvalidInput() { WriteToInput("three\n3\n\n"); Spiral expectedGeneratedSpiral = new Spiral(3); const string expectedRenderedSpiral = "[multi-line string\nrepresenting the\nrendered spiral]"; _mockGenerator.GeneratedSpiral = expectedGeneratedSpiral; _mockRenderer.ExpectedSpiral = expectedGeneratedSpiral; _mockRenderer.RenderedSpiral = expectedRenderedSpiral; _program.Run(); Assert.That(_outWriter.ToString(), Is.EqualTo(_USER_PROMPT + _BAD_INPUT_PROMPT + Environment.NewLine + _USER_PROMPT + expectedRenderedSpiral + _USER_PROMPT)); }
public void DrawOn(Spiral spiral) { DrawRightOn(spiral); DrawBottomOn(spiral); DrawLeftOn(spiral); DrawTopOn(spiral); }
private void DrawTopOn(Spiral spiral) { for (int i = 0; i < _sideLength; i++) { spiral.SetValueRelative(_topStartX + i, -_index, _topStartValue + i); } }
private void DrawRightOn(Spiral spiral) { for (int i = 0; i < _sideLength; i++) { spiral.SetValueRelative(_index, _rightStartY + i, _rightStartValue + i); } }
private void DrawLeftOn(Spiral spiral) { for (int i = 0; i < _sideLength; i++) { spiral.SetValueRelative(-_index, _leftStartY - i, _leftStartValue + i); } }
private void DrawBottomOn(Spiral spiral) { for (int i = 0; i < _sideLength; i++) { spiral.SetValueRelative(_bottomStartX - i, _index, _bottomStartValue + i); } }
private Spiral PopulateSpiral(Spiral spiral) { var offset = spiral.Origin.X; for (int x = -offset; x <= offset; x++) { for (int y = -offset; y <= offset; y++) { int valueAtCoordinates = GetValueAt(x, y); spiral.SetValueRelative(x, y, valueAtCoordinates); } } return spiral; }