Skip to content

Instantly share code, notes, and snippets.

@maxzhang000
Created April 3, 2014 06:59
Show Gist options
  • Select an option

  • Save maxzhang000/9949523 to your computer and use it in GitHub Desktop.

Select an option

Save maxzhang000/9949523 to your computer and use it in GitHub Desktop.
I used to have just one asshole until i met this project
package player;
import java.util.ArrayList;
public class MyBoard extends Board {
private Piece[][] pieces = new Piece[8][8];
private ArrayList<Move> mostRecentMove = new ArrayList<Move>() ;
public MyBoard(){
for(int x = 0; x < 8; x ++){
for (int y = 0; y < 8; y++) {
pieces[x][y] = new Piece(Piece.NULL);;
}
}
}
/*
* (non-Javadoc)
* @see player.Board#getArray()
*/
public Piece[][] getArray(){
return pieces;
}
/*
* (non-Javadoc)
* @see player.Board#isFull()
*/
public boolean isFull(){
if(numPieces() == 20){
return true;
}else{
return false;
}
}
/*
* (non-Javadoc)
* @see player.Board#evaluateScore(int)
*/
public int evaluateScore(int givenColor){
int score = 0;
if(networkParser(givenColor)){
return Integer.MAX_VALUE;
}else if(networkParser((givenColor+1)%2)){
return Integer.MIN_VALUE;
}
int opponent = 0, self = 0;
for(int x = 0; x < 8; x ++){
for (int y = 0; y < 8; y++) {
if(!(pieces[x][y].getColor() == Piece.NULL)){
int connect = pieces[x][y].numConnections();
if (pieces[x][y].getColor()==givenColor){
self += connect;
}else{
opponent += connect;
}
}
}
}
score = self - opponent;
return score;
}
/*
* (non-Javadoc)
* @see player.Board#makeMove(player.Move, int)
*/
public void makeMove(Move m, int color){
if(m.moveKind == 1 && !checkInvalidMove(m, color)){
int x = m.x1;
int y = m.y1;
Piece p = new Piece(x, y, color, this);
pieces[x][y] = p;
Piece.update(p.getx(), p.gety(), this);
}else if (m.moveKind == 2 && !checkInvalidMove(m, color)){
int curX = m.x2;
int curY = m.y2;
int newX = m.x1;
int newY = m.y1;
Piece p = new Piece(newX, newY, color, this);
Piece.removePiece(curX, curY, this);
pieces[newX][newY] = p;
Piece.update(p.getx(), p.gety(), this);
}
mostRecentMove.add(m);
}
/*
* (non-Javadoc)
* @see player.Board#undoMove(player.Move, int)
*/
public void undoMove(Move m, int color){
if(m.moveKind == Move.ADD){
Piece.removePiece(m.x1, m.y1, this);
}else if (m.moveKind == Move.STEP){
Piece.removePiece(m.x1, m.y1, this);
makeMove(new Move(m.x2, m.y2), color);
mostRecentMove = new ArrayList<Move>(mostRecentMove.subList(0, mostRecentMove.size()-1));
}
mostRecentMove = new ArrayList<Move>(mostRecentMove.subList(0, mostRecentMove.size()-1));
}
/*
* (non-Javadoc)
* @see player.Board#lastMove()
*/
public Move lastMove(){
if(mostRecentMove.size() > 0){
return mostRecentMove.get(mostRecentMove.size()-1);
}else{
return null;
}
}
/*
* (non-Javadoc)
* @see player.Board#getAllMoves(int)
*/
public ArrayList<Move> getAllMoves(int playerColor){
if(isFull()){
return getAllStep(playerColor);
}else{
return getAllAddMoves(playerColor);
}
}
/*
* Returns an Arraylist of Move types that represent all possible moves of moveKind 1 that can be made by the
* player with playerColor
*/
private ArrayList<Move> getAllAddMoves(int playerColor){
ArrayList<Move> moveArray = new ArrayList<Move>();
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
Move m = new Move(x, y);
if(!checkInvalidMove(m, playerColor)){
moveArray.add(m);
}
}
}
return moveArray;
}
/*
* Returns an Arraylist of Move types that represent all possible moves of moveKind 2 that can be made by the
* player with playerColor
*/
private ArrayList<Move> getAllStep(int playerColor){
ArrayList<Move> moveArray = new ArrayList<Move>();
Piece current;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
current = pieces[x][y];
if(!(current.getColor() == Piece.NULL) && current.getColor() == playerColor){
for (int x1 = 0; x1 < 8; x1++) {
for (int y1 = 0; y1 < 8; y1++) {
Move m = new Move(x1, y1, x, y);
if(!checkInvalidMove(m, playerColor)){
if(pieces[6][2] != null && pieces[6][2].getColor() == playerColor
&& pieces[7][3] != null && pieces[7][3].getColor() == playerColor){
if((m.x1 == 7) && (m.y1 == 4) && (m.x2 == 2) &&(m.y2 == 1)){
checkInvalidMove(m, playerColor);
}
}
moveArray.add(m);
}
}
}
}
}
}
return moveArray;
}
/*
* (non-Javadoc)
* @see player.Board#checkInvalidMove(player.Move, int)
*/
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){
if (m.x1 == m.x2 && m.y1 == m.y2){
return true;
}
Piece tempPiece = pieces[m.x2][m.y2];
pieces[m.x2][m.y2] = new Piece(Piece.NULL);
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;
}
/*
* Helper method for checkInvalidMove, checks to see if placing a piece with playerColor at x and y would create a cluster
*/
private boolean wouldMakeTrio(int x,int y,int playerColor){
if (adjPieces(x,y,playerColor) > 1){
return true;
}
for (int a = x-1; a < x+2; a++) {
for (int b = y-1; b < y+2; b++) {
if(a >= 0 && a < 8 && b >= 0 && b < 8){
if(pieces[a][b].getColor() == playerColor){
if (adjPieces(a,b,playerColor) > 1){
return true;
}
}
}
}
}
return false;
}
/*
* Helper method for checkInvalidMove, checks to see if the space with coordinates x and y is occupied
*/
private boolean isOccupied(int x, int y) {
if (hasPiece(x, y) == 1) {
return true;
} else {
return false;
}
}
/*
* Returns the total pieces present on the board
*/
private int numPieces(){
int numPieces = 0;
for (int x = 0; x<8 ;x++){
for (int y = 0; y<8 ;y++){
if (! (pieces[x][y].getColor() == Piece.NULL)){
numPieces++;
}
}
}
return numPieces;
}
/*
* Returns the total pieces present on the board with playerColor
*/
private int numPieces(int playerColor){
int numPieces = 0;
for (int x = 0; x<8 ;x++){
for (int y = 0; y<8 ;y++){
if (! (pieces[x][y].getColor() == Piece.NULL) && pieces[x][y].getColor() == playerColor){
numPieces++;
}
}
}
return numPieces;
}
/*
* Helper method for checkInvalidMove, checks to see if placing a piece with playerColor at space with coordinates x and y
* is placing a piece in an opponent's goal or a corner
*/
private boolean isInOppGoalOrCorner(int x, int y, int playerColor) {
if (playerColor == 1) {
if (y == 0 || y == 7) {
return true;
} else {
return false;
}
} else {
if (x == 0 || x == 7) {
return true;
} else {
return false;
}
}
}
/*
* Helper method for adjPieces, returns 1 if a space with coordinates x and y has a piece, returns 0 otherwise
*/
private int hasPiece(int x,int y){
if (pieces[x][y].getColor() == Piece.NULL){
return 0;
}else {
return 1;
}
}
/*
* Helper method for adjPieces, returns 1 if a space with coordinates x and y has a piece has a piece of a specific
* playerColor, returns 0 otherwise
*/
private int hasPiece(int x,int y,int playerColor){
if (pieces[x][y].getColor() == Piece.NULL){
return 0;
}else if (pieces[x][y].getColor() == playerColor){
return 1;
}else{
return 0;
}
}
/*
* Helper method for checkInvalidMove, returns the total amount of adjacent pieces of a specific playerColor at
* a space with coordinates x and y
*/
private int adjPieces(int x,int y,int playerColor){
int totalCount = 0;
for (int xVal = x-1 ; xVal < x+2 ; xVal++) {
for (int yVal = y-1; yVal < y + 2; yVal++) {
if (!(xVal < 0 || xVal > 7 || yVal < 0 || yVal > 7)){
totalCount += hasPiece(xVal, yVal, playerColor);
}
}
}
return totalCount;
}
/*
* (non-Javadoc)
* @see player.Board#networkParser(int)
*/
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() == Piece.NULL) && pieces[x][0].getColor() == playerColor){
networkTop[nonNullLength(networkTop)] = pieces[x][0];
if (hasTop == false){
hasTop = true;
}
}
if (!(pieces[x][7].getColor() == Piece.NULL) && 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, -1);
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].getColor() == Piece.NULL) && pieces[0][y].getColor() == playerColor){
networkLeft[nonNullLength(networkLeft)] = pieces[0][y];
if (hasLeft == false){
hasLeft = true;
}
}
if (!(pieces[7][y].getColor() == Piece.NULL) && 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;
}
/*
* 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 (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 (nonNullLength(checkCompleteNetwork) != 0 && pieceInArray(checkCompleteNetwork[nonNullLength(checkCompleteNetwork)-1],endPoints)){
return checkCompleteNetwork;
}
return new Piece[1];
}
}
}
return new Piece[1];
}
/*
* Helper method for networkSearch, determines if a Piece p is in the Piece[] pArray
*/
private boolean pieceInArray(Piece p, Piece[] pArray){
for (Piece check : pArray){
if (check != null && p.getx() == check.getx() && p.gety() == check.gety() && p.getColor() == check.getColor()){
return true;
}
}
return false;
}
/*
* Helper method for networkSearch, determines the length of Piece[] p subtracted by the number of null entries in the array
*/
private int nonNullLength(Piece[] p){
int length = 0;
while (p[length] != null){
length ++;
}
return length;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString(){
String str = "";
for (int y = 0; y < 8; y++) {
str = str +"|";
for (int x = 0; x < 8; x++) {
if(pieces[x][y]!=null){
if(pieces[x][y].getColor()==Piece.NULL){
str = str +".";
}else if(pieces[x][y].getColor()==1){
str = str +"O";
}else{
str = str +"B";
}
}
}
str = str + "\n";
}
return str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment