# 棋盘的表示

MSN & Gmail & QQDL88250@gmail.com

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},
};

/**
* Move a chessman to destination. If the point at
* (<code>fromX</code>, <code>fromY</code>) has no any chessman, do 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 <code>Constants.MOVE_INVALID</code>,
* 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;
}
}

/**
* 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这个类是用于描述最近一次移动棋子的信息：
/*
* @(#)LatestMotion.java
* Author: 88250 <DL88250@gmail.com>, http://blog.csdn.net/DL88250
* Created on May 24, 2008, 5:03:23 PM
*
* This program is 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 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., 59 Temple Place - Suite 330, 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 do 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 who do 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;
}
}

（郁闷。。。。Blog的“插入代码”功能坏了－ －#）