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.