Last active
August 29, 2015 13:57
-
-
Save maxzhang000/9911789 to your computer and use it in GitHub Desktop.
Yoloswago and all its associated methods
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 checkInvalidMove(Move m, int playerColor){ | |
if (m.moveKind == 0){ | |
return false; | |
}else if (m.moveKind == 1){ | |
if (numPieces(playerColor) == 10){ | |
return true; | |
}else{ | |
if (isOccupied(m.x1,m.y1)||isInOppGoalOrCorner(m.x1,m.y1,playerColor)||wouldMakeTrio(m.x1,m.y1,playerColor)){ | |
return true; | |
} | |
} | |
}else if (m.moveKind == 2){ | |
Piece tempPiece = pieces[m.x2][m.y2]; | |
pieces[m.x2][m.y2] = new NullPiece(); | |
if (isOccupied(m.x1,m.y1)||isInOppGoalOrCorner(m.x1,m.y1,playerColor)||wouldMakeTrio(m.x1,m.y1,playerColor)){ | |
pieces[m.x2][m.y2] = tempPiece; | |
return true; | |
} | |
pieces[m.x2][m.y2] = tempPiece; | |
} | |
return false; | |
} | |
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