JSudoku

Yet another Sudoku program

Main interface

Down to the basics, here’s what I did:

  1. Generate the whole board with backtracking, randomizing the number I chose at each step and remember the choice, this assures that the board is random, while still keep the algorithm run in an acceptable time (it doesn’t have to regenerate a long track of numbers if it get into some sticky situation). Sudoku board have to keep up with a number of rules that make up a complex vector class, lost in there and you’re done. The other method to generate the board was to swap the rows/column/numbers, it’s safe and much faster but it took more time to implement, and it generates only a subclass of the possible Sudoku class, but it’s a good choice if you don’t want recursive calls in your program.
  2. Difficulty: I implemented a simple, but it’s not good for real application, I think. A good difficulty implementation should have taken humans’ deduction rules into consideration (you should know what are these these if you’ve played Sudoku before), and the algorithm should also make sure that with the given cells, only one solution is possible, this is a continuous generate-remove-check for solutions-remove loop and it’s complex to implement. So, I have only removed a number of cells based on difficulty, the more difficulty chosen, the less given cell there are. This implementation suffers from the above points.
  3. Build a usable Sudoku class’ interface: I deducted from coding that you need to store both the solution and the playing board in the class, provide board and solution get method, the set move method for the board should only accept valid moves, this way you only need to count the number of moves played to know if the player have won or not. This has the drawback that you can’t store invalid values in the board (to visually notify the player of their wrong moves), but it keeps your code clean of unnecessary checks.
  4. Build the interface: If you choose to manually generate the JFrame, JButton and uses a loop to create JTextField, it saves time to point-and-click creating the cells, but you will have to manually calculate the button and frame’s position to get a nice interface; the other way around, you’ll have to create JTextFields 81 times. I choose a hybrid approach: use the designer to design the form, add a JPanel to it, and then add the JTextField in the user interface’s constructor.
  5. Just for kicks: I add a key press handler into the text fields, the handler catches input, and if it’s not a valid number, not correct according to Sudoku rules or some other reason, the cell will be highlighted so the user can see it easily, the hint function is implemented in this way too: it compares what the user have to the solution and highlight the differences.

Creating text fields

    /** Creates new form SudokuInterface */
    public SudokuInterface() {
        initComponents();

        // Automatically arranges the cells into a grid-like layout
        jPanel1.setLayout(new GridLayout(Sudoku.ROWS, Sudoku.COLUMNS));
        fields = new JTextField[Sudoku.ROWS * Sudoku.COLUMNS];
        for (int i = 0; i < Sudoku.ROWS * Sudoku.COLUMNS; i++) {
            fields[i] = new JTextField(1);
            fields[i].setSize(66, 66);
            fields[i].setFont(cellFont);
            fields[i].setName(Integer.toString(i));
            fields[i].addKeyListener(cellInputHander);
            fields[i].setHorizontalAlignment(JTextField.CENTER);
            fields[i].setEnabled(false);
            jPanel1.add(fields[i]);
        }

        // First screen
        String intro = "JSudoku";
        for (int i = 1; i < intro.length() + 1; i++) {
            fields[4 * Sudoku.ROWS + i].setText(intro.charAt(i - 1) + "");
        }

    }

Handling key input

    private final KeyListener cellInputHander = new KeyListener() {

        /**
         * Controls, the user's input, let them input invalid data and display it
         * to avoid confusing the user (higher usability), but this won't be
         * reflected in the actual game, we have to keep track of which cells are
         * invalid, resulting in this complex implementation
         */
        public void keyTyped(KeyEvent e) {
            e.consume();
            int move = 0;
            String moveString = e.getKeyChar() + "";
            try {
                move = Integer.parseInt(moveString);
            } catch (Exception exception) {
                return;
            }
            // If the move is invalid, do nothing, the key won't appear
            int cellIndex = Integer.parseInt(e.getComponent().getName());
            if (!game.set(cellIndex, move)) {
                ((JTextField)e.getSource()).setForeground(cellIncorrectForeground);
                jLabel3.setText("You have just performed an invalid move");
                isInvalid[cellIndex] = true;
            } else {
                ((JTextField)e.getSource()).setForeground(cellEnabledForeground);
                jLabel3.setText("");
                isInvalid[cellIndex] = false;
            }
            ((JTextField)e.getSource()).setText(moveString);
            if (game.won()) {
                JOptionPane.showMessageDialog(rootPane, "You have solved the puzzle!", "Congratulation", JOptionPane.INFORMATION_MESSAGE);
                newGame(lastDifficulty);
            }
        }

        public void keyPressed(KeyEvent e) {

        }

        public void keyReleased(KeyEvent e) {

        }
    };

Move checking
Like I said, there’s plenty of room for improvement, so here’s the source code if you want to do just that 😉

The webgame storm

It was not so long ago the web was still a bunch of text-only HTML without CSS and stuff. Now it can open your documents, let you communicate, catch the latest scoop on entertainment, then why not play games on it?

Before, the web was designed to deliver text-based content. Most related technologies are also text-oriented: CSS, XML, AJAX, and JavaScript. But now, as browsers have got more mature and text features are saturated among competitors, there goes the performance race. JavaScript got faster, as JIT compiling and other interpreted language technologies are improved. Now games are feasible and the web game industry began to rise. GIF for sprites, PNG for effects, JavaScript is capable of image processing is far than enough to make a game possible.

For web game, you know I don’t mean those simple flash games you play at work to make your boss mad, but instead role playing games that will take you an indefinite amount of time to play, just like Lineage or Ragnarok. Those game is also a kind of society, where players can communicate with each other, thus games could be considered a part of the 2.0 hype.

This is far from being an exhaustive review of web games, but instead the experiences I have with them.

Gaia online

Possibly the most influential web game now is Gaia Online, a collection of game and community activities. As the biggest, it also causes most controversial problems (say pedophilias). It’s not a single game with a single story but it does have role playing elements like quests. Nevertheless, it’s nice interface and huge community is quite attractive to those who have nothing else to do. Gaia also sure inspired a lot of modern web games.

Gaia online town

Gaia online town

Image: CNET

Gameforge

Gameforge is an influential game maker emerged recently. Though their games are pretty much in beta stage, their international approach (provides as many language as possible) seem to be effective. Based on the number of new games they release each year, it’s clear that they made quite a lot selling enhancements to their games. Those enhancements does not provide a real advantage over those who don’t have time, but that’s a right move: they attracted a huge number of free players that even the slightest advantage will have effect toward thousands of another players.

Most of their games are real-time. That is, moving armies or explore and area will cost you hours in real life. It’s up to you when to care about the game but the more you care about it, the more successful you are. It’s like the Japanese’s Tamagochi where your chicken is now a gladiator or an empire :P.

They also don’t have a nice interface like other producers. I’m unsure why but all their games look pretty clumsy to me

Ikariam is the most recently Gameforge game I played. You can build cities in an ancient Rome theme. You build cities on islands with a specific resource. Among them: Wine, Marble, Crystal and Sulphur. You can freely develop your empire, research to improve technologies, build troops to defend your empire or wage war at another, trade and exercise diplomacy with other empires. Addictive and time-consuming. Though you don’t have to wait for the buildings to build, you are always at risk getting attacked if you are not prepared; and even if you did prepared, you might get attacked by someone stronger anyways :p

Ikariam city view

Ikariam city view

Before, I have also played Gladiatus, where you play the role of a Roman gladiator. You can fight others, work to earn money, do quest to gain fame etc. Simple interface, you don’t actually see the gladiators combat and I get bored after a while doing repetitive actions: work – quest – explore – buy / sell items – dungeon – work, and get bashed by stronger players (though their levels are lower). It’s still a mystery how they get so many gold to win at auctions ;).

Other Gameforge games include bitefight, kingsage and ogame.

Though those games are of great entertainment value, Gameforge is quite stingy to rely that much on volunteers to exercise control on the game. Even when it cost them almost nothing hiring those volunteers (they pay by in game bonuses), they are still reluctant to hire some more. It’s funny when they don’t allow multiple accounts in their rules but doesn’t have any automatic control over user registration. Instead they rely on 5 game operators to ban each of the violating accounts. Imagine when 30% of the server of 10 thousand players decide to go against the rule, what will happen?

Well, that is happening already and it made those games tasteless to me now, since Vietnamese are so clever to dodge the rules to satisfy their deflated self-esteem in games :(. What’s the good for that anyways? Games are games and they still spend hours to fight in game, and then take another hour to fight on the forum. In the end, it turns out that all the big shots are multis 🙂 (Note that multi account players have a huge advantage over those who don’t, say, a 1000% increase in resources by manipulating the clones).

I’m off until those games get better so I can play it the way I want, undisturbed by idiots who have no reasoning capabilities. I’m giving away my accounts for Ikariam and Gladiatus for someone who can take care of them under one condition that I can borrow them sometimes :). My Ikariam account is 4 months old on Gamma and has 3 cities: level 16 -16 -15. The Gladiatus one is somewhere around level 19. Email me if you have inquiries about this.

Aurora blade

A RPG, where you can actually cast spell, do action and handle items, which you once thought it wouldn’t possible without a 2 GB client :P. Its graphics is quite nice though the image compression is somehow flawed (you can see jagged JPGs all around). Nice game with rich content, from monster fighting, quests to game events. Though it’s criticized for copying many designs from Ragnarok and WoW, it still take quite a lot of effort to build this game and integrate all the graphics. Cheers for the Chinese! (IGG is founded by Chinese).

In Vietnam

Though the big guys have developed quite a stand in this business with quality games and strategic moves, smaller publisher and developers still find their foothold somewhere. Sadly, most of the games in Vietnam are still just translations from foreign developers (again, Chinese). Though they employed large advertisement campaign and sexy cosplayers I still find little interest in them. First, they are from China. What’s so great about Chinese games? Or are they paid by the Chinese to accomplish the cultural elimination the Chinese was unable to complete years ago? Second, under their colorful interface, you can barely find the register link (sometimes you actually have to ask to be shown the register link :P).

There are quite a lot of stuff to entertain you on the net already. Web games are still at the beginning of their era. Even though they have come over technical challenges, most of them still don’t fit the average user’s playing habit. For any SimCity fanatic out there, Ikariam is like a disater where your beloved city get ripped of its bone by aliens from the next planet :P. Each of the game has its own strength and weakness, competition between them would be a fun thing to watch in the future.

In Vietnam, I don’t think any of those games will make a difference.

  1. The current economic crisis have driven away some hardcore player. They have to make a living!
  2. There are fewer clients who is willing to buy virtual stuff to support a game.
  3. Unless Vietnamese gamers’ barbaric and lawless attitude is changed, those game won’t attract the more educated classes. Playing along with idiots is pretty irritating. (LEEEEEROY!)

End of another lengthy post 😛