The exploration of Java Chinese chess game procedure [2]--Board representation __java

Source: Internet
Author: User
The representation of a chessboard

Reprint please retain the author information:

Author: 88250

blog:http:/blog.csdn.net/dl88250

MSN & Gmail & qq:dl88250@gmail.com


In the Chess game program, we first want to determine the next chessboard-pieces of the data structure description.

At present, there are mainly two kinds of chessboard-chess (hereinafter referred to as "situation", situation) notation, one is "chessboard array", the other is "bit chessboard".

Since the chessboard was originally introduced in chess (8*8, just 64bits), Chinese chess is a 9*10 chessboard, which is not easy to use a bit board. However, the biggest advantage of the chessboard is the extraordinary high computational efficiency and space savings. Therefore, most of the game game or business game software is the use of a bit board as the basic data structure.

This paper represents a situation in the form of checkerboard array notation because it is easy for people to read the code to understand. Here, we have to make a decision: whether to use object-oriented development.

With object-oriented development, pieces can be abstracted to concrete, various search algorithms and controls can use some design patterns to obtain good code readability and easy modification. However, the core of the game program is to be efficient, the cost of the benefits of object technology is performance problems (I have tried to write this program in a very object-oriented perspective, inefficient). So we should develop this chess program in a process-oriented way (Java writing Process code, seemingly a bit nondescript--!).

After making a decision, the chessboard can be expressed as follows:

Chessboard = new int[][]{
{1, 0, 0, 4, 0,/**/0, 11, 0, 0, 8},
{2, 0, 3, 0, 0,/**/0, 0, 10, 0, 9},
{5, 0, 0, 4, 0,/**/0, 11, 0, 0, 12},
{6, 0, 0, 0, 0,/**/0, 0, 0, 0, 13},
{7, 0, 0, 4, 0,/**/0, 11, 0, 0, 14},
{6, 0, 0, 0, 0,/**/0, 0, 0, 0, 13},
{5, 0, 0, 4, 0,/**/0, 11, 0, 0, 12},
{2, 0, 3, 0, 0,/**/0, 0, 10, 0, 9},
{1, 0, 0, 4, 0,/**/0, 11, 0, 0, 8},
};

Among them, 0 means No child, 1~14 represents a chess piece respectively.

Here, there is no use of Java enum type, mainly beware of performance problems (JDK1.5 after the enum is also very object-oriented, hehe). Also, using constants instead of enumerations I don't think it's going to be too much of a hindrance to understanding a program (1 is a red car).

The only things you can do to move a piece are the following:
/**
* Move a Chessman to destination. If the point at
* (<code>fromx</code&gt, <code>fromY</code>) has no any chessman, doing nothing.
* There is three condition when we makemove a Chessman:
* @param fromX x coordinate of which Chessman do this move
* @param fromY y coordinate of which chessman do this move
* @param toX x coordinate of which Chessman ' s destination
* @param toY y coordinate of which Chessman ' s destination
* @return <ul>
* <li>if the (<code>fromy</code>, <code>fromY</code>) has no chessman or
* The move is invalid, returns &LT;CODE&GT;CONSTANTS.MOVE_INVALID&LT;/CODE&GT;
* Value of it equals to-100</li>
* <li>if move successfully, returns the destination ID. The ID maybe
* A Chessman ID (be Eaten) or 0 (empty chessboard grid) </li>
* </ul>
*/
Final public int makemove (int fromX, int fromY, int toX, int toY) {
int chessman = chessboard[fromx][fromy];
if (Chessman = = 0) {
Invalid move
return-100;
}

if (Isvalidmove (FromX, FromY, ToX, ToY)) {
Backup motion
int latestbeeaten = Chessboard[tox][toy];
Latestmotions.push (New Latestmotion (Chessman,
FromX, FromY,
ToX, ToY,
Latestbeeaten,
Isredgo));
Move
Chessboard[fromx][fromy] = 0;
Chessboard[tox][toy] = Chessman;
Isredgo =!isredgo;

return latestbeeaten;
} else {
Invalid move
return-100;
}
}

There is an important method in this method: Isvalidmove, used to determine whether a move is legal. There is also a historical Shifa record stack, used for Unmove:

/**
* Undo the latest motion.
*/
Final public void Unmove () {
Latestmotion latestmotion = Latestmotions.pop ();
Recovery move
Chessboard[latestmotion.fromx][latestmotion.fromy] = latestmotion.who;
Recovery eaten
Chessboard[latestmotion.tox][latestmotion.toy] = latestmotion.killed;
Recovery who ' s go
Isredgo = Latestmotion.isredgo;
}

Latestmotion This class is the information that describes the most recent move of a pawn:
/*
* @ (#) Latestmotion.java
* author:88250 <dl88250@gmail.com> http://blog.csdn.net/DL88250
* Created on May, 2008, 5:03:23 PM
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package Cn.edu.ynu.sei.chinesechess.infrastructure.common;

/**
* A Chessman ' s latest motion.
* @author 88250 <dl88250@gmail.com> http://blog.csdn.net/DL88250
* @version 1.1.0.0, May 28, 2008
*/
Final public class Latestmotion {

/**
* Who does this motion
*/
public int who;
/**
* x coordinate of Chessman ' location
*/
public int FromX;
/**
* Y coordinate of chessman ' location
*/
public int FromY;
/**
* x coordinate of Chessman ' s destination
*/
public int ToX;
/**
* Y coordinate of Chessman ' s destination
*/
public int ToY;
/**
* The Chessman killed other Chessman
*/
public int killed;
/**
* is red ' s turn?
*/
public Boolean Isredgo;

/**
* Constructor with parameters.
* @param who does the motion
* @param fromX x coordinate of Chessman ' s location
* @param fromY y coordinate of Chessman ' s location
* @param toX x coordinate of Chessman ' s destination
* @param toY y coordinate of Chessman ' s destination
* @param killed the Chessman killed other Chessman
* @param Isredgo is red ' s turn?
*/
Public latestmotion (int ' Who, int fromX, int fromY, int toX, int toY, int killed,
Boolean Isredgo) {
this.who = who;
THIS.FROMX = FromX;
this.fromy = FromY;
This.tox = ToX;
This.toy = ToY;
this.killed = killed;
This.isredgo = Isredgo;
}
}


At this point, we basically already can use this chessboard to carry on the game of Chinese Chess:

(Depressed ....) Blog's "Insert Code" function is broken--#)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.