public void DisplayCandidatesForCell(SudokuLabel label) { var candidates = "123456789"; const string nl = "\r\n"; // Get the SudokuLabels that can be changed by the user. foreach (var lbl in tableLayoutPanel1.Controls.OfType <SudokuLabel>().Where(lbl => lbl.Column == label.Column)) { candidates = candidates.Replace(lbl.Value.ToString(), " "); label.Candidates = $@"{candidates.Substring(0, 3)}{nl}{candidates.Substring(3, 3)}{nl}{candidates.Substring(6, 3)}"; } foreach (var lbl in tableLayoutPanel1.Controls.OfType <SudokuLabel>().Where(lbl => lbl.Row == label.Row)) { candidates = candidates.Replace(lbl.Value.ToString(), " "); label.Candidates = $@"{candidates.Substring(0, 3)}{nl}{candidates.Substring(3, 3)}{nl}{candidates.Substring(6, 3)}"; } foreach (var lbl in tableLayoutPanel1.Controls.OfType <SudokuLabel>().Where(lbl => lbl.SubGrid == label.SubGrid)) { candidates = candidates.Replace(lbl.Value.ToString(), " "); label.Candidates = $@"{candidates.Substring(0, 3)}{nl}{candidates.Substring(3, 3)}{nl}{candidates.Substring(6, 3)}"; } candidates = $"{candidates.Substring(0, 3)}{nl}{candidates.Substring(3, 3)}{nl}{candidates.Substring(6, 3)}"; label.Text = candidates; }
public void DrawBoard() { //if (tableLayoutPanel1.Controls.Count >= 81) throw new Exception("The board has already been drawn."); // used to store the location of the cell var location = new Point(); // draws the cells for (var row = 1; row <= 9; row++) { for (var col = 1; col <= 9; col++) { location.X = col * (CellWidth + 1) + XOffset - 8; location.Y = row * (CellHeight + 1) + YOffset - 28; var subgrid = SudokuPuzzle.GetRegion(col, row); var sudokuLabel = new SudokuLabel { Name = $"Label{col}{row}{subgrid}", Tag = $"Label{col}{row}{subgrid}", Font = new Font("Consolas", 8, FontStyle.Bold), BorderStyle = BorderStyle.Fixed3D, Location = location, Width = CellWidth, Height = CellHeight, Margin = new Padding(0), Padding = new Padding(0), TextAlign = ContentAlignment.MiddleCenter, BackColor = UserBackColor, ForeColor = UserForeColor, Text = $@"{col}{row}{subgrid}", Column = col, Row = row, SubGrid = SudokuPuzzle.GetRegion(col, row) }; SudokuPuzzle.SetAlternateRegionColors(ref sudokuLabel); sudokuLabels.Add(sudokuLabel); if (tableLayoutPanel1.Controls.Count < 81) { tableLayoutPanel1.Controls.Add(sudokuLabel); } } } }
public static void SetAlternateRegionColors(ref SudokuLabel label) { switch (label.SubGrid) { case 1: case 3: case 5: case 7: case 9: label.BackColor = Color.LightYellow; break; case 2: case 4: case 6: case 8: label.BackColor = Color.LightSteelBlue; break; } }