Skip to content

Instantly share code, notes, and snippets.

@maxzhang000
Created April 2, 2014 09:31
Show Gist options
  • Select an option

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

Select an option

Save maxzhang000/9930909 to your computer and use it in GitHub Desktop.
myboard comments
package player;
import java.util.ArrayList;
public class MyBoard extends Board {
private Piece[][] pieces = new Piece[8][8];
private ArrayList<Move> mostRecentMove;
public MyBoard(){
for(int x = 0; x < 8; x ++){
for (int y = 0; y < 8; y++) {
pieces[x][y] = new NullPiece();
}
}
}
public Piece[][] getArray(){ //Returns a two dimensional Piece array of all the pieces on the board
return pieces;
}
public boolean isFull(){ //Returns true if the board has the maximum amount of pieces and false otherwise
if(numPieces() == 20){
return true;
}else{
return false;
}
}
public int evaluateScore(int color){ //Evaluates the how favorable a move is and returns that score as an Integer
int total = 0, opponent = 0, self = 0;
for(int x = 0; x < 8; x ++){
for (int y = 0; y < 8; y++) {
if(!(pieces[x][y] instanceof NullPiece)){
int connect = pieces[x][y].numConnections();//Score is based on how many more connections each Piece has than the opponents Pieces
if (pieces[x][y].getColor()==color){
self += connect;
}else{
opponent += connect;
}
}
}
}
return self - opponent;
}
public void makeMove(Move m, int color){// Executes the actions described in a move
if(m.moveKind == 1 && !checkInvalidMove(m, color)){//AddMove
int x = m.x1;
int y = m.x2;
Piece p = new Piece(x, y, color, this);
pieces[x][y] = p;
}else if (m.moveKind == 2 && !checkInvalidMove(m, color)){//StepMove
int curX = m.x1;
int curY = m.y1;
int newX = m.x2;
int newY = m.y2;
Piece p = pieces[curX][curY];
pieces[curX][curY] = new NullPiece();
pieces[newX][newY] = p;
p.update(p.getx(), p.gety(), this); //updates the connections for the point that was moved
}
mostRecentMove.add(m); //adds this move to mostRecentMove ArrayList
}
public void undoMove(Move m, int color){//Reverses a move, used in AlphaBeta to check the score of a certain move
//TODO write this
if(m.moveKind == Move.ADD){
//THIS HAVE TO BE STATIC removePiece(m.x1, m.y1, this);
}else if (m.moveKind == Move.STEP){
//removePIECE //TODO
makeMove(new Move(m.x1, m.y1), color);
mostRecentMove = (ArrayList<Move>)mostRecentMove.subList(0, mostRecentMove.size()-1); //remove this relocation move
}
mostRecentMove = (ArrayList<Move>)mostRecentMove.subList(0, mostRecentMove.size()-1); //remove chosen move
}
public Move lastMove(){//Returns the last index of mostRecentMove as a Move
if(mostRecentMove.size() > 0){
return mostRecentMove.get(mostRecentMove.size()-1);
}else{
System.out.println("Nothing");
return null;
}
}
public ArrayList<Move> getAllMoves(int playerColor){//Returns ArrayList of all the Moves made by a certain player
if(isFull()){
return getAllStep(playerColor);
}else{
return getAllAddMoves(playerColor);
}
}
private ArrayList<Move> getAllAddMoves(int playerColor){//Returns ArrayList of all AddMoves made by a certain player
// Two different types: Add moves and Step Moves
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;
}
private ArrayList<Move> getAllStep(int playerColor){//Returns ArrayList of all StepMoves made by a certain player
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 instanceof NullPiece) && current.getColor() == playerColor){
for (int x1 = 0; x1 < 8; x1++) {
for (int y1 = 0; y1 < 8; y1++) {
Move m = new Move(x,y, x1, y1);
if(!checkInvalidMove(m, playerColor)){
moveArray.add(m);
}
}
}
}
}
}
return moveArray;
}
public boolean checkInvalidMove(Move m, int playerColor){//Returns True if a move by a certian player is invalid,otherwise returns False
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 wouldMakeTrio(int x,int y,int playerColor){//Helper method for checkInvalidMove, returns True if a certain move would allow a trio to be made, False otherwise
if (adjPieces(x,y,playerColor)<2){
return false;
}else{
return true;
}
}
private boolean isOccupied(int x, int y) {//Helper method for checkInvalidMove, returns True if a coordinate on the Board has a Piece returns False otherwise
if (hasPiece(x, y) == 1) {
return true;
} else {
return false;
}
}
private int numPieces(){ //returns number of Pieces on the board as an Integer
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){ //returns number of Pieces of a certain color on the board as an Integer
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) {// Helper method for checkInvalidMove returns true if spot is in the opponent's goal or a corner,returns False otherwise
// 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){ //helper method for adjPieces and returns 0 if there is a NullPiece, 1 otherwise
if (pieces[x][y] instanceof NullPiece){
return 0;
}else {
return 1;
}
}
private int hasPiece(int x,int y,int playerColor){//helper method for adjPieces and returns 0 if there is a NullPiece, 1 if the Piece's playerColor is the same as the playerColor arguement
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){//helper method for wouldMakeTrio, returns the number of pieces around a piece as an Integer
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 connection
*/
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);
if (network.length != 1){
return true;
}
}
}
}
return false;
}
private Piece[] networkSearch (Piece[] currentConnections,int curConnections,Piece[] endPoints){//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))){
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){//returns True if Piece p is in pArray, false otherwise
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){//returns an integer representing the number on non-null indexes in an array
int length = 0;
while (p[length] != null){
length ++;
}
return length;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment