Hello everyone,
currently I am refactoring my existing code and I realized, that I have almost two identical methods. One method checks values in a matrix along the row, the other one does excatly the same along the columns.
I remember the function AddForce() in Unity. At the end, you can chose an enum for the force mode. For example ForceMode.Force, ForceMode.Impulse etc.
Example from UNITY DOCS:
using UnityEngine;
public class Example : MonoBehaviour
{
public float thrust = 1.0f;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.AddForce(0, 0, thrust, ForceMode.Impulse);
}
}
In the end, I would like to achieve a method call which looks like this:
private void Start()
{
numberCollector(ExecutionMode.Row);
numberCollector(ExecutionMode.Column);
}
Now I would like to do the same with the following two methods, combining them to one.
Later, when calling the method, I like to chose something like ExecutionMode.AsRow or ExecutionMode.AsColumn.
Does anyone has an idea how to solve this issue in an elegant way?
Current Code for Columns:
public List<int> numberCollectorColumn(int column)
{
int[] numberCollector = new int[gridSize];
int numberCollectorIndex = 0;
int numberCount = 0;
for (int k = 0; k < gridSize; k++)
{
if (tileInfo[column, k])
{
numberCount++;
numberCollector[numberCollectorIndex] = numberCount;
}
if (k < gridSize - 1)
{
if (tileInfo[column, k] && !tileInfo[column, k + 1])
{
numberCollectorIndex++;
numberCount = 0;
}
}
}
List<int> numberCollectorList = new List<int>(numberCollector);
return numberCollectorList;
}
Current Code for Rows:
public List<int> numberCollectorRow(int row)
{
int[] numberCollector = new int[gridSize];
int numberCollectorIndex = 0;
int numberCount = 0;
for (int k = 0; k < gridSize; k++)
{
// Case No.1 - Current entry and next entry are both false.
if (tileInfo[k, row])
{
numberCount++;
numberCollector[numberCollectorIndex] = numberCount;
}
if (k < gridSize-1)
{
if (tileInfo[k, row] && !tileInfo[k + 1, row])
{
numberCollectorIndex++;
numberCount = 0;
}
}
}
List<int> numberCollectorList = new List<int>(numberCollector);
return numberCollectorList;
}