Skip to content

Commit

Permalink
Add more comments, clean code, due to PR
Browse files Browse the repository at this point in the history
  • Loading branch information
tt committed Oct 2, 2018
1 parent c31e5b1 commit 25f74cf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
23 changes: 12 additions & 11 deletions src/main/java/featurecat/lizzie/gui/VariationTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void draw(Graphics2D g, int posx, int posy, int width, int height) {
YSPACING = (Lizzie.config.showLargeSubBoard() ? 20 : 30);
XSPACING = YSPACING;

posx1=posx;posy1=posy;width1=width;height1=height; //backup for mouse click event
posx1=posx;posy1=posy;width1=width;height1=height; //backup for mouse click event

// Draw background
g.setColor(new Color(0, 0, 0, 60));
Expand Down Expand Up @@ -149,7 +149,7 @@ public void draw(Graphics2D g, int posx, int posy, int width, int height) {
}


//clone from drawTree() method but draw nothing, just caculate (x,y)->BoardNode
//Clone from drawTree() method but draw nothing, just caculate (x,y)->BoardNode
public BoardHistoryNode inSubtree(int posx, int posy, int mouseX, int mouseY, int startLane, int maxposy, BoardHistoryNode startNode, int variationNumber)
{
// Finds depth on leftmost variation of this tree
Expand Down Expand Up @@ -205,13 +205,14 @@ public BoardHistoryNode inSubtree(int posx, int posy, int mouseX, int mouseY, in
posy -= YSPACING;
}
return null;
}

}

//Clone from draw() method but draw nothing, just caculate (x,y)->BoardNode
public BoardHistoryNode inVariationTree(int mouseX, int mouseY) {

//check if (x,y) is in the variation window
if (mouseX < posx1 || mouseX > posx1+width1 || mouseY < posy1 || mouseY > posy1+height1)
return null;
//check if (x,y) is in the variation window
if (mouseX < posx1 || mouseX > posx1+width1 || mouseY < posy1 || mouseY > posy1+height1)
return null;

// Use dense tree for saving space if large-subboard
YSPACING = (Lizzie.config.showLargeSubBoard() ? 20 : 30);
Expand All @@ -235,13 +236,13 @@ public BoardHistoryNode inVariationTree(int mouseX, int mouseY) {
}
return inSubtree(posx1 + xoffset, curposy, mouseX, mouseY,0, posy1 + height1, node, 0);

}
}

public int jumpVariationTree(int mouseX, int mouseY){
BoardHistoryNode node;
node = inVariationTree(mouseX, mouseY); //check if (x,y) hit proper branch node
if (node!=null)
Lizzie.board.jumpToAnyPosition(node);
return 0;
if (node==null) return -1; //check if any error occur
Lizzie.board.jumpToAnyPosition(node);
return 0;
}
}
58 changes: 32 additions & 26 deletions src/main/java/featurecat/lizzie/rules/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,27 @@ public boolean nextVariation(int idx) {
}


// jump anywhere in the board history tree. Written for mouse click navigation
// Jump anywhere in the board history tree. Written for mouse click navigation
public void jumpToAnyPosition(BoardHistoryNode targetNode) {
BoardHistoryNode srcNode, tarNode=targetNode, prevNode;

int[] tar = new int[512], src = new int[512]; int i=0 ,j=0, k=0;

BoardHistoryNode srcNode, tarNode, prevNode;

//tar[] to track path from target node to root node
//src[] to track path from source node to root node
int[] tar = new int[512], src = new int[512];

int i=0 ,j=0, k=0; //i is index for target node, j for source node, k is working variable

//find path from target node to root node
tarNode=targetNode;
prevNode=tarNode.previous();
while(prevNode != null){
for(k=0;k<prevNode.numberOfChildren();k++)
if (prevNode.getVariation(k) == tarNode)
tar[i++]=k;
tarNode = prevNode; prevNode=prevNode.previous();
}


//find path from source node to root node
srcNode=history.getCurrentHistoryNode();
prevNode=srcNode.previous();
while(prevNode != null){
Expand All @@ -512,32 +519,31 @@ public void jumpToAnyPosition(BoardHistoryNode targetNode) {
srcNode = prevNode; prevNode=prevNode.previous();
}

//compare tar[] with src[] , and try to find nearest branch
//Compare tar[] with src[] , and try to find nearest branch
for(k=0; k<Math.min(i,j);k++) {
if(tar[i-k-1] == src[j-k-1]) continue;
break;
}

if (k==i)
{
//target node is shorter, just move back
for (int m=0; m<j-k; m++) previousMove();
}
else if (k==j){

//Move to the target node from source node
if (k==i) {
//target node is shorter, just move back
for (int m=0; m<j-k; m++)
previousMove();

} else if (k==j) {
//source node is shorter, move forward
for (int m=i-k; m>0;m--)
nextVariation(tar[m-1]);
}

else {
// in the different branchs, must move back first, then move forword
for (int m=0; m<j-k; m++)
previousMove();
for (int m=i-k; m>0;m--){
nextVariation(tar[m-1]);
}
}
}
nextVariation(tar[m-1]);

} else {
// in the different branchs, must move back first, then move forword
for (int m=0; m<j-k; m++)
previousMove();
for (int m=i-k; m>0;m--)
nextVariation(tar[m-1]);
}
}


/*
Expand Down

0 comments on commit 25f74cf

Please sign in to comment.