I'm starting a new project for a Chess AI and am just doing the drag and drop selection for human players at the moment. The code I have below works fine, but when I start doing move generation it is going to be really important that the code is as fast as possible.
Right now more than one piece can be on a square (haven't coded a condition to prevent it yet) but later that will never happen. What I would like to be able to do is pick a square on the board (right now with the mouse) and know which long I should be doing my bitwise operations on (there is a long for white pawns, another for white rooks, another for white knights, ect). Right now I just test all of the longs in my array with a bitmask to see if they intersect the square I have selected (with the mouse). It would be ideal not to test all of the longs in an array and just point to the right element somehow, help would be greatly appreciated.
EDIT: I put in comments to help and highlighted the code I was talking about in red.
- boolean somethingSelected, wTurn;
- long mSelect;
- long[] pieces = new long[12];
- PImage[] pieceImgs = new PImage[12];
- void setup() {
- size(801, 801);
- stroke(128);
- pieceImgs[0] = loadImage("WhitePawn.png");
- pieceImgs[1] = loadImage("WhiteRook.png");
- pieceImgs[2] = loadImage("WhiteKnight.png");
- pieceImgs[3] = loadImage("WhiteBishop.png");
- pieceImgs[4] = loadImage("WhiteQueen.png");
- pieceImgs[5] = loadImage("WhiteKing.png");
- pieceImgs[6] = loadImage("BlackPawn.png");
- pieceImgs[7] = loadImage("BlackRook.png");
- pieceImgs[8] = loadImage("BlackKnight.png");
- pieceImgs[9] = loadImage("BlackBishop.png");
- pieceImgs[10] = loadImage("BlackQueen.png");
- pieceImgs[11] = loadImage("BlackKing.png");
- initialize();
- }
- void draw() {
- display();
- }
- void mousePressed() {
- somethingSelected = false;
- mSelect = 1L<<(7-mouseX/100+(7-mouseY/100)*8);
- if (wTurn) { // If it is White's turn...
- for (int i = 0; i < 6; i++) { // Test elements corresponding to White pieces
- if ((mSelect&pieces[i]) != 0) {
- somethingSelected = true;
- break;
- }
- }
- }
- else { // If it is Black's turn...
- for (int i = 6; i < 12; i++) { // Test elements corresponding to Black pieces
- if ((mSelect&pieces[i]) != 0) {
- somethingSelected = true;
- break;
- }
- }
- }
- }
- void mouseReleased() {
- boolean execute = true;
- if (mouseX <= 0 || mouseX >= width || mouseY <= 0 || mouseY >= height) execute = false;
- if (execute) {
- long newLoc = 1L<<(7-mouseX/100+(7-mouseY/100)*8);
- if (wTurn) { // If it is White's turn...
- for (int i = 0; i < 6; i++) { // Test elements corresponding to White pieces
- if ((mSelect&pieces[i]) != 0) {
- pieces[i] ^= mSelect;
- pieces[i] |= newLoc;
- wTurn = false;
- break;
- }
- }
- }
- else { // If it is Black's turn...
- for (int i = 6; i < 12; i++) { // Test elements corresponding to Black pieces
- if ((mSelect&pieces[i]) != 0) {
- pieces[i] ^= mSelect;
- pieces[i] |= newLoc;
- wTurn = true;
- break;
- }
- }
- }
- }
- somethingSelected = false;
- }
- void display() {
- int mouseLoc = 7-mouseX/100+(7-mouseY/100)*8;
- boolean showTargetLoc = true;
- if (mouseX <= 0 || mouseX >= width || mouseY <= 0 || mouseY >= height) showTargetLoc = false;
- for (int i = 0; i < 64; i++) {
- int x = (7-i%8)*100;
- int y = (7-i/8)*100;
- if (somethingSelected && 1L<<i == mSelect) fill(128, 0, 0);
- else if (somethingSelected && showTargetLoc && mouseLoc == i) fill(128, 0, 0);
- else if ((i%2 == 0 && i/8%2 == 0) || (i%2 == 1 && i/8%2 == 1)) fill(204);
- else fill(51);
- rect(x, y, 100, 100);
- long bitMask = 1L<<i;
- for (int j = 0; j < 12; j++) {
- if ((bitMask&pieces[j]) != 0) {
- image(pieceImgs[j], x, y); // Show appropriate image for corresponding piece
- break;
- }
- }
- }
- }
- void initialize() {
- wTurn = true;
- pieces[0] = 0x000000000000ff00L; // White Pawns
- pieces[1] = 0x0000000000000081L; // White Rooks
- pieces[2] = 0x0000000000000042L; // White Knights
- pieces[3] = 0x0000000000000024L; // White Bishops
- pieces[4] = 0x0000000000000010L; // White Queens
- pieces[5] = 0x0000000000000008L; // White King
- pieces[6] = 0x00ff000000000000L; // Black Pawns
- pieces[7] = 0x8100000000000000L; // Black Rooks
- pieces[8] = 0x4200000000000000L; // Black Knights
- pieces[9] = 0x2400000000000000L; // Black Bishops
- pieces[10] = 0x1000000000000000L; // Black Queens
- pieces[11] = 0x0800000000000000L; // Black King
- }
1