Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Pocket PC » Advanced technique to improve Sudoku for PocketPC

Advanced technique to improve Sudoku for PocketPC

This article will explain a techniques to visualize sudoku in pocketPC environment and adding some helpful addons.

Page Views : 2854
Downloads : 45
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
graphDoku.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

First of all we should realize that we are writing for pocketPC and not for PC platform. What the differences? Well despite interfaces and forms there is one big problem - limited memory! This means whatever we do and create - it must not "eat" to much memory. Adding graphics to a sudoku in C# can be a bit treaky. Of course we can always use C++ lagnguage and create a more low level code and more optimized compiled source but let's do some work and use C# to do that.

First - lets create some nice interface - buttons. it is not that difficult to do if you are familiar with user controls and event handling. We will use both since our button must responce to a click.

The definition of colorbutton class in in colorbutton.cs file of sources. Initially this class was created for a PC, but since compact environment does not support some of its features they were just disabled but main feature remains - a button is better than a regular one in compact framework - we can real-time change its shape and colors!

using System;

using System.ComponentModel; 

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace ColorButton

{ 

          public class ColorButton : System.Windows.Forms.Control

          {

                    private enum _States

                    {

                             Normal,

                             MouseOver,

                             Clicked

                    } 

                    public enum ButtonStyles

                    {

                             Rectangle,

                             Ellipse

                    } 

                    // default values

                    private bool _Active = true;

                    private _States _State = _States.Normal; 

                    private ButtonStyles _ButtonStyle = ButtonStyles.Rectangle;

                    private int handle = 0; 

                    private Color _NormalBorderColor = Color.Green; 

                    private Color _NormalColorA = Color.Green;

                    private Color _NormalColorB = Color.Honeydew; 

                    private Color _HoverBorderColor = Color.MediumVioletRed;

                    private Color _HoverColorA = Color.MediumVioletRed;

                    private Color _HoverColorB = Color.White; 

                    public ColorButton(int ihandle)

                    {

                             // initiate button size and font

                             base.Size = new Size(200, 40);

                             base.Font = new Font("Verdana", 10, FontStyle.Bold);

                             this.handle = ihandle;

                    } 

                    public int getHandle()

                    {

                             return this.handle;

                    } 

                    public ButtonStyles ButtonStyle

                    {

                             get

                             {

                                       return _ButtonStyle;

                             }

                             set

                             {

                                       _ButtonStyle = value;

                                       this.Invalidate();

                             }

                    } 

                    public Color NormalBorderColor

                    {

                             get

                             {

                                       return _NormalBorderColor;

                             }

                             set

                             {

                                       _NormalBorderColor = value;

                                       this.Invalidate();

                             } 

                    } 

                    public Color HoverBorderColor

                    {

                             get

                             {

                                       return _HoverBorderColor;

                             }

                             set

                             {

                                       _HoverBorderColor = value;

                                       this.Invalidate();

                             } 

                    } 

                    public Color NormalColorA

                    {

                             get

                             {

                                       return _NormalColorA;

                             }

                             set

                             {

                                       _NormalColorA = value;

                                       this.Invalidate();

                             }

                    } 

                    public Color NormalColorB

                    {

                             get

                             {

                                       return _NormalColorB;

                             }

                             set

                             {

                                       _NormalColorB = value;

                                       this.Invalidate();

                             }

                    } 

                    public Color HoverColorA

                    {

                             get

                             {

                                       return _HoverColorA;

                             }

                             set

                             {

                                       _HoverColorA = value;

                                       this.Invalidate();

                             }

                    } 

                    public Color HoverColorB

                    {

                             get

                             {

                                       return _HoverColorB;

                             }

                             set

                             {

                                       _HoverColorB = value;

                                       this.Invalidate();

                             }

                    } 

                    public bool Active

                    {

                             get

                             {

                                       return _Active;

                             }

                             set

                             {

                                       _Active = value;

                                       this.Invalidate();

                             }

                    } 

                    // to make sure the control is invalidated(repainted) when the text is changed

                    public override string Text

                    {

                             get

                             {

                                       return base.Text;

                             }

                             set

                             {

                                       base.Text = value;

                                       this.Invalidate();

                             }

                    } 

                    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

                    {

                             SolidBrush brush; 

                             SizeF textSize = e.Graphics.MeasureString(this.Text, base.Font);

                             int textX = (int)(base.Size.Width / 2) - (int)(textSize.Width / 2);

                             int textY = (int)(base.Size.Height / 2) - (int)(textSize.Height / 2);

                             Rectangle newRect = new Rectangle(ClientRectangle.X + 1, ClientRectangle.Y + 1,

                                       ClientRectangle.Width - 3, ClientRectangle.Height - 3);   

                             if (_Active)

                             {

                                       brush = new SolidBrush(_NormalColorA);

                                       switch (_ButtonStyle)

                                       {

                                                case ButtonStyles.Rectangle:

                                                          e.Graphics.FillRectangle(brush, newRect);

                                                          e.Graphics.DrawRectangle(new Pen(_NormalBorderColor), newRect);

                                                          break;

                                                case ButtonStyles.Ellipse:

                                                          e.Graphics.FillEllipse(brush, newRect);

                                                          e.Graphics.DrawEllipse(new Pen(_NormalBorderColor), newRect);

                                                          break; 

                                       }

                            e.Graphics.DrawString(this.Text, base.Font, new SolidBrush(base.ForeColor), textX, textY);

                             }

                             else

                             {

                                       brush = new SolidBrush(_NormalColorA);

                                       switch (_ButtonStyle)

                                       {

                                                case ButtonStyles.Rectangle:

                                                          e.Graphics.FillRectangle(brush, newRect);

                                                          e.Graphics.DrawRectangle(new Pen(_NormalBorderColor), newRect);

                                                          break;

                                                case ButtonStyles.Ellipse:

                                                          e.Graphics.FillEllipse(brush, newRect);

                                                          e.Graphics.DrawEllipse(new Pen(_NormalBorderColor), newRect);

                                                          break; 

                                       }

                           e.Graphics.DrawString(this.Text, base.Font, new SolidBrush(_NormalColorA), textX, textY); 

                             }

                    }

                    protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)

                    {

                             if (_Active)

                             {

                                       _State = _States.MouseOver;

                                       this.Invalidate();

                                       base.OnMouseUp(e);

                             }

                    } 

                    protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)

                    {

                             if (_Active)

                             {

                                       _State = _States.Clicked;

                                       this.Invalidate();

                                       base.OnMouseDown(e);

                             }

                    } 

                    protected override void OnClick(System.EventArgs e)

                    {

                             // prevent click when button is inactive

                             if (_Active)

                             {

                                       if (_State == _States.Clicked)

                                       {

                                                base.OnClick(e);

                                       }

                             }

                    }

          }

}

Now let see what we've got :

A qubic type of buttons.

Looks not bad isn't it? But it is not all - in the menu you might want those buttons to be somethins else? No problem :

Ellipce buttons

Working in this direction you might change the above class and create whatever shape you want - I leave it to your imagination :)

Now what else can we add to make a user's life a bit better?

We can add some kind of hints, no, not the hits which number to put, it would make a game not interesting, but at least we might tell to user that he is putting a number which can not be at that place bacause he is violating a sudoku rules - same number twice in a row, column or a block! I'm not that good to always see it so I can make such mistake just very easy.

for(int xx = 0; xx < 9; xx++)

{

   if(xx != x)

     {

          if(sudoku[xx][y] == chosenNum)

              {

                   MessageBox.Show("Number " + chosenNum.ToString() + " can not be set!","Column constraint violated!");

                   buttons[x][y].Text = " ";

                   statuses[x][y] = 0;

                   buttons[x][y].NormalColorA = colors[statuses[x][y]];

                   sudoku[x][y] = 0;

                   numbers[chosenNum]++;

                   clickers[chosenNum-1].Visible = true;

                   return;

              }

     }

  if(xx != y)

     {

          if(sudoku[x][xx] == chosenNum)

             {

                  MessageBox.Show("Number " + chosenNum.ToString() + " can not be set!","Row constraint violated!");

                  buttons[x][y].Text = " ";

                  statuses[x][y] = 0;

                  buttons[x][y].NormalColorA = colors[statuses[x][y]];

                  sudoku[x][y] = 0;

                  numbers[chosenNum]++;

                  clickers[chosenNum-1].Visible = true;

                  return;

             }

     }

}

int relX = 3*(x/3);

int relY = (y/3)*3;

for(int xx = relX;xx < relX+3;xx++)

     for(int yy = relY; yy < relY+3; yy++)

         {

              if(xx != x && yy != y)

                     {

                        if(sudoku[xx][yy] == chosenNum)

                         {

                              MessageBox.Show("Number " + chosenNum.ToString() + " can not be set!","Block constraint violated!");

                              buttons[x][y].Text = " ";

                              statuses[x][y] = 0;

                              buttons[x][y].NormalColorA = colors[statuses[x][y]];

                              sudoku[x][y] = 0;

                              numbers[chosenNum]++;

                              clickers[chosenNum-1].Visible = true;

                              return;

                         }

                     }

         }

Cinstraint violation message!

In my previous article I have already explained how to generate sudokus and solve them using programming techniques ( use of Knuth's dancing links algorythm ),  so this time for those who missed it just a few words :

Generating a valid sudoku grid can be done in many ways, for me the fastest I could make for now was is to use an actual sudoku solver. So first I fill 27 sudoku cells on the big diagonal - upper left, middle and lower right blocks since all those cell do not have intersections with others. So I fill them with random variations of numbers 1.9

Next I use my solver to found a firt solution for this grid - if no solution, well fill it again.

After getting solution I start to remove numbers one by one and checking that grid would have a solution and preferbly the only one. If no, I put the taken number back and play with other one. I continue to do so until there are expected number of numbers ( clues ) left on the grid.

The solver it simple inplementation of an total coverage puzzle solution. I create matrix for covering all constraints and numbers already existing in a grid and find a combination as it is done for a classic Knuth's matrix with 1's and zeroes. You might do a short lookup in google for it, it is easy to find that article.

Good luck in making games.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Alex Groysman
Software development expert and consultant. Unix environments, win32, pocket PC.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.