Skip to content

Instantly share code, notes, and snippets.

@maxzhang000
Created March 28, 2014 04:09
Show Gist options
  • Select an option

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

Select an option

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

Revisions

  1. maxzhang000 created this gist Mar 28, 2014.
    149 changes: 149 additions & 0 deletions MyBoard
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,149 @@
    package player;

    public abstract class MyBoard {

    /**
    * @param args
    */
    private Piece[][] pieces = new Piece[8][8];
    public boolean isValidMove(Move m){
    return false;
    }

    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 (isOccupied(m.x1,m.y1)||isInOppGoalOrCorner(m.x1,m.y1,playerColor)||wouldMakeTrio(m.x1,m.y1,playerColor)){
    return true;
    }
    }
    else {
    return false;
    }
    }

    private boolean wouldMakeTrio(int x,int y,int playerColor){
    if (adjPieces(x,y,playerColor)<2){
    return false;
    }else{
    return true;
    }
    }

    private boolean isOccupied(int x,int y){
    if (hasPiece(x,y)==1){
    return true;
    }else{
    return false;
    }
    }

    private int numPieces(){
    int numPieces = 0;
    for (int x = 0; x<8 ;x++){
    for (int y = 0; y<8 ;y++){
    if (! pieces[x][y] instanceof NullPiece){
    numPieces++;
    }
    }
    }
    return numPieces;
    }

    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] instanceof NullPiece && pieces[x][y].getColor() == playerColor){
    numPieces++;
    }
    }
    }
    return numPieces;
    }

    private boolean isInOppGoalOrCorner(int x,int y,int playerColor){//black is top to bottom white is side to side
    if (playerColor == 0){
    if (y==0 || y==7){
    return true;
    }else{
    return false;
    }
    }else{
    if (x==0 || x==7){
    return true;
    }else{
    return false;
    }
    }
    }

    private int hasPiece(int x,int y){
    if (pieces[x][y] instanceof NullPiece){
    return 0;
    }else {
    return 1;
    }
    }

    private int hasPiece(int x,int y,int playerColor){
    if (pieces[x][y] instanceof NullPiece){
    return 0;
    }else if (pieces[x][y].getColor() == playerColor){
    return 1;
    }else{
    return 0;
    }
    }

    private int adjPieces(int x,int y,int playerColor){
    if (x == 0){
    if (y == 0){
    return hasPiece(x+1,y,playerColor)+hasPiece(x,y+1,playerColor)+hasPiece(x+1,y+1,playerColor);
    }else if (y>0 && y<7){
    return hasPiece(x+1,y,playerColor)+hasPiece(x,y+1,playerColor)+hasPiece(x+1,y+1,playerColor)+hasPiece(x,y-1,playerColor)+hasPiece(x+1,y-1,playerColor);
    }else{
    return hasPiece(x+1,y,playerColor)+hasPiece(x,y-1,playerColor)+hasPiece(x+1,y-1,playerColor);
    }
    }else if (x>0 && x<7){
    if (y == 0){
    return hasPiece(x-1,y+1,playerColor)+hasPiece(x-1,y,playerColor)+
    hasPiece(x,y+1,playerColor)+hasPiece(x,y,playerColor)+
    hasPiece(x+1,y+1,playerColor)+hasPiece(x+1,y,playerColor);
    }else if (y>0 && y<7){
    return hasPiece(x-1,y+1,playerColor)+hasPiece(x-1,y,playerColor)+hasPiece(x-1,y-1,playerColor)+
    hasPiece(x,y+1,playerColor)+hasPiece(x,y,playerColor)+hasPiece(x,y-1,playerColor)+
    hasPiece(x+1,y+1,playerColor)+hasPiece(x+1,y,playerColor)+hasPiece(x+1,y-1,playerColor);
    }else{
    return hasPiece(x-1,y,playerColor)+hasPiece(x-1,y-1,playerColor)+
    hasPiece(x,y,playerColor)+hasPiece(x,y-1,playerColor)+
    hasPiece(x+1,y,playerColor)+hasPiece(x+1,y-1,playerColor);
    }
    }else{
    if (y == 0){
    return hasPiece(x-1,y,playerColor)+hasPiece(x,y+1,playerColor)+hasPiece(x-1,y+1,playerColor);
    }else if (y>0 && y<7){
    return hasPiece(x-1,y,playerColor)+hasPiece(x,y+1,playerColor)+hasPiece(x-1,y+1,playerColor)+hasPiece(x,y-1,playerColor)+hasPiece(x-1,y-1,playerColor);
    }else{
    return hasPiece(x-1,y,playerColor)+hasPiece(x,y-1,playerColor)+hasPiece(x-1,y-1,playerColor);
    }
    }
    }

    /*
    * Will check if the board has a valid connnection
    */
    private int networkParser() {
    return 0;
    }

    }