June 20, 2018
Hot Topics:

Building a Java ME CDC Application Using the SavaJe Phone

  • October 24, 2006
  • By Carl Quinn
  • Send Email »
  • More Articles »

Create the Game Engine

The Puzzles class is a simple singleton that serves as a Puzzle factory for ten predefined puzzles.

public class Puzzles {

   * Predefined puzzles. Each string is one puzzle, each full row
     separated by |'s.
   private static final String[] puzzles = {

   public static Puzzle newPuzzle(int index) {
      return new Puzzle(puzzles[index % puzzles.length]);

I generated and exported the puzzles by using the sudoku solver found at http://sudoku.sourceforge.net/. You can do the same to add new puzzles—simply export the generated puzzles in XML format, and cut the actual puzzle text from the middle and paste it into a string in the Puzzles class, adding '|' separators between rows. An obvious next logical addition to the app would be to read the puzzles directly from an external file in the original XML format.

The Puzzle class handles parsing of the puzzle definition string and holds the current state of the guesses. Any alternate formats would be handled here. Eventually, this class should handle saving and loading of puzzle solution states.

public class Puzzle {

   public final int[][] grid = new int[9][9];
   public final boolean[][] given = new boolean[9][9];
   public final int[][] answer = new int[9][9];

   public Puzzle(String text) {
      Reader reader = new CharArrayReader(text.toCharArray());
      for (int r = 0; r < 9; r++)
         for (int c = 0; c < 9; c++) {
            grid[r][c] = nextDigit(reader);
            given[r][c] = grid[r][c] > 0;
            if (given[r][c])
               answer[r][c] = grid[r][c];

Ironically, this simple class is where I kept running into the sneaky Java 5 trap. At one point, I introduced a couple of String calls to trim the definition strings. This innocuous method:

test = test.replace("|", "");

was actually added in Java 5, so it and others like it will need to be avoided.

Testing It Out

The quickest way to see the game running is to build and run the sudoku-se under Eclipse. It will run as a default Java Application run configuration. Use the keyboard to navigate and test out the game logic and rendering.

To see the game running on the phone, build the sudoku-xlet project under Eclipse. Then run the two scripts: mkdist to build the jar and dist dir, and tophone to copy the directory tree to the phone's SD. (You will need to adjust the paths in this second script for your machine and SD volume name). Plug the SD into the phone, select My Applications, and play Sudoku!

If your Xlet won't start at all on the phone, it is most like caused by an unresolved symbol (like one in the Java 5 JRE), or that the compiler is set to level 5 Java.


Java ME has come a long way since the early days. The latest CDC specs really cover a lot of ground, and SavaJe provides an incredibly rich platform. It's almost like a desktop in your hand. So many possible applications, so little time.

This is the first real Java ME app that I've written, and see how easy it was? Let me know if you see anything that I really botched, or of course if you have any suggestions. I know that there are quite a few fun things to add to this Sudoku game. You can coordinate any additions at http://code.google.com/p/sudoku-xlet/.

Further Reading/Resources

About the Author

Carl Quinn is a Software Engineer at Google [google.com]. He focuses primarily on Java infrastructure, libraries, and development tools. He can be reached at carl.quinn@gmail.com. He also co-hosts The Java Posse, a podcast devoted to Java news and the Java community, which can be found at http://javaposse.com or via the Google group http://groups.google.com/group/javaposse.

Page 4 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date