New to compact framework tutorials? Learn to write your first compact framework application here.
Zac Efron - Hairspray star and Mythbusters' Kari Byron at Celebrity Treat. Check out income and career information in Singapore.

Wednesday, November 23, 2005

- Controls_: using the soft keys for a Sudoku solver app

Well, maybe the first app and even the browser app are too easy. (If you're just getting started with Compact Framework or the tools, see this.) Let's see if things get better...

One of the most important new features in Windows Mobile 5.0 is the Soft Keys. Instead of using a traditional menu (which is still possible), most apps will now use just the left soft key and the right soft key at the bottom of the screen. Like in cell phones, the functions of the soft keys change according to the context (i.e. it is "app-dependent", and "form-dependent").



Sudoku puzzles are all the rage these days. According to this there are 5,472,730,538 unique Sudoku puzzles, and this claims that Sudoku puzzles are NP-complete. These all mean you'll have endless fun with Sudoku. So today, to make it more fun, we're going to write a Sudoku solver app. It uses a brute force algorithm, so any solvable Sudoku puzzle can be solved using this app.
  1. Create a new C# Windows Mobile 5.0 PPC Device Application project in Visual Studio .NET 2005.
  2. At the bottom of the form is a blue bar. That is the menu. To add the left soft key, click on the left side of the blue bar and type "Default". Similarly for the right soft key, click on the right side and type "Solve It!"
  3. Add a TextBox control to the form. Change its Multiline property to True. When the app runs, the user will enter the puzzle in the textbox. Dock the textbox so that it always fills the entire form, by changing its Dock property to Fill (see also this on docking and anchoring). Change its Font property to use Courier New Bold 12pt.
  4. Change other properties (see previous posts) as needed to get this:
  5. Double-click on "Default" to jump to the event handler of the left soft key. We want to set the textbox to contain a default puzzle when the user clicks on this soft key. Type the following code in the event handler method:
  6. textBox1.Text=
    @"800017060
    407065000
    905080310
    300200001
    070000040
    500004008
    086070504
    000540206
    050620009";
  7. Next, the logic of the solver. Press Shift-F7 to go back to design view and double-click on the "Solve It!" right soft key. Type the following in the event handler:
  8. if (textBox1.Text.Trim() == "") return;
    string s = textBox1.Text.Replace(" ", "").Replace("\t", "");
    char[] tA = s.Replace("\r", "").Replace("\n", "").ToCharArray();
    textBox1.Text = "";
    int[] A = new int[tA.Length];
    for (int i = 0; i < tA.Length; i++) A[i] = (int)(tA[i] - '0');
    R(A);
  9. Copy the following 2 methods and paste into your code (below the previous event handler method):
  10. private void R(int[] A)
    {
      for (int i = 0; i <= 80; i++)
      {
        if (A[i] > 0) continue;
        ArrayList aL = new ArrayList(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
        for (int j = 0; j <= 80; j++)
          if (j / 9 == i / 9 || j % 9 == i % 9 ||
          (j / 27 == i / 27 && (j % 9) / 3 == (i % 9) / 3))
            aL.Remove(A[j]);
        foreach (int k in aL) { A[i] = k; R(A); }
        A[i] = 0; return;
      }
      print(A);
    }
    private void print(int[] A)
    {
      string s = "";
      for (int i = 0; i <= 80; i++)
      {
        s += A[i] + " ";
        if (i % 3 == 2) s += " ";
        if (i % 9 == 8) s += "\r\n";
        if (i % 27 == 26) s += "\r\n";
      }
      textBox1.Text = s;
    }
  11. Because ArrayList is used in the code, add a line right at the top of your code to include the Collections namespace:
  12. using System.Collections;
  13. To tabify/beautify your code, click Edit-> Advanced-> Format Document in your code view.
  14. Finally, click Ctrl-F5 to deploy and run. To test, click on Default and click Solve It! You may also type in your own Sudoku puzzle (enter all the numbers in 9 rows, with a zero for each "space" to solve, as illustrated in the default puzzle).
Ever wondered what really is the Microsoft .NET Compact Framework? Read this.

Or write a Multithreaded Prime Number Generator for your PDA!

Categories: [Controls_] Tags: [] [] []

0 Comments:

Post a Comment

<< Home