6 Replies - 582 Views - Last Post: 28 April 2014 - 08:54 AM Rate Topic: -----

#1 TgaJava  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 28-April 14

Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 06:33 AM

Hey guys, As it is known there are nine boxes and in each box we can put numbers 1-9 without repetition. I am trying to check a given filled Sudoku puzzle whether appropriate numbers are inserted in each of the nine boxes. For the time being I don't consider row repetition and column repetition. I wrote the code using loops and condition

I used 9*9 text-boxes. The names of the textboxes is sequential like for example for the first box
txt11,txt12,txt13
txt14,txt15,txt16
txt17,txt18,txt19

Actually, I used also the controls id to access the text boxes, and each box is checking with the neighboring boxes for equality both in forward -> and backward <-


image

So here is the code
public void BoxCheck()
        {
            int start = 82, end = 74;
            int i, b, f;

            for (int p = 1; p <= 9; p++, start -= 9, end -= 9)
            {
                richTextBox1.AppendText("\nBox " + 1);

                for (i = start, b = 0, f = 8; i >= end; i--, f--, b++)
                {

                    int comp = int.Parse(Form1.ActiveForm.Controls[i].Text);

                    richTextBox1.AppendText("\nChecking  " + Form1.ActiveForm.Controls[i].Name);
                    Form1.ActiveForm.Controls[i].BackColor = Color.Purple;
                    for (int bi = 1, n = i; bi <= b; bi++)
                    {
                        int curr = int.Parse(Form1.ActiveForm.Controls[++n].Text);
                        richTextBox1.AppendText("\nWith  " + Form1.ActiveForm.Controls[n].Name);
                        // Form1.ActiveForm.Controls[i].BackColor = Color.Green;
                        if (comp != curr)
                            continue;
                        else
                        {
                            Form1.ActiveForm.Controls[i].BackColor = Color.Red;
                            Form1.ActiveForm.Controls[n].BackColor = Color.Red;
                        }

                    }
                    for (int fi = 1, n = i; fi <= f; fi++)
                    {
                        int curr = int.Parse(Form1.ActiveForm.Controls[--n].Text);
                        richTextBox1.AppendText("\nWith  " + Form1.ActiveForm.Controls[n].Name);
                        // Form1.ActiveForm.Controls[i].BackColor = Color.Green;
                        if (comp != curr)
                            continue;
                        else
                        {
                            Form1.ActiveForm.Controls[i].BackColor = Color.Red;
                            Form1.ActiveForm.Controls[n].BackColor = Color.Red;
                        }
                    }

                }
            }
        }



This works pretty fine, but is is the right way to do it? it is efficient? Remember for a full checking I have to include row check and column check as well, this is just for checking within each of the 9 boxes in Sudoku.

Is This A Good Question/Topic? 0
  • +

Replies To: Sudoku Solver using loops, textboxes

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 06:41 AM

You shouldn't use your controls as your data store, you should have that somewhere else and update the display as needed. But with what you have, why are you converting them to ints? You can compare strings and save the trouble. Your routine also looks overly complicated. If you have 9 things to check, run one loop from the first to the eighth, the other from <first loop + 1) to nine.
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 06:47 AM

Your GUI needs to update itself based on the data you give it, not other way around. My quick solution is to make a class Sudoku with all attributes that sudoku has(I don't play sudoku so i don't know what to put in your class).
Was This Post Helpful? 0
  • +
  • -

#4 TgaJava  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 28-April 14

Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 06:54 AM

View PostMomerath, on 28 April 2014 - 06:41 AM, said:

You shouldn't use your controls as your data store, you should have that somewhere else and update the display as needed.

that? somewhere else? update the display, which display?

Quote

But with what you have, why are you converting them to ints? You can compare strings and save the trouble.

I accept this comment

Quote

Your routine also looks overly complicated. If you have 9 things to check, run one loop from the first to the eighth, the other from <first loop + 1) to nine.

This is great idea it just didn't come to my mind, so I have to rewrite the code.
Was This Post Helpful? 0
  • +
  • -

#5 TgaJava  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 28-April 14

Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 07:00 AM

View PostMichael26, on 28 April 2014 - 06:47 AM, said:

Your GUI needs to update itself based on the data you give it, not other way around. My quick solution is to make a class Sudoku with all attributes that sudoku has(I don't play sudoku so i don't know what to put in your class).


So the solution for any software problem is making a class :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 07:10 AM

Typically, one of the solutions to software problems is to break it down into smaller pieces. Apply this principle recursively. By implementing code that focuses on just storing the data, you split the problem into maintaining the UI and maintaining the data. Another approach to problem solving is applying levels of abstraction. It may not necessarily break the problem into smaller pieces, but it does make it easier to think about and reason with the problem. In both cases, when dealing with C# and Java, you are typically lead to creating classes to encapsulate those portions of functionality or ideas.
Was This Post Helpful? 1
  • +
  • -

#7 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 08:54 AM

In object oriented language such as C# everything revolves around classes and object. But Skydiver explained it better.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1