Skip to content

Instantly share code, notes, and snippets.

@rossgoodwin
Created September 29, 2014 18:19
Show Gist options
  • Save rossgoodwin/21bf9a56daa246970db2 to your computer and use it in GitHub Desktop.
Save rossgoodwin/21bf9a56daa246970db2 to your computer and use it in GitHub Desktop.
Piece and Pawn Classes from Che55
import java.util.*;
char[] files = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
char[] ranks = {'1', '2', '3', '4', '5', '6', '7', '8'};
boolean enpassantOp = false;
Piece enpassantable;
Piece selected;
Piece[] pieces;
float[] position(char[] square) {
files = sort(files);
ranks = sort(ranks);
int x = Arrays.binarySearch(files, square[0]) * 80 + 70;
int y = 630 - Arrays.binarySearch(ranks, square[1]) * 80;
float[] pos = {x, y};
return pos;
}
char[] square(float[] position) {
int files_index = int((position[0] - 30) / 80);
int ranks_index = int((670 - position[1]) / 80);
char[] s = new char[2];
if (position[0] > 30 && position[0] < 670 && position[1] > 30 && position[1] < 670) {
char file = files[files_index];
char rank = ranks[ranks_index];
s[0] = file;
s[1] = rank;
}
return s;
}
class Piece {
char[] startSq;
int side;
float[] startPos = new float[2];
float[] currentPos = new float[2];
boolean beenCaptured = false;
Piece (char[] tempStartSq, int tempSide) {
startSq = tempStartSq;
side = tempSide;
startPos = position(startSq);
currentPos = startPos;
}
}
class Pawn extends Piece {
Pawn(char[] tempStartSq, int tempSide) {
super(tempStartSq, tempSide);
}
void display() {
this.shadow();
if (side == 1) {
stroke(137);
fill(255);
} else {
stroke(137);
fill(0);
}
smooth();
ellipseMode(CENTER);
ellipse(this.currentPos[0], currentPos[1], 20, 20);
}
void shadow() {
smooth();
noStroke();
fill(50, 50);
ellipseMode(CENTER);
if (selected == this) {
ellipse(currentPos[0], currentPos[1]+7, 20, 20);
} else {
ellipse(currentPos[0], currentPos[1]+4, 20, 20);
}
}
boolean legalmove(char[] move) {
char[] originSq = {move[0], move[1]};
char[] destSq = {move[2], move[3]};
boolean legal = false;
int horizontalDiff = Arrays.binarySearch(files, destSq[0]) - Arrays.binarySearch(files, originSq[0]);
int verticalDiff = Arrays.binarySearch(ranks, destSq[1]) - Arrays.binarySearch(ranks, originSq[1]);
char[] tgtSq = {destSq[0], char(int(destSq[1])+1+side*2)};
if (horizontalDiff == 0 && verticalDiff == side * 2 - 1) {
legal = true;
for (int i = 0; i < pieces.length; i++) {
if (pieces[i] != this && square(pieces[i].currentPos) == destSq) {
legal = false;
}
}
} else if (horizontalDiff == 0 && verticalDiff == side*4-2 && originSq == startSq) {
legal = true;
for (int i = 0; i < pieces.length; i++) {
if (pieces[i] != this && (square(pieces[i].currentPos) == destSq || square(pieces[i].currentPos) == tgtSq)) {
legal = false;
}
}
} else if (abs(horizontalDiff) == 1 && verticalDiff == -1 + side*2) {
for (int i = 0; i < pieces.length; i++) {
if (pieces[i] != this && pieces[i].side != this.side && square(pieces[i].currentPos) == destSq) {
legal = true;
} else if (enpassantOp && pieces[i] == enpassantable && pieces[i].side != this.side && Arrays.binarySearch(pieces, pieces[i]) < 16 && pieces[i].beenCaptured == false && square(pieces[i].currentPos) == tgtSq) {
legal = true;
}
}
}
return legal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment