Created
April 2, 2014 10:35
-
-
Save maxzhang000/9931686 to your computer and use it in GitHub Desktop.
lomo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean networkParser(int playerColor) {//returns True if a network exists for a certain player, False otherwise. | |
| //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,-1); | |
| if (network.length != 1){ | |
| return true; | |
| } | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| private Piece[] networkSearch (Piece[] currentConnections,int curConnections,Piece[] endPoints,int badDir){//recursive method that returns a network if found, returns a empty network with length 1 otherwise | |
| 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)) | |
| && x!=badDir){ | |
| Piece[] updatedConnections = currentConnections; | |
| updatedConnections[curConnections] = currentConnections[curConnections-1].getSameConnection(x); | |
| Piece[] checkCompleteNetwork = networkSearch(updatedConnections,curConnections+1,endPoints,x); | |
| if (pieceInArray(checkCompleteNetwork[nonNullLength(checkCompleteNetwork)-1],endPoints)){ | |
| return checkCompleteNetwork; | |
| } | |
| return new Piece[1]; | |
| } | |
| } | |
| } | |
| return new Piece[1]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment