📄 defaultboardmodel.java
字号:
int b[] = {y , y , y - 1 , y + 1};
int tmpLife=0;
for (int i = 0 ; i < 4 ; i++){
if (!validatePoint(a[i], b[i]))
continue;
else {
int xx = a[i];
int yy = b[i];
if (getPoint(xx, yy).getState() == GoPoint.EMPTY){
//周围若有空点,tmpLife!=0,允许落子
tmpLife = tmpLife + 1;
}else {
//如果敌方棋子已死,则提掉
if (GoPoint.isEnemy(getPoint(x, y), getPoint(xx, yy))){
if (isDead(xx,yy)){
if (maybeProhibited){
if (isSingle(xx,yy)){
getPoint(x, y).setState(GoPoint.EMPTY);
throw new GoException("Prohibited put");
}
}
removeBody(xx,yy, move);
tmpLife = tmpLife + 1;
}
}
}
}
}
//如果不能提子,并且落子后为死子,则此处不能落子
if (tmpLife == 0){
if (isDead(x, y)){ //如果仍为死子,不许落子
getPoint(x, y).setState(GoPoint.EMPTY);
throw new GoException("Prohibited put");
}
}
moves_put.addElement(move);
getPoint(x, y).setNumber(moves_put.size());
setCurrent(x, y);
grids.fireDataChanged();
}
/*
public void setNumber(int col, int row, int number) {
getPoint(col, row).setSerialNumber(number);
grids.fireDataChanged();
}
public void showNumbers(int startIndex, int destIndex, int startNumber) {
for (int i = startIndex; i <= destIndex; i++) {
Movement move = (Movement) moves_put.elementAt(i);
getPoint(move.col, move.row).setSerialNumber(i + startNumber);
}
grids.fireDataChanged();
}
public void hideNumbers() {
for (int i = 0 ; i < getBoardSize() ; i++)
for (int j = 0 ; j < getBoardSize() ; j++){
getPoint(i, j).setSerialNumber(GoPoint.NONE);
}
}
*/
protected void removeBody(int x, int y, Movement move){
int a[] = {x - 1 , x + 1 , x , x};
int b[] = {y , y , y - 1 , y + 1};
int curState = getPoint(x, y).getState();
getPoint(x, y).setState(GoPoint.EMPTY);
if (move != null) move.addCaptive(x, y, getPoint(x,y).getNumber());
for (int i = 0 ; i < 4 ; i++){
if (!validatePoint(a[i], b[i]))
continue;
else {
int xx = a[i];
int yy = b[i];
if (getPoint(xx, yy).getState() == curState){
removeBody(xx, yy, move);
}
}
} // end for
}
/**
* get owner of the body
* @v: points in the body
*/
protected void findSpaceHolder(Vector v){
int holder = GoPlayer.UNKNOWN;
for (int i = 0 ; i < v.size() ; i++){
Point p = (Point) v.elementAt(i);
int x = p.x;
int y = p.y;
int a[] = {x - 1 , x + 1 , x , x};
int b[] = {y , y , y - 1 , y + 1};
for (int j = 0 ; j < 4 ; j++){
if (!validatePoint(a[j], b[j]))
continue;
else {
int xx = a[j];
int yy = b[j];
if (getPoint(xx, yy).getState() == GoPoint.BLACK){
if (holder == GoPlayer.UNKNOWN) {
holder = GoPlayer.BLACK;
}
else if (holder == GoPlayer.WHITE){
holder = GoPlayer.BOTH;
break;
}
}
if (getPoint(xx, yy).getState() == GoPoint.WHITE){
if (holder == GoPlayer.UNKNOWN){
holder = GoPlayer.WHITE;
}
else if (holder == GoPlayer.BLACK){
holder = GoPlayer.BOTH;
break;
}
}
}
}
} // end for
for (int i = 0 ; i < v.size() ; i++) {
Point p = (Point) v.elementAt(i);
getPoint(p.x, p.y).setHolder(holder);
}
}
/**
* get chessmen with same color and adjacent to each other
* @v: points in the body
*/
protected Vector getBody(int x, int y) {
clearAllSigns();
Vector v = new Vector();
createBody(x, y, v);
return v;
}
/**
* get chessmen with same color and adjacent to each other
* @v: points in the body
* @see #getBody(int x, in y)#
*/
protected void createBody(int x, int y, Vector v){
int a[] = {x - 1 , x + 1 , x , x};
int b[] = {y , y , y - 1 , y + 1};
int curState = getPoint(x, y).getState();
if (v != null) v.addElement(new Point(x, y));
getPoint(x, y).setSigned(true);
for (int i = 0 ; i < 4 ; i++){
if (!validatePoint(a[i], b[i]))
continue;
else {
int xx = a[i];
int yy = b[i];
if (getPoint(xx, yy).isSigned()) continue;
if (getPoint(xx, yy).getState() == curState){
createBody(xx, yy, v);
}
}
} // end for
}
/**
* check whether the chessman in position x,y have no neibor with the same color
*/
protected boolean isSingle(int x, int y){
int a[] = {x - 1 , x + 1 , x , x};
int b[] = {y , y , y - 1 , y + 1};
for (int i = 0 ; i < 4 ; i++){
if (!validatePoint(a[i], b[i]))
continue;
int xx = a[i];
int yy = b[i];
if (!GoPoint.isEnemy(getPoint(x, y), getPoint(xx, yy))) return false;
}
return true;
}
/**
* check whether the chessman in position x,y be in "dead" state
*/
protected boolean isDead(int x, int y){
clearAllSigns();
return noLife(x, y);
}
/**
* recursively check whether the chessmen are in "dead" state
*/
protected boolean noLife(int x, int y){
int a[] = {x - 1 , x + 1 , x , x};
int b[] = {y , y , y - 1 , y + 1};
getPoint(x, y).setSigned(true);
for (int i = 0 ; i < 4 ; i++){
if (!validatePoint(a[i], b[i]))
continue;
else {
int xx = a[i];
int yy = b[i];
if (getPoint(xx, yy).isSigned()) continue;
if (getPoint(xx, yy).getState() == GoPoint.EMPTY){
return false;
}
if (getPoint(xx, yy).getState() == getPoint(x, y).getState())
if (!noLife(a[i], b[i]))
return false;
}
}
return true;
}
private void setCurrent(int c, int r) {
if ((cur_col > 0) && (cur_row > 0)) {
getPoint(cur_col, cur_row).setStyle(GoPoint.GENERAL);
}
if ((c > 0) && (r > 0)) {
getPoint(c, r).setStyle(GoPoint.HIGHLIGHT);
}
cur_col = c;
cur_row = r;
}
/**
* clear signs
*/
private void clearAllSigns(){
for (int i = 0 ; i < getBoardSize(); i++)
for (int j = 0 ; j < getBoardSize(); j++){
getPoint(i, j).setSigned(false);
}
}
private boolean validatePoint(int col, int row) {
if ((col < 0) || (col >= getBoardSize()) || (row < 0) || (row >= getBoardSize()))
return false;
else
return true;
}
/**
* save qipu
* <chessman>
* <color>black</color>
* <x>13</x>
* <y>3</y>
* </chessman>
* /
public String getActionRecords() {
StringBuffer records = new StringBuffer();
records.appends("<records>\n");
for (int i = 0; i < moves_put.size(); i++) {
GoBoardAction action = (GoBoardAction) moves_put.elementAt(i);
records.appends("\t<chessman>\n");
records.appends("\t\t<color>");
if (action.getSource() == GoBoardAction.SOURCE_BLACK) {
records.appends("black");
}
else {
records.appends("white");
}
records.appends("</color>\n");
records.appends("\t\t<x>" + action.getX() + "</x>\n");
records.appends("\t\t<y>" + action.getY() + "</y>\n");
records.appends("\t</chessman>\n");
}
records.appends("</records>\n");
return records.toString();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -