Notes in Creating a Sudoku game in Perl

by Chenxi on May 18, 2010

This is the original words from Ryan Chadwick’s email, as the last challenge for us to practice programming in Perl :

Create a script that can create sudoku challenges.
This one is tricky and is probably best broken down into several steps. Below is a suggestion but you are free to approach it how you like.

* Create a script that can verify that a given sudoku grid is valid
* Create a script that can generate a full valid grid
* Create a script that can take a full valid grid and hide the required squares

Think about how you may best store the grid. Arrays and associative arrays may be best here.

As I am learning Perl from scratch, I would really like to that this challenge and see how far I can go.
I have crafted the first and easiest part – Create a script that can verify that a given sudoku grid is valid – more like a hybrid of Java programming style and some ugly-looking syntax from Perl.

You can get the script “sudoku_validator.pl” from here

However, this script is far from perfect. I use just a two-dimensional array inside the script as the target grid to validate; ideally, this script should take a input file, which contains the sudoku grid to validate, and of the format defined as:

1
2
3
4
5
6
7
8
9
5,1,8,3,6,7,4,2,9
9,4,7,5,2,1,6,3,8
3,6,2,8,9,4,7,5,1
8,3,4,2,1,9,5,7,6
6,7,5,4,8,3,9,1,2
2,9,1,6,7,5,3,8,4
4,5,6,1,3,2,8,9,7
7,2,3,9,4,8,1,6,5
1,8,9,7,5,6,2,4,3

This way of input is much easier than the standard-in from command line, where people can easily make mistakes and cannot reuse the entered grid.

I might refactor the code in sudoku_validator later. Now I am just too eager to move on to the next two tasks…

Comments on this entry are closed.

Previous post:

Next post: