Ejemplo n.º 1
0
    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));
    }
Ejemplo n.º 2
0
        public static void Main()
        {
            while (true)
            {
                Sudoku s = new Sudoku();

                s.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 }
                };

                s.Generate(30);
                if (s.Solve())
                {
                    Console.Write("\"");
                    for (int i = 0; i < 9; i++)
                    {
                        for (int j = 0; j < 9; j++)
                        {
                            Console.Write(s.Data[i, j]);
                        }
                    }
                    Console.WriteLine("\",");
                }
            }
        }
Ejemplo n.º 3
0
        // 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");
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        // 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");
        }