📄 move.java
字号:
/**
* This should only be used by Game.undo(). When a server move
* is permanently undone it becomes a variation.
**/
public void setIsVariation (boolean b) {
isVariation = b;
}
/*
* Add a new variation to this move (i.e., at this point in the
* game tree). If a variation at the same position already exists,
* then replace it.
*/
public void addVariation (Move m) {
if (variations == null)
variations = new Vector(1, 3);
for (int i = 0; i < variations.size(); i++) {
if (m.equals(variationAt(i))) {
variations.setElementAt(m, i);
return;
}
}
variations.addElement(m);
}
/**
* The user may remove unwanted variations, or a local scoring
* variation may be removed automatically when scoring mode exits.
*/
public boolean removeVariation (Move m) {
if (m == child) {
if (variations != null && variations.size() > 0) {
child = variationAt(0);
variations.removeElementAt(0);
}
else
child = null;
return true;
}
else if (variations != null) {
for (int i = 0; i < variations.size(); i++)
if (m == variationAt(i)) {
//Debug.println("Removing variation " + m);
variations.removeElementAt(i);
return true;
}
return false;
}
return false;
}
public boolean isBranch() {
return variations != null && variations.size() != 0;
}
public String toVariationString () {
StringBuffer buff
= new StringBuffer(" at move #" + (moveNumber() + 1) + " -- "
+ Character.toLowerCase(colorToChar(color()))
+ " " + toString());
Move m = this;
while (m.child() != null) {
m = m.child();
buff.append(", " + Character.toLowerCase(colorToChar(m.color()))
+ " " + m.toString());
}
return buff.toString();
}
/*
* Return the color of the move after the given move.
*/
public int nextColor () {
return nextColor(color);
}
public static int nextColor (int c) {
return (c == WHITE) ? BLACK : WHITE;
}
public void addKibitz (String kib) {
if (kib != null) {
if (kibitzes == null)
kibitzes = new Vector(4, 4);
kibitzes.addElement(kib);
}
}
// Return the final move in the trunk. If serverMoveOnly is true
// return the final server move in the trunk. Else go to the end.
public Move finalMove (boolean serverMoveOnly) {
Move m = this;
while (true) {
Move n = m.child();
if (n == null || (serverMoveOnly && n.isVariation))
return m;
else
m = n;
}
}
// Called after a move has been successfully placed.
// Inspired by the need to let a GameResultMove communicate the
// game result array to the GameWindow.
public void noteMovePlaced (StoneStasher ss) {} // default method
// Called after a move is undone via backup() and therefore via undo() also.
public void noteMoveUndone (StoneStasher ss) {} // default method
// A list of positions placed by the move.
public abstract PositionVector placedPositions (int size);
// A list of groups captured by the move.
public abstract SimpleGroupVector capturedGroups ();
// The number of stones placed by the move. (Could remove this.)
public abstract int numberOfStones ();
// Whether one move is the same as another. (Not too well defined.)
public abstract boolean equals (Move m);
// Whether the move occupies the given position.
public abstract boolean occupiesPosition (Position pos);
// Whether the move is legal at the current position in the given
// game. Various calculations will be stored in result if given.
// This is a combined method.
public boolean isLegal (Game g, boolean fast, ILMV result) {
if (parent == null) {
result.failureReason = "Move has no parent.";
return false;
}
// This case is because when a game is re-observed the server
// sends the last move again, so it's very probable that that's
// why the illegal move is occurring. Don't issue a warning.
else if (moveNumber == parent.moveNumber())
return false;
else if (moveNumber != parent.moveNumber() + 1) {
result.failureReason = "Move numbers out of order. ("
+ parent.moveNumber() + " " + moveNumber + ")";
return false;
}
else if (color == parent.color()) {
result.failureReason = "Color is same as parent move's color.";
return false;
}
else
return true;
}
public String colorToString (int color) {
return (color == BLACK) ? "Black" : "White";
}
public char colorToChar (int color) {
return colorToString(color).charAt(0);
}
protected void writeTimeLeft (PrintWriter out) {
if (timeLeft != -1)
out.print(colorToChar(color) + "L[" + timeLeft + "]");
}
// This is called as the last thing done by each subclass's method
// of the same name (except perhaps for the RootMove class). This
// assumes that the subclass's method has output a ';' for the node.
// +++ This sux, cuz if the subclass forgets to call super.writeSGF()
// the game save stops prematurely. Should do all the basic stuff
// in the SGF class and then just call writeSGF on moves to output
// additional move-specific info.
// Parameter n is the number of moves left to output on the current
// line of the output stream. More or less...comments go on their
// own line, and maybe other "large" properties.
public void writeSGF (PrintWriter out, Game game, int n) {
// Output any unhandled properties that were read in if this
// game was loaded from file.
/*
if (unhandledSGFproperties != null) {
for (int i = 0; i < unhandledSGFproperties.size(); i++) {
Vector pair = (Vector) unhandledSGFproperties.elementAt(i);
String prop = (String) pair.elementAt(0);
Vector vals = (Vector) pair.elementAt(1);
String[] pinfo = SGF.getPropInfo(prop);
out.print(prop);
if (pinfo != null)
// If there's no property info then this is not a known (to Ergo)
// SGF property. Just write it out as it was read in.
*/
if (kibitzes != null && kibitzes.size() > 0) {
out.print("\nC[");
for (int i = 0; i < kibitzes.size(); i++) {
SGF.writeText(out, (String) kibitzes.elementAt(i));
if (i != kibitzes.size() - 1)
out.print("\n");
}
out.print("]\n");
n = SGF.nodesPerLine;
}
// If the move has any variations then each variation and the
// child must be output as a GameTree (i.e., inside parens)...
if (variations != null && variations.size() > 0) {
for (int i = -1; variationAt(i) != null; i++) {
out.print("\n(");
variationAt(i).writeSGF(out, game, SGF.nodesPerLine);
out.print(")");
}
}
// Else the child is output as part of the current GameTree...
else if (child != null) {
if (n <= 0)
out.print("\n");
child.writeSGF(out, game, (n <= 0) ? SGF.nodesPerLine : n - 1);
}
}
// for debugging
public void print () {
if (!isVariation)
System.out.println();
System.out.print(this + " ");
if (variations != null && variations.size() > 0) {
System.out.print(" (");
for (int i = 0; i < variations.size(); i++) {
Move m = variationAt(i);
m.print();
}
System.out.print(")");
}
if (child != null)
child.print();
}
} // end class Move
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -