We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I made a code for this chess game that allows the program to detect certain coloured pieces. but for some reason, it doesn't work for white. it works for black, but the strange thing is, if I switch the sides around, it becomes black that doesn't work. somebody please help me.
PImage board;
PImage[][] locations = new PImage [8][8];
PImage wpawn, bpawn, wknight, bknight, wbishop, bbishop, wrook, brook, wqueen, bqueen, wking, bking;
boolean white = true, turn = white, black = false, click = false;
int clickx, clicky, clickx1, clicky1;
player john = new player();
player ai = new player();
void setup() {
size(800, 800);
board = loadImage("board.png");
board.resize(width, height);
imageMode(CENTER);
ai.spawn(0, true);//0 is black, 1 is white
john.spawn(1, false);
}
void draw() {//------------------------------------------------------------------
image(board, width/2, height/2);
john.move();
ai.move();
print(white(1, 1));
}
void mousePressed() {
if (click) {
clickx1 = round(mouseX / (width/8)-0.5);
clicky1 = round(mouseY / (height/8)-0.5);
if (locations[clicky][clickx] == bqueen) {
if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
locations[clicky][clickx] = null;
locations[clicky1][clickx1] = null;
locations[clicky1][clickx1] = bqueen;
}
} else if (locations[clicky][clickx] == bbishop) {
if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
locations[clicky][clickx] = null;
locations[clicky1][clickx1] = null;
locations[clicky1][clickx1] = bbishop;
}
} else if (locations[clicky][clickx] == bknight) {
if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
locations[clicky][clickx] = null;
locations[clicky1][clickx1] = null;
locations[clicky1][clickx1] = bknight;
}
} else if (locations[clicky][clickx] == brook) {
if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
locations[clicky][clickx] = null;
locations[clicky1][clickx1] = null;
locations[clicky1][clickx1] = brook;
}
} else if (locations[clicky][clickx] == bpawn) {
if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
locations[clicky][clickx] = null;
locations[clicky1][clickx1] = null;
locations[clicky1][clickx1] = bpawn;
}
} else if (locations[clicky][clickx] == bking) {
if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
locations[clicky][clickx] = null;
locations[clicky1][clickx1] = null;
locations[clicky1][clickx1] = bking;
}
}
click = false;
} else {
clickx = round(mouseX / (width/8)-0.5);
clicky = round(mouseY / (height/8)-0.5);
click = true;
}
}
boolean black(int x, int y) {
if (locations[y][x] == bpawn) {
return true;
} else if (locations[y][x] == bbishop) {
return true;
} else if (locations[y][x] == bknight) {
return true;
} else if (locations[y][x] == brook) {
return true;
} else if (locations[y][x] == bqueen) {
return true;
} else if (locations[y][x] == bking) {
return true;
}else return false;
}
boolean white(int x, int y) {
if (locations[y][x] == wpawn) {
return true;
} else if (locations[y][x] == wbishop) {
return true;
} else if (locations[y][x] == wknight) {
return true;
} else if (locations[y][x] == wrook) {
return true;
} else if (locations[y][x] == wqueen) {
return true;
} else if (locations[y][x] == wking) {
return true;
}else return false;
}
boolean validmove(int x, int x1, int y, int y1, PImage[][] locations) {
if (locations[y][x] == wpawn) {
if (y!=0) {
if (x-x1==1&&y-y1 == 1&&(black(x-1,y-1)||black(x+1,y-1))) {
return true;
} else if (x-x1==-1&&y-y1 == 1&&(black(x-1,y-1)||black(x+1,y-1))) {
return true;
}
if (locations[y-1][x] == null) {//no block
if (x==x1) {
if (y == 1||y == 6) {//on rank 2 or 7
if (y-y1 <=2) {// can move up to 2 squares
return true;
} else return false;//moving over 2 squares
} else {
if (y-y1 == 1) {
return true;
} else return false;
}//else
} else return false;
} else return false;
} else return false;
} else if (locations[y][x] == wbishop) {
// if (x=x+1);
// return false;
// } else {
return false;
}else return false;
}
void img() {
wpawn = loadImage("whitepawn.png");
bpawn = loadImage("blackpawn.png");
bqueen = loadImage("blackqueen.png");
wknight = loadImage("whiteknight.png");
wbishop = loadImage("whitebishop.png");
bknight = loadImage("blackknight.png");
wrook = loadImage("whiterook.png");
brook = loadImage("blackrook.png");
wking = loadImage("whiteking.png");
bking = loadImage("blackking.png");
bbishop = loadImage("blackbishop.png");
wqueen = loadImage("whitequeen.png");
bpawn.resize(100, 100);
wknight.resize(100, 100);
bknight.resize(100, 100);
wbishop.resize(100, 100);
bbishop.resize(100, 100);
wrook.resize(100, 100);
brook.resize(100, 100);
wking.resize(100, 100);
bking.resize(100, 100);
wqueen.resize(100, 100);
bqueen.resize(100, 100);
wpawn.resize(100, 100);
}
class player {
boolean ai;
int colour, pawns, knights, bishops, rooks, queens;
void spawn(int team, boolean computer) {
colour = team;
ai = computer;
img();
if (colour==0) {
locations[0][0] = wrook;
locations[0][1] = wknight;
locations[0][2] = wbishop;
locations[0][3] = wqueen;
locations[0][4] = wking;
locations[0][5] = wbishop;
locations[0][6] = wknight;
locations[0][7] = wrook;
locations[1][0] = wpawn;
locations[1][1] = wpawn;
locations[1][2] = wpawn;
locations[1][3] = wpawn;
locations[1][4] = wpawn;
locations[1][5] = wpawn;
locations[1][6] = wpawn;
locations[1][7] = wpawn;
} else {
locations[7][0] = brook;
locations[7][1] = bknight;
locations[7][2] = bbishop;
locations[7][3] = bqueen;
locations[7][4] = bking;
locations[7][5] = bbishop;
locations[7][6] = bknight;
locations[7][7] = brook;
locations[6][0] = bpawn;
locations[6][1] = bpawn;
locations[6][2] = bpawn;
locations[6][3] = bpawn;
locations[6][4] = bpawn;
locations[6][5] = bpawn;
locations[6][6] = bpawn;
locations[6][7] = bpawn;
}
}
void display() {
board();
}
void board() {
for (int i = 0; i<8; i++)
for (int j = 0; j<8; j++) {
if (locations[j][i] != null) image(locations[j][i], i*width/8+50, j*height/8+50);//piece
}
}
void move() {
display();
if (ai) {
}
}
}
Answers
@Deiplz -- I'm not sure I understand your question, but I'll take a stab at it.
You have a mousePressed function that checks for only 6 PImages -- specifically, the black ones. Your checks look like this:
"but for some reason, it doesn't work for white" -- because you aren't testing for white pieces when you click the mouse?
"if I switch the sides around, it becomes black that doesn't work" -- I don't know what you are switching -- the graphics assets, or something else -- but if you are only click-testing for 6 piece types, you are only going to be able to recognize 6 piece types.
you load all 12 images for both players. that's too many.
and we don't have the images so we can't run this ourselves.
set it to null and then immediately set it to something else? why?
For jeremydouglass's question, the switch means that If I switch the locations of black with white, like for example right now [0][0] is occupied by a white rook, what I mean is that it switches with the black, and then [0][0] would be occupied by a black rook and the white rook would be wherever. Does that answer your question?
and also, the detect is in draw, the bit of code that says: print(white(1, 1)); the clicks are only for moving the pieces, I have tried swaping the colours, but it's still the same problem. it is either a problem with the boolean, or the PImage, but I think it is the PImage.
koogs, that is the take function. I make the spot null to destroy any piece that was there, and then replaces it with bqueen. but maybe I don't need the null function. and for the image problem, I have put the zip file with the data here: https://drive.google.com/open?id=1ehgxRtZ1DuyKbEE4_2xXg2miDbmD91z5
okay, but what about the original problem?
Not sure. a bunch of things about the code seem odd -- for one example:
white
and a functionwhite
- same for black.Also:
Maybe a bit of cleanup before taking another shot at debugging?
Your "player" class always puts the pieces of each color on the same sides of the board -- so if you are expecting a swap to swap sides, it doesn't do that. You can test for white(1,1) or black(6,6), but you can't test for black(1,1) or white(6,6). Again, I don't really know exactly what doing when you are switching, as I believe (?) it isn't shown in code.
I have quite a few things I don't understand, but I think you don't get what I mean. It will take quite a long time for you to get it, but One thing I can tell you is that what you suggested isn't the problem. I have updated my code quite a bit, here is it now:
sorry, but for some reason it works now, thank you
;)