Hello,
I've been working on a game in which you must remove all the balls. However, I cannot think of a way to remove multiple balls (of the same color!) in a line that has branches going up and down.
TL;DR: How can I remove the balls that are connected to each other and have a chain of more than 3?
All the necessary code is included for the level, and an image of what I want to remove (a chain of 3 or more)
World class (Level4)
dropRow_ball

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
/**
* Write a description of class Level4 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Level4 extends World
{
private int totalBallRows = 3;
private int drawBallStop = 6;
private int yPos;
/**
* Constructor for objects of class Level4.
*
*/
public Level4()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(400, 600, 1);
createBackGround();
loadGameAssets();
generateBalls();
}
public void createBackGround() {
addObject(new dropRow_Title(), 348, 69);
addObject(new dropRow_playArea(), 123, 274);
}
public void loadGameAssets() {
addObject(new dropRow_scoreboard(), 346, 204);
addObject(new dropRow_scoreDisplay(0), 372, 251);
addObject(new dropRow_ballStop(), 98, 546);
}
public void generateBalls() { //523
for (int counter2 = 0; counter2 < totalBallRows; counter2=counter2+1) {
if(counter2 == 0) yPos = 0;
else if(counter2 == 1) yPos = 45;
else if(counter2 == 2) yPos = 90;
else yPos = 1;
addObject(new dropRow_ball(ballType()), 22, yPos);
addObject(new dropRow_ball(ballType()), 62, yPos);
addObject(new dropRow_ball(ballType()), 102, yPos);
addObject(new dropRow_ball(ballType()), 142, yPos);
addObject(new dropRow_ball(ballType()), 182, yPos);
addObject(new dropRow_ball(ballType()), 222, yPos);
}
}
public int ballType() {
int balltype;
int bt_temp = getRandomNumber(0, 4);
if(bt_temp == 1) { bt_temp = getRandomNumber(0, 4); }
balltype = bt_temp;
return balltype;
}
public int getRandomNumber(int min, int max)
{
Random random = new Random();
int n = random.nextInt(max-min) + min;
return n;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Dropto y=544
*/
public class dropRow_ball extends dropRow
{
public dropRow_ball(int type) {
if(type == 1) { //Red
setImage("dropfun_redball.png");
}
else if(type == 2) { //Yellow
setImage("dropfun_yellowball.png");
}
else if(type == 3) {//Green
setImage("dropfun_greenball.png");
}
}
public void act() {
if(isTouching(dropRow_ball.class)) {} //Do nothing
else if(isTouching(dropRow_ballStop.class)) {} //Do nothing
else setLocation(getX(), getY()+5);
}
public boolean checkMovePossible() {
return true;
}
}


