public static string Generate() { SudokuModel.Sudoku sudoku = new SudokuModel.Sudoku(); sudoku.Data = new byte[, ] { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; while (!(sudoku.Generate(30).second&& sudoku.Solve())) { } char[] rawData = new char[81]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { rawData[i * 9 + j] = sudoku.Data[i, j].ToString()[0]; } } return(new string(rawData)); }
public Game() { // // Required for Windows Form Designer support // InitializeComponent(); boxes = new ComboBox[9, 9]; Font font = null; for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { ComboBox comboBox = new ComboBox(); if (font == null) { font = new Font(comboBox.Font, FontStyle.Bold); } comboBox.Font = font; String[] items = new String[] { "-", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; comboBox.Items.AddRange(items); comboBox.DropDownStyle = ComboBoxStyle.DropDownList; comboBox.Location = new Point(x * 40 + 10, y * 30 + 10); comboBox.Size = new Size(36, 21); comboBox.SelectedIndex = 0; comboBox.MaxDropDownItems = 10; this.Controls.Add(comboBox); boxes[y, x] = comboBox; } } Sudoku s = new Sudoku(); byte[,] d = s.read_from_file(); /* * * tbHistory.AppendText("-------------------------------\r\n"); * if(!s.IsSudokuFeasible()) * {// * tbHistory.AppendText("Sudoku not feasible\r\n"); * return; * } * * DateTime now = DateTime.Now; * tbHistory.AppendText("Solve started: " + now.ToLongTimeString() + "\r\n"); * * if(s.Solve()) * { * // Solve successful * tbHistory.AppendText("Solve successful\r\n"); * * d = s.Data; * SetData(d); * } * else * { * // Solve failed * tbHistory.AppendText("Solve failed\r\n"); * } * tbHistory.AppendText(String.Format("{0} seconds\r\n", (DateTime.Now - now).TotalSeconds)); * * // Add blank * tbHistory.AppendText("\r\n"); */ SetData(d); s.Data = d; if (!s.IsSudokuFeasible()) {// tbHistory.AppendText("Sudoku not feasible\r\n"); return; } if (s.Solve()) { // Solve successful tbHistory.AppendText("Solve successful\r\n"); d = s.Data; SetData(d); } else { // Solve failed tbHistory.AppendText("Solve failed\r\n"); } m_printDocument = new PrintDocument(); m_printDocument.PrintPage += new PrintPageEventHandler(PrintPage); }
// Check uniqueness private void btnUnique_Click(object sender, System.EventArgs e) { Sudoku s = new Sudoku(); byte[,] d = GetData(); s.Data = d; tbHistory.AppendText("-------------------------------\r\n"); if(s.IsSudokuUnique()) { tbHistory.AppendText("Sudoku unique\r\n"); } else { tbHistory.AppendText("Sudoku not unique\r\n"); } }
// Test uniqueness private void btnTest_Click(object sender, System.EventArgs e) { Sudoku s = new Sudoku(); byte[,] d = GetData(); s.Data = d; tbHistory.AppendText("-------------------------------\r\n"); if(s.IsSudokuFeasible()) { tbHistory.AppendText("Sudoku feasible\r\n"); } else { tbHistory.AppendText("Sudoku not feasible\r\n"); } }
// Solve private void btnSolve_Click(object sender, System.EventArgs e) { Sudoku s = new Sudoku(); byte[,] d = GetData(); s.Data = d; tbHistory.AppendText("-------------------------------\r\n"); if(!s.IsSudokuFeasible()) { tbHistory.AppendText("Sudoku not feasible\r\n"); return; } DateTime now = DateTime.Now; tbHistory.AppendText("Solve started: " + now.ToLongTimeString() + "\r\n"); if(s.Solve()) { // Solve successful tbHistory.AppendText("Solve successful\r\n"); d = s.Data; SetData(d); } else { // Solve failed tbHistory.AppendText("Solve failed\r\n"); } tbHistory.AppendText(String.Format("{0} seconds\r\n", (DateTime.Now - now).TotalSeconds)); // Add blank tbHistory.AppendText("\r\n"); }
// Generate private void btnGenerate_Click(object sender, System.EventArgs e) { Sudoku s = new Sudoku(); byte[,] d = GetData(); s.Data = d; Cursor oldCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; tbHistory.AppendText("-------------------------------\r\n"); var result = s.Generate((int)numSpots.Value); if(result.Item2) { tbHistory.AppendText(String.Format("Sudoku generated succesfully after {0} tries\r\n", result.Item1)); tbHistory.AppendText("Sudoku with " + numSpots.Value + " spots\r\n"); d = s.Data; SetData(d); } else { tbHistory.AppendText(String.Format("Sudoku not found after {0} tries.\r\n Press generate to try again.\r\n", result.Item1)); } Cursor.Current = oldCursor; }