I’m making a match 3 game or at least I think it still called match 3. Anyways you click on a block and all the blocks touching that one of the same type vanish too. My problem is diagonal blocks are being chosen instead of the ones to the right, left, up & down.
in this pic here you can see it chose a box to the upper left which it shouldn’t be able to do.
main
int s = 50;
ArrayList<Block> block = new ArrayList<Block>();
ArrayList<Spawner> spawn = new ArrayList<Spawner>();
int minH = 50;
int w = 5;
int h = 5;
int blockTimer = 0;
int chainN = 0;
boolean chose = false;
void setup(){
size(600,600);
reset();
}
void reset(){
for (int i = 0; i < w; i++){
spawn.add(new Spawner(25 + (s * i), minH + ((h-1) * s)));
}
}
void draw(){
background(0);
textSize(20);
text("B: " + blockTimer,5,20);
text("ChainN: " + chainN,50,20);
if (blockTimer >= 0 && chose == true){
blockTimer -= 1;
}
/*if (blockTimer < 0 && chainN >= 1){
if (chainN >= 3){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
if (b.chain == true){
b.destroy = true;
}
b.coll = false;
chainN = 0;
chose = false;
blockTimer = 0;
}
}
if (chainN < 3){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
b.chain = false;
b.click = false;
chainN = 0;
chose = false;
blockTimer = 0;
}
}
}*/
for (Spawner s: spawn){
s.show();
s.create();
}
for (Block b : block){
b.move();
b.show();
b.checked();
}
for (int i = block.size()-1; i >= 0; i--){
Block b = block.get(i);
if (b.destroy == true){
block.remove(i);
}
}
}
void keyPressed(){
if (key == 'r'){
for (Block b: block){
b.destroy = true;
}
for (Spawner s: spawn){
s.coll = false;
}
}// r
if (key == ' '){
if (blockTimer < 0 && chainN >= 1){
if (chainN >= 3){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
if (b.chain == true){
b.destroy = true;
}
b.coll = false;
chainN = 0;
chose = false;
blockTimer = 0;
}
}
if (chainN < 3){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
b.chain = false;
b.click = false;
chainN = 0;
chose = false;
blockTimer = 0;
}
}
}
}
}
void mousePressed(){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
if (b.check == true){
b.click = true;
b.chain = true;
chainN = 1;
}
}
}
spawner
class Spawner{
PVector pos;
int timer = 0;
boolean coll = false;
Spawner(int x, int y){
pos = new PVector(x,y);
}
void show(){
if (coll){fill(0);}
if (coll == false){fill(125,125,125);}
rect(pos.x,pos.y,s,s);
}
void create(){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
if (b.pos.y == pos.y && b.pos.x == pos.x){
coll = true;
break;
} else{
coll = false;
}
}
if (timer >= 0){timer -= 1;}
if (timer < 0 && coll == false){
float r = random(2);
r = round(r);
block.add(new Block(pos,int(r)));
timer = 64;
}// timer
}// create
}
block
class Block{
PVector pos;
int moveSpd = 0;
boolean coll = false;
int type = 0;
boolean check = false;
boolean click = false;
boolean chain = false;
boolean destroy = false;
Block(PVector p, int type){
pos = p.copy();
this.type = type;
}
void checked(){
if (chainN < 1){
if (mouseX >= pos.x && mouseX <= pos.x + s &&
mouseY >= pos.y && mouseY <= pos.y + s){
check = true;
} else {
check = false;
}
} else {
check = false;
}
if (click == true){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
if (b.type == type){
if (b.pos.y == pos.y && b.pos.x == pos.x + s && b.chain == false ||
b.pos.y == pos.y - s && b.pos.x == pos.x && b.chain == false ||
b.pos.y == pos.y + s && b.pos.x == pos.x && b.chain == false ||
b.pos.y == pos.y + s && b.pos.x == pos.x && b.chain == false){
chain = true;
b.chain = true;
b.click = true;
click = false;
chainN += 1;
blockTimer = 32;
chose = true;
}
}
}
}
}
void show(){
if (type == 0){fill(255,0,0);}// red
if (type == 1){fill(0,255,0);}// green
if (type == 2){fill(0,0,255);}// blue
if (type == 3){fill(255,255,0);}// yellow
if (type == 4){fill(255,0,255);}// purple
if (type == 5){fill(0,255,255);}// cyan
if (type == 6){fill(255, 165, 0);}// orange
if (check == true){fill(255,255,255);}
rect(pos.x,pos.y,s,s);
if (chain == true){
fill(0);
text("C",pos.x + 5, pos.y + (s-5));
}
}
void move(){
if (pos.y == minH){
coll = true;
} else {
coll = false;
}
if (coll == false){
for (int i = 0; i < block.size(); i++){
Block b = block.get(i);
if (b.pos.y == pos.y - s && b.pos.x == pos.x){
coll = true;
break;
}
else {
coll = false;
}
}// block size
}
if (coll == true){moveSpd = 0;}
if (coll == false){moveSpd = -1;}
pos.y += moveSpd;
}// move
}