CHAPTER 17

image

Special Sudokus

Now that you know how to generate standard Sudoku puzzles, it is time to learn how to make some special ones.

Designer Sudokus

By “designer Sudokus,” I mean puzzles in which the clues form a particular pattern. For example, Figure 17-1 shows a “diamond Sudoku” (with difficulty level 3). I grayed the cells with the clues to highlight the pattern.

9781484209967_Fig17-01.jpg

Figure 17-1. A diamond Sudoku

To implement designer puzzles, you need to modify sudoku_gen.c in such a way that instead of removing clues by pseudorandom choices, it removes the clues you want to have removed. You do this by replacing the code shown in Listing 15-6 with that shown in Listing 17-1.

Listing 17-1. sudoku_gen.c: code to Remove Clues for Diamond Sudokus

char *KEEP =
    ".1..1..1."
    "1..1.1..1"
    "..1...1.."
    ".1..1..1."
    "1..1.1..1"
    ".1..1..1."
    "..1...1.."
    "1..1.1..1"
    ".1..1..1."
    ;
for (int i = 0; i < 81; i++) {
  if (KEEP[i] == '.') {
    int k = i / 9;
    int j = i - k * 9;
    puzzle[k][j] = 0;
    grid[k][j] = 0;
    }
  }

These few statements let you choose what clues you keep in order to form nice patterns. A word of warning though: you have to leave at least 26 clues because, as you saw in Chapter 16, the Generator is not able to create puzzles with 25 clues or less. The diamond pattern consists of 28 clues.

If you set KEEP as in

char *KEEP =
    "..11111.."
    ".1.....1."
    "1..1.1..1"
    "1.......1"
    "1.......1"
    "1.1...1.1"
    "1..111..1"
    ".1.....1."
    "..11111.."
    ;

you obtain “smiley Sudokus” with 31 clues, like the puzzle shown in Figure 17-2 (difficulty level 2).

9781484209967_Fig17-02.jpg

Figure 17-2. A smiley Sudoku

With KEEP set as in

char *KEEP =
    ".11...11."
    "1111.1111"
    "11.111.11"
    "11..1..11"
    "11.....11"
    ".11...11."
    "..11.11.."
    "...111..."
    "....1...."
    ;

you obtain “heart Sudokus” with 40 clues, like the puzzle shown in Figure 17-3 (an easy one).

9781484209967_Fig17-03.jpg

Figure 17-3. A heart Sudoku

And one more example: Figure 17-4 shows you a “wave Sudoku” with 27 clues generated by setting KEEP as follows:

char *KEEP =
    "..1..1..1"
    ".1..1..1."
    "1..1..1.."
    ".1..1..1."
    "..1..1..1"
    ".1..1..1."
    "1..1..1.."
    ".1..1..1."
    "..1..1..1"
    ;

9781484209967_Fig17-04.jpg

Figure 17-4. A wave Sudoku

If you experiment with designer Sudokus by setting KEEP to different strings, you will discover that the Generator might take a very long time to create puzzles with particular patterns. In some cases, it will not be able to generate a puzzle at all. I believe that it has to do with the logic of Sudoku puzzles, rather than with the Generator’s implementation, but I see no way of proving it.

In any case, to generate designer Sudokus, you don’t need to use KEEP. You can use other mechanisms to select the clues. For example, if you replace the code shown in Listing 15-6 with

for (int k = 0; k < 9; k++) {
  for (int j = 0; j < 9; j++) {
    if (k != 4 && j != 4 && j != k && j != 8-k) {
      puzzle[k][j] = 0;
      }
    }
  }

you obtain an “asterisk Sudoku” with 33 clues as shown in Figure 17-5.

9781484209967_Fig17-05.jpg

Figure 17-5. An asterisk Sudoku

Symbolic Sudokus

As you never perform any mathematical operation with the numbers that appear in a Sudoku, you don’t really need to keep them as numbers. Any group of nine letters, symbols, or even words would do.

The Generator exploits the fact that the symbols used within Sudokus are in fact numbers from 1 to 9, but you can easily create symbolic Sudokus by “tweaking” the function save_html(). All you need to do is associate a different symbol with each number.

First, define the array to contain the association

char *symbols[10] = {"", "A", "B", "C", "D", "E", "F", "G", "H", "I"};

Then, use symbols[] as a look-up table when you insert the numbers into the HTML table. You do this by replacing

fprintf(fp, "<td class="c%d">%c</td>",
    kR % 3 * 3 + kC % 3, ((puzzle[i] == '0') ? ' ' : puzzle[i])
    );

with

fprintf(fp, "<td class="c%d">%s</td>",
    kR % 3 * 3 + kC % 3, symbols[(int)(puzzle[i] - '0')]
    );

With these two small changes (the code is already in save_html.c but switched off with a #define), the puzzle shown in Figure 16-5 becomes a symbolic Sudoku, as shown in Figure 17-6.

9781484209967_Fig17-06.jpg

Figure 17-6. A symbolic Sudoku—1

But why stop there?

By setting symbols[] to Unicode characters as in

char *symbols[10] = {"", "u260E", "u2622", "u262F", "u263C", "u263D",
    "u2658", "u269B", "u2665", "u266B"
    };

you can transform the same puzzle into that shown in Figure 17-7.

9781484209967_Fig17-07.jpg

Figure 17-7. A symbolic Sudoku—2

You can find the list of Unicode characters on the Web (e.g., one good site  is http://www.utf8-chartable.de/unicode-utf8-table.pl (accessed on February 7, 2015)).

Now, the next step is to use your own icons, isn’t it? No problem at all. All you need to do is create the images (of size 50x50 pixels), name them 1.jpg to 9.jpg and store them in the same folder where you keep the puzzle’s HTML pages. You will also have to create an image 0.jpg completely white. Then, replace the fprintf() statement with the following one:

fprintf(fp, "<td class="c%d" background="%c.jpg"> </td>",
    kR % 3 * 3 + kC % 3, puzzle[i]
    );

See an example in Figure 17-8, for which I used freely available icons I found on the Web at

http://www.smashingmagazine.com/2009/06/07/50-fresh-useful-icon-sets-for-your-next-design/

They are much nicer in color, but you get the idea.

9781484209967_Fig17-08.jpg

Figure 17-8. A symbolic Sudoku—3

Summary

This chapter has shown you how to personalize Sudokus. You can now create Sudokus with the clues that form a particular pattern or with symbols or images instead of numbers. Next, you will learn how to make complex Sudokus by joining together multiple 9x9 puzzles.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset