static bool DoesSubgridsHaveDuplicates(ASudokuGrid Grid) { List <List <int> > Subgrids = new List <List <int> >(); int YCounter = 1; int XCounter = 1; bool ContainsDuplicate = false; //May want/need to wrap into a method, this converts a 2D Grid into sub grids for a 9x9 grid. for (int SubgridIndex = 1; SubgridIndex <= 9; SubgridIndex++) { Subgrids.Add(new List <int>()); for (int x = ((Grid.Grid.GetLength(0) / 3) * XCounter) - (Grid.Grid.GetLength(0) / 3); x < (Grid.Grid.GetLength(0) / 3) * XCounter; ++x) { //Y Works well, do something for X now. for (int y = (Grid.Grid.GetLength(1) - (3 * (3 - YCounter))) - 3; y < (Grid.Grid.GetLength(1) - (3 * (3 - YCounter))); ++y) { Subgrids[Subgrids.Count - 1].Add(Grid.Grid[x, y]); } } if (SubgridIndex % 3 == 0) { YCounter = 1; XCounter++; } else { YCounter++; } } //Count doubles for subgrids Subgrids.ForEach(SubGrid => { if (SubGrid.Count != SubGrid.Distinct().Count()) { ContainsDuplicate = true; } }); return(ContainsDuplicate); }
static public float GridsFitness(int[,] Grid) { //Seems to count two rows at a time? List <int> RowInts = new List <int>(); List <int> ColInts = new List <int>(); List <List <int> > Subgrids = new List <List <int> >(); float DuplicateCount = 0.0f; int YCounter = 1; int XCounter = 1; //May want/need to wrap into a method, this converts a 2D Grid into sub grids for a 9x9 grid. for (int SubgridIndex = 1; SubgridIndex <= 9; SubgridIndex++) { Subgrids.Add(new List <int>()); for (int x = ((Grid.GetLength(0) / 3) * XCounter) - (Grid.GetLength(0) / 3); x < (Grid.GetLength(0) / 3) * XCounter; ++x) { //Y Works well, do something for X now. for (int y = (Grid.GetLength(1) - (3 * (3 - YCounter))) - 3; y < (Grid.GetLength(1) - (3 * (3 - YCounter))); ++y) { Subgrids[Subgrids.Count - 1].Add(Grid[x, y]); } } if (SubgridIndex % 3 == 0) { YCounter = 1; XCounter++; } else { YCounter++; } } //Count doubles for subgrids Subgrids.ForEach(SubGrid => { if (SubGrid.Count != SubGrid.Distinct().Count()) { DuplicateCount = DuplicateCount + SubGrid.Count - SubGrid.Distinct().Count(); } }); //Count for rows and cols for (int x = 0; x < (Grid.GetLength(0)); ++x) { for (int y = 0; y < (Grid.GetLength(1)); ++y) { RowInts.Add(Grid[x, y]); ColInts.Add(Grid[y, x]); } if (RowInts.Count != RowInts.Distinct().Count()) { //DuplicateCount = DuplicateCount + RowInts.Count - RowInts.Distinct().Count(); } if (ColInts.Count != ColInts.Distinct().Count()) { DuplicateCount = DuplicateCount + ColInts.Count - ColInts.Distinct().Count(); } RowInts.Clear(); ColInts.Clear(); } return(DuplicateCount / 144.0f);//216.0f; }