static private BoxSpot GetCurrentBox(int r, int c) { BoxSpot start = new BoxSpot(); if (r < 3) { start.r = 0; } else if (r < 6) { start.r = 3; } else if (r < 9) { start.r = 6; } if (c < 3) { start.c = 0; } else if (c < 6) { start.c = 3; } else if (c < 9) { start.c = 6; } return(start); }
static private void FindLockedCandidatesCols(string[,] values) { for (int n = 1; n < 10; n++) { for (int c = 0; c < 9; c++) { List <BoxSpot> spots = new List <BoxSpot>(); for (int r = 0; r < 9; r++) { if (values[r, c].Contains(n.ToString())) { if (spots.Count == 0) { spots.Add(new BoxSpot(r, c)); } else { bool same_box = true; foreach (BoxSpot spot in spots) { if (!spot.CompareBox(r, c)) { same_box = false; break; } } if (same_box) { spots.Add(new BoxSpot(r, c)); } else { spots = null; break; } } } } if (spots != null && spots.Count > 0) { BoxSpot box = GetCurrentBox(spots[0].r, spots[0].c); for (int cc = box.c; cc < box.c + 3; cc++) { for (int rr = box.r; rr < box.r + 3; rr++) { if (spots.Where(x => x.r == rr && x.c == cc).ToList().Count == 0) { values[rr, cc] = values[rr, cc].Replace(n.ToString(), ""); } } } } } } }
static private void FindLockedCandidates(string[,] values) { for (int n = 1; n < 10; n++) { for (int b_r = 0; b_r < 9; b_r += 3) { for (int b_c = 0; b_c < 9; b_c += 3) { List <BoxSpot> spots = new List <BoxSpot>(); for (int r = b_r; r < b_r + 3; r++) { for (int c = b_c; c < b_c + 3; c++) { if (values[r, c].Contains(n.ToString())) { BoxSpot spot = new BoxSpot(); spot.r = r; spot.c = c; spot.n = n; spots.Add(spot); } } } if (spots.Count < 4) { int r_count = 0; int c_count = 0; for (int i = 1; i < spots.Count; i++) { if (spots[i].r == spots[i - 1].r) { r_count++; } if (spots[i].c == spots[i - 1].c) { c_count++; } } if (r_count == spots.Count - 1) { for (int c = 0; c < 9; c++) { if (spots.Where(s => s.c == c).ToList().Count == 0) { values[spots[0].r, c] = values[spots[0].r, c].Replace(spots[0].n.ToString(), ""); } } } if (c_count == spots.Count - 1) { for (int r = 0; r < 9; r++) { if (spots.Where(s => s.r == r).ToList().Count == 0) { values[r, spots[0].c] = values[r, spots[0].c].Replace(spots[0].n.ToString(), ""); } } } } } } } }