private void populateDGV() { //Set up row headers so it's easy to see the row numbers for (int i = 0; i < 9; i++) { ViewPuzzle.Rows.Add(); ViewPuzzle.Rows[i].HeaderCell.Value = "R" + (1 + i); ViewPuzzle.Rows[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } //Populate the rows popThread = new Thread(() => { for (int i = 0; i < 9; i++) { for (int k = 0; k < 9; k++) { ViewPuzzle.Rows[i].Cells[k].Value = sodokuPuzzle[i, k]; ViewPuzzle.Invoke((MethodInvoker) delegate { if (sodokuPuzzle[i, k] != 0) { ViewPuzzle.Rows[i].Cells[k].Style.BackColor = Color.Aquamarine; } }); } } popThread.Join(); } ); popThread.Start(); }
private void solvePuzzle() { bool giveUp = false; //Quit if program can't figure out the puzzle. int passes = 0; //Current amount of passes without placing a number. List <int> possibleAnswers; //Possible answers for this current box. solveThread = new Thread(() => { while (!giveUp) { //Step through each box for (int i = 0; i < 9; i++) { for (int k = 0; k < 9; k++) { getPossibleAnswers(k, i, out possibleAnswers); if (possibleAnswers.Count == 1 && sodokuPuzzle[k, i] == 0) { ViewPuzzle.Invoke((MethodInvoker) delegate { ViewPuzzle.Rows[k].Cells[i].Value = possibleAnswers[0]; ViewPuzzle.Rows[k].Cells[i].Style.BackColor = Color.Green; sodokuPuzzle[k, i] = possibleAnswers[0]; placedNumbers++; } ); Thread.Sleep(150); } } } passes++; if (passes == 30) { giveUp = true; MessageBox.Show("Solved to the best of my ability!" + "\n" + "Placed numbers: " + placedNumbers); } } solveThread.Join(); } ); solveThread.Start(); }