# Sudoku Solver using loops, textboxes

Page 1 of 1

## 6 Replies - 934 Views - Last Post: 28 April 2014 - 08:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=345838&amp;s=caaab0037409d0dea49c84a66a81b9ad&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TgaJava

Reputation: 12
• Posts: 71
• 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

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• 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.

### #3 Michael26

Reputation: 378
• Posts: 1,570
• 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).

### #4 TgaJava

Reputation: 12
• Posts: 71
• Joined: 28-April 14

## Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 06:54 AM

Momerath, 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.

### #5 TgaJava

Reputation: 12
• Posts: 71
• Joined: 28-April 14

## Re: Sudoku Solver using loops, textboxes

Posted 28 April 2014 - 07:00 AM

Michael26, 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

### #6 Skydiver

• Code herder

Reputation: 4100
• Posts: 13,080
• 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.

### #7 Michael26

Reputation: 378
• Posts: 1,570
• 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.