Skip to content

Instantly share code, notes, and snippets.

@richardliaw
Forked from maxzhang000/gist:9911789
Created April 1, 2014 18:08
Show Gist options
  • Save richardliaw/9919672 to your computer and use it in GitHub Desktop.
Save richardliaw/9919672 to your computer and use it in GitHub Desktop.
private boolean networkParser(int playerColor) {
//find piece on left side and right side
boolean hasLeft,hasRight,hasTop,hasBottom;
Piece[] networkTop, networkBottom, networkLeft, networkRight;
if (playerColor == 0){
hasTop = false;
hasBottom = false;
networkTop = new Piece[8];
networkBottom = new Piece[8];
for (int x = 0;x<8;x++){
if (!(pieces[x][0] instanceof NullPiece) && pieces[x][0].getColor() == playerColor){
networkTop[nonNullLength(networkTop)] = pieces[x][0];
if (hasTop == false){
hasTop = true;
}
}
if (!(pieces[x][7] instanceof NullPiece) && pieces[x][7].getColor() == playerColor){
networkBottom[nonNullLength(networkBottom)] = pieces[x][7];
if (hasBottom == false){
hasBottom = true;
}
}
}
if (hasTop&&hasBottom){
Piece[] network;
for (Piece startPiece : networkTop){
Piece[] start = new Piece[10];
start[0] = startPiece;
network = networkSearch(start,1,networkBottom);
if (network.length != 1){
return true;
}
}
}
}else{
hasLeft = false;
hasRight = false;
networkLeft = new Piece[8];
networkRight = new Piece[8];
for (int y = 0;y<8;y++){
if (!(pieces[0][y] instanceof NullPiece) && pieces[0][y].getColor() == playerColor){
networkLeft[nonNullLength(networkLeft)] = pieces[0][y];
if (hasLeft == false){
hasLeft = true;
}
}
if (!(pieces[7][y] instanceof NullPiece) && pieces[7][y].getColor() == playerColor){
networkRight[nonNullLength(networkRight)] = pieces[7][y];
if (hasRight == false){
hasRight = true;
}
}
}
if (hasLeft&&hasRight){
Piece[] network;
for (Piece startPiece : networkLeft){
Piece[] start = new Piece[10];
start[0] = startPiece;
network = networkSearch(start,1,networkRight);
if (network.length != 1){
return true;
}
}
}
}
return false;
}
private Piece[] networkSearch (Piece[] currentConnections,int curConnections,Piece[] endPoints){
if (curConnections<10){
for(int x = 0; x<9; x++){
if (!(currentConnections[curConnections-1].getSameConnection(x) instanceof NullPiece) &&
!(pieceInArray(currentConnections[curConnections-1].getSameConnection(x),currentConnections))){
Piece[] updatedConnections = currentConnections;
updatedConnections[curConnections] = currentConnections[curConnections-1].getSameConnection(x);
Piece[] checkCompleteNetwork = networkSearch(updatedConnections,curConnections+1,endPoints);
if (pieceInArray(checkCompleteNetwork[nonNullLength(checkCompleteNetwork)-1],endPoints)){
return checkCompleteNetwork;
}
return new Piece[1];
}
}
}
return new Piece[1];
}
private boolean pieceInArray(Piece p, Piece[] pArray){
for (Piece check : pArray){
if (p.getx() == check.getx() && p.gety() == check.gety() && p.getColor() == check.getColor()){
return true;
}
}
return false;
}
private int nonNullLength(Piece[] p){
int length = 0;
while (p[length] != null){
length ++;
}
return length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment