5/5/10

Summary

This 3D Chinese chess game is completely written in Java. The basic GUI part is constructed with widget toolkit Swing, and the 3D graphics part is constructed with Java 3D API.

Game features:
  1. The game is written in Java, which means it can be played on web browser. Of course, it's necessary to modify pieces of code using, such as, Java applet.
  2. When you press left button of mouse on a piece, all possible moves of the piece will be displayed on the chess board. This is a big different from other similar Chinese chess game.
  3. This game allows you doing drag & drop operation on a piece to achieve movement. But most of other chess games don't.
Weak points:
  1. Chess AI of this game is weak. Actually, if I have much more knowledge about AI, I think I can write a better one than now.
  2. No shadows for the pieces. Since in Java 3D, the shadow is geometry object, and it's position must be dynamically computed while the piece is moving. So, creating shadows is a little bit hard. (Java 3D tutorial chapter 6 talks about how does shadow mechanism work.)

5/2/10

AI

This game supports three levels of chess AI which the level 3 is the most difficult one. The AI level can be changed through game option menu. The default level is 2.

Description of three AI levels:
  1. AI makes a random move. Even though the move is randomly picked from all possible moves. However, the probability that the AI move attacks your (red) pieces is bigger than the probability that it doesn't.
  2. Be careful, at this level, AI will try to attack your (red) General and defend black General as possible as it can.
  3. For each piece, AI assigns it an appropriate point. And for all positions on the board, AI gives them different points, that the more important position has more bigger point. For all possible moves, AI evaluates each of them and picks the best move which has biggest point. Of course, the level 1 and 2 are still valid at this level.

4/12/10

New piece appearance

Without Chinese character support in Java environment, when you run this chess game on your PC, you probably will get a view as same as following figure. Since each piece consists of a Chinese character and a body shape, and both of them are 3D objects. Therefore, if your PC doesn't have Chinese character font, such as "HG明朝B", "HG正楷書体-PRO" or "HGゴシックM" etc, then the Chinese character of the piece will not be rendered.

Thus, to avoid above case, I decided to use texture image to wrap each piece. The next two figures show that pieces with Chinese character images and figuer images on the surfaces, respectively.


In order to switch these two kinds of piece appearance, I added a new "Design" menu item to "Game" menu. Players can change piece appearance whenever they want.


4/4/10

Rule implementation (3)

So far, basic game rules have been completed. I used word "basic" which means two players actually can play the game together now. For example making a piece move one by one, changing game options, etc. I expect these rules work well. If it's necessary(maybe exist few logical bugs somewhere), I'll modify or add some rules later.

3/29/10

Game option & rule implementation (2)

An option item is added to game menu. The option dialog will be set visible when player click the option menu item. There are three game options:
  1. Show move hint - when player pick a piece, the board shows circles that indicate possible moves for the piece.
  2. Rotate board - in order to give player a clear view, the board automatically rotates on y-axis with π angle after each move. This option is only valid when two players play the game.
  3. Play sound - after player made a move, an action sound is played.
So far, rules for horse and chariot are done.

3/21/10

Limited undo & rule implementation (1)

As mentioned few days ago, it's not a good idea that allow players to undo each move they made. Since Java doesn't support infinite memory allocation neither other languages. Thus, I modified a piece of code, then each player only can undo 10 previous moves from now on.

Here is a link to Wikipedia page that talked about Xiangqi including its rule. I need to make a statement about rule of General. The original text is:

The general starts the game at the midpoint of the back edge (within the palace). The general may move one point either vertically or horizontally, but not diagonally. A general cannot move into a file which is occupied by the enemy general unless there is at least one piece positioned between the generals in the file. The general may not leave the palace.

In fact, in Chinese version of the rule, two generals may face to face. But, in such case, one general can leave the palace and attack another general directly. In other word, a general can move into a file which is occupied by the enemy general. In this case, the enemy general can attack your general directly. So, when you try to make a general move, be careful this case. In my implementation, I'll follow Chinese version of the rule for generals.

Above figure shows possible moves of red general. Since both right and left sides of the red general are red guards. Thus, its possible moves are one point forward and the black general's position.

At this point, rules for general, guard and elephant are done.


3/18/10

GUI update

This is the latest game board.

3/15/10

Undo

Player may click "Undo" menu item or type Ctrl+Z to execute undo operation.

When a player moved piece successfully, current piece positions will be stored in a list. Since there are 32 pieces in total. So, every bout will cost int[32] memory bytes.

Game doesn't write the list to disk file. Thus, it's better to allow players undo limited moves. But, at this point, assume that Java supports infinite memory allocation. In other words, game allows players to undo every move they did.

3/5/10

Drag & Drop piece

The coordinate system of the Java3D virtual universe is right-handed. The x-axis is positive to the right, y-axis is positive up, and z-axis is positive toward the viewer (computer screen). The above figure is quoted from Sun Java3D API tutorial chapter 2.

In above Figure-1, the board didn't rotate x-axis any angle.

In above Figure-2, the aboard rotated x-axis -PI/6.

In above Figure-3, the board rotated x-axis -PI/3. I'm going to use these three figures to talk about how I implemented drag & drop piece function in the game, and a tiny issue.

Roughly speaking, when player pressed left button of mouse on a piece, the board shows all possible moves using circles. While player dragging mouse, at background, some Java class staff calculates mouse coordinates and update the piece coordinates. Note, the mouse coordinates based on chessboard local coordinates. When player released left button, if the mouse position is inside of one of circles, the Java class staff automatically adjusts the piece's central to the circle's central.

Dragging a piece in 3D universe is not same as in 2D canvas. Since the sense of sight are different. From viewer position, in fact, circles in the Figure-3 are not circles. They are ellipses. So, when I rotate the board's x-axis more bigger angle, I need to adjust mouse's coordinates more. Specifically, while player dragging the mouse, I need to get the mouse current coordinates, and add a fixed quantity (especially y-axis coordinate) to it. Then set the sum coordinates to the piece.

The tiny issue is what the quantity should be. Because an appropriate value effects reality a little. Anyhow, drag & drop function is done.

2/25/10

Pieces

The rendering of 32 pieces has been completed. Each piece consists of a 3D Chinese character and a cylinder. The color of pieces, board and background will be modified later. So, the UI will look better. At this point, the Java3D rendering is almost done.

2/20/10

Game frame and chess board

This is a draft of user interface frame which contains two menu options on the menu bar. The chess board is made by rendering 9 vertical and 10 horizontal lines in 3D space.

There are 90 intersections on this chess board. Each of them has a tiny circle which is used to show possible move hints after player clicks a piece. In order to confirm all circles' position, they can be seen temporarily now.

2/16/10

Introduction of Xiangqi

Xiangqi, commonly called Chinese chess in English, is one of the most popular board games in China. According to recent research, it evolved from an ancient Chinese game called Liubo which was invented about 3,500 years ago.

The board is 9 lines wide and 10 lines long. Dividing the two opposing sides is a river (reference to Chu-Han contention) marked with traditional Chinese character '楚河汉界'.

Xiangqi isn't as well-known as chess game in the world. However, at least 1.3 billion Chinese people like this game.