using System; using System.Drawing; using NUnit.Framework; using Tetris.Models; namespace TestTetris; public class GridTest { private Color[,] ig; private Grid g; private Tetrominoe t; private static readonly Random random = new Random(); [SetUp] public void SetUp() { ig = new Color[10, 20]; g = new Grid(ig); t = new Tetrominoe(g, new bool[,] { {true, false, false}, {true, true, true} }, new Point(5, 0), 0, Color.Aqua); Console.Out.WriteLine("===={Grid w/ Tetrominoe}===="); Console.Out.WriteLine(g.ToString(t)); Console.Out.WriteLine("============================"); } [Test] public void CanGoLeft() { int startX = t.Coordinates.X; int startY = t.Coordinates.Y; Console.Out.WriteLine(t.Coordinates); for (int i = 1; i <= startX - g.MinGrid.X; i++) { t.GoLeft(); Console.Out.WriteLine(g.ToString(t)); Assert.AreEqual(new Point(startX-i, startY), t.Coordinates); } // Try over the left grid limit Assert.AreEqual(new Point(g.MinGrid.X, startY), t.Coordinates); t.GoLeft(); Assert.AreEqual(new Point(g.MinGrid.X, startY), t.Coordinates); } [Test] public void CanGoRight() { int startX = t.Coordinates.X; int startY = t.Coordinates.Y; Console.Out.WriteLine(t.Coordinates); for (int i = 1; i < (g.MaxGrid.X+1)-startX-t.Shape.GetLength(0)+1; i++) { t.GoRight(); Console.Out.WriteLine(g.ToString(t)); Assert.AreEqual(new Point(startX+i, startY), t.Coordinates); } // Try over the right grid limit Assert.AreEqual(new Point((g.MaxGrid.X+1)-t.Shape.GetLength(0), startY), t.Coordinates); t.GoRight(); Assert.AreEqual(new Point((g.MaxGrid.X+1)-t.Shape.GetLength(0), startY), t.Coordinates); } [Test] public void CanGoDown() { int startX = t.Coordinates.X; int startY = t.Coordinates.Y; for (int i = 1; i < (g.MaxGrid.Y+1)-t.Shape.GetLength(1)+1; i++) { t.GoDown(); Console.Out.WriteLine(g.ToString(t)); Assert.AreEqual(new Point(startX, startY+i), t.Coordinates); } // Try over the grid depth limit Assert.AreEqual(new Point(startX, (g.MaxGrid.Y+1)-t.Shape.GetLength(1)), t.Coordinates); t.GoDown(); Assert.AreEqual(new Point(startX, (g.MaxGrid.Y+1)-t.Shape.GetLength(1)), t.Coordinates); } [Test] public void CanRotate() { bool[,] shape = new bool[,] { {true, false, false}, {true, true, true} }; int longestShapeBorder; if (shape.GetLength(0) > shape.GetLength(1)) longestShapeBorder = shape.GetLength(0); else longestShapeBorder = shape.GetLength(1); g = new Grid(new Color[longestShapeBorder, longestShapeBorder]); t = new Tetrominoe(g, shape, new Point(0, 0), 0, Color.Aqua); Console.Out.WriteLine(g.ToString(t)); t.RotateLeft(); Console.Out.WriteLine(g.ToString(t)); Assert.AreEqual(3, t.Orientation); t.RotateLeft(); Console.Out.WriteLine(g.ToString(t)); Assert.AreEqual(2, t.Orientation); t.RotateLeft(); Console.Out.WriteLine(g.ToString(t)); Assert.AreEqual(1, t.Orientation); t.RotateLeft(); Console.Out.WriteLine(g.ToString(t)); Assert.AreEqual(0, t.Orientation); } [Test] public void CantGo() { ig[4, 0] = Color.Red; ig[4, 1] = Color.Red; ig[4, 2] = Color.Red; ig[4, 3] = Color.Red; ig[5, 3] = Color.Red; ig[6, 3] = Color.Red; ig[7, 3] = Color.Red; ig[7, 2] = Color.Red; ig[7, 1] = Color.Red; ig[7, 0] = Color.Red; Console.Out.WriteLine(g.ToString(t)); int startX = t.Coordinates.X; int startY = t.Coordinates.Y; int startOrientation = t.Orientation; t.GoDown(); Assert.AreEqual(new Point(startX, startY), t.Coordinates); t.GoLeft(); Assert.AreEqual(new Point(startX, startY), t.Coordinates); t.GoRight(); Assert.AreEqual(new Point(startX, startY), t.Coordinates); t.RotateLeft(); Assert.AreEqual(startOrientation, t.Orientation); t.RotateRight(); Assert.AreEqual(startOrientation, t.Orientation); } [Test] public void LineFull() { Console.Out.WriteLine(g.ToString()); Assert.AreEqual(-1, g.LineFull()); int line = random.Next(g.MinGrid.Y, g.MaxGrid.Y); for (int x = 0; x < ig.GetLength(0); x++) ig[x, line] = Color.Aqua; Console.Out.WriteLine("=========="); Console.Out.WriteLine(g.ToString()); Assert.AreEqual(line, g.LineFull()); } [Test] public void ClearLine() { for (uint x = 0; x < ig.GetLength(0); x++) for (uint y = 0; y < ig.GetLength(1); y++) { ig[x, y] = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256)); } int line = random.Next(g.MinGrid.Y, g.MaxGrid.Y); for (int x = 0; x < ig.GetLength(0); x++) ig[x, line] = Color.Aqua; Color[,] olg_grid = (Color[,])ig.Clone(); Console.Out.WriteLine(g.ToString()); g.ClearLine(line); Console.Out.WriteLine("=========="); Console.Out.WriteLine(g.ToString()); for (uint x = 0; x < ig.GetLength(0); x++) for (uint y = 1; y <= line; y++) { Assert.AreEqual(olg_grid[x, y-1], ig[x,y]); } for (uint x = 0; x < ig.GetLength(0); x++) Assert.AreEqual(Color.Empty, ig[x, 0]); } }