Skip to content

Instantly share code, notes, and snippets.

@maxzhang000
Last active August 29, 2015 13:58
Show Gist options
  • Save maxzhang000/9961793 to your computer and use it in GitHub Desktop.
Save maxzhang000/9961793 to your computer and use it in GitHub Desktop.
Ok i need help
public boolean networkParser(int playerColor) {
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].getColor() == playerColor){
networkTop[x] = pieces[x][0];
if (hasTop == false){
hasTop = true;
}
}
if (pieces[x][7].getColor() == playerColor){
networkBottom[x] = pieces[x][7];
if (hasBottom == false){
hasBottom = true;
}
}
}
if (hasTop&&hasBottom){
Piece[] network;
for (Piece startPiece : networkTop){
if (startPiece != null){
Piece[] start = new Piece[10];
start[0] = startPiece;
network = networkSearch(start,1,networkBottom, -1);
if (network.length != 1){
return true;
}
}
}
}
}else if (playerColor == 1){
hasLeft = false;
hasRight = false;
networkLeft = new Piece[8];
networkRight = new Piece[8];
for (int y = 0;y<8;y++){
if (pieces[0][y].getColor() == playerColor){
networkLeft[y] = pieces[0][y];
if (hasLeft == false){
hasLeft = true;
}
}
if (pieces[7][y].getColor() == playerColor){
networkRight[y] = pieces[7][y];
if (hasRight == false){
hasRight = true;
}
}
}
if (hasLeft&&hasRight){
Piece[] network;
for (Piece startPiece : networkLeft){
if (startPiece != null){
Piece[] start = new Piece[10];
start[0] = startPiece;
network = networkSearch(start,1,networkRight,-1);
if (network.length != 1){
return true;
}
}
}
}
}
return false;
}
/*
* Recursive method that returns a network if found, returns a empty network with length 1 otherwise
* Takes in a Piece[] currentConnections storing the current pieces, in order of connection, in a potential network, the
* number of connections in such a network, a Piece[] off the potential endpoints of the network, and a integer
* representing the direction of the last connection that was made.
*/
private Piece[] networkSearch (Piece[] currentConnections,int curConnections,Piece[] endPoints,int badDir){
if (nonNullLength(currentConnections) >= 6 && pieceInArray(currentConnections[nonNullLength(currentConnections)-1],endPoints)){
return currentConnections;
}
if (curConnections<10){
for(int x = 0; x<8; x++){
if (currentConnections[curConnections-1] != null &&
currentConnections[curConnections-1].getSameConnection(x) != null &&
currentConnections[curConnections-1].getSameConnection(x).getColor() != Piece.NULL &&
!(pieceInArray(currentConnections[curConnections-1].getSameConnection(x),currentConnections))&& x!=badDir){
Piece[] updatedConnections = currentConnections;
updatedConnections[curConnections] = currentConnections[curConnections-1].getSameConnection(x);
Piece[] checkCompleteNetwork = networkSearch(updatedConnections,curConnections+1,endPoints,x);
if (checkCompleteNetwork.length >1){
return checkCompleteNetwork;
}
}
}
}
return new Piece[1];
}
private int nonNullLength(Piece[] p){
int length = 0;
for(Piece checkNull : p){
if (checkNull != null){
length ++;
}
}
return length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment