/// <summary> /// Creates squarelines accordingly before calculating is done. /// </summary> public void CreateSquareLines() { // Clearing actions from previous executions. _squareLines.Clear(); ResetSquareStats(); foreach (Square square in _squares) { // Checking if square actually has a player icon on it. if (square.PlayerNumber != 0) { // Checking for three possible cases: // 1) Square is not calculated horizontally AND square on it's right has the same icon. if (!square.IsCalculatedHorizontally && square.SamePlayerNumber(square.SquareOnRight)) { SquareLine line = new SquareLine(); line.Add(square); square.IsCalculatedHorizontally = true; Direction direction = Direction.Horizontal; FinishSquareLine(line, square.SquareOnRight, direction); _squareLines.Add(line); square.IsIndividual = false; } // 2) Square is not calculated vertically AND square below it has the same icon. if (!square.IsCalculatedVertically && square.SamePlayerNumber(square.SquareBelow)) { SquareLine line = new SquareLine(); line.Add(square); square.IsCalculatedVertically = true; Direction direction = Direction.Vertical; FinishSquareLine(line, square.SquareBelow, direction); _squareLines.Add(line); square.IsIndividual = false; } // 3) Square doesn't have same icon on it's right or below (aka it is "individual"). if (square.IsIndividual) { SquareLine line = new SquareLine(); line.Add(square); _squareLines.Add(line); } } } }
/// <summary> /// Finishes one line recursively by collecting all squares /// that are part of the line. /// </summary> /// <param name="line">Squareline that is being operated.</param> /// <param name="square">Square that is being added to line.</param> /// <param name="direction">Tells which direction line is moving.</param> public void FinishSquareLine(SquareLine line, Square square, Direction direction) { line.Add(square); square.IsIndividual = false; if (direction == Direction.Horizontal) { square.IsCalculatedHorizontally = true; if (square.SamePlayerNumber(square.SquareOnRight)) { FinishSquareLine(line, square.SquareOnRight, direction); } } else { square.IsCalculatedVertically = true; if (square.SamePlayerNumber(square.SquareBelow)) { FinishSquareLine(line, square.SquareBelow, direction); } } }