Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java HELP - Beginner Uni project

I am trying to make a grid where I have 3 different coloured squares the weeds being limited to 7 and the farmer being just 1 square. The rest of the grid is supposed to be green where there is not any of these other 2 blocks. Eventually I will have to use the collide(); in order for the farmer to get rid of the weeds. Any help is greatly appreciated!



[CODE lang="java" highlight="83-106"]
int x = 0;
int y = 0;
int farmer = 1;
int weeds = 7;
int cols = 8;
int rows = 10;
int grid = 80;

IntList seq = new IntList(grid);

boolean[][] myArr = new boolean[cols][rows];

boolean[][] myFarmer = new boolean[cols][rows];

boolean[][] myGrid = new boolean[cols][rows];

void setup() {
size(300, 400);
preset();
setgrids(grid);
setitems(weeds);
setfarmers(farmer);

}

void preset() {
for (int a =0; a<grid; a++) seq.append(a);
seq.shuffle();
}

void setgrid(int rec) {
for (int a = 0; a < cols; a++)
for (int b = 0; b < rows; b++)
if ( seq.get(b+a*rows) == rec ) myGrid[a] = true;
}
void setgrids(int item) {
for ( int f = 0; f < item; f++ ) setgrid(f);
}



void setitem(int rec) { //Start of item weeds
for (int d = 0; d < cols; d++)
for (int e = 0; e < rows; e++)
if ( seq.get(e+d*rows) == rec ) myArr[d][e] = true;
}
void setitems(int item) {
for ( int f = 0; f < item; f++ ) setitem(f);
} //End of weeds


void setfarmer(int rec) { //Start of farmer
for (int g = 0; g < cols; g++)
for (int h = 0; h < rows; h++)
if ( seq.get(h+g*rows) == rec ) myFarmer[g][h] = true;
}

void setfarmers(int item) {
for ( int i = 0; i < item; i++ ) setfarmer(i);
} //End of Farmer

void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
y -= 1;
} else if (keyCode == DOWN) {
y += 1;
} else if (keyCode == LEFT) {
x -= 1;
} else if (keyCode == RIGHT) {
x += 1;
}
}
}

void collide() {

//int i =id;


}

void draw() {

background(0);

for (int a = 0; a < cols; a++) {
for (int b = 0; b < rows; b++) {
if ( myGrid[a] ) fill(0, 0, 200);
rect(10+a*(36), 10+b*(36), 34, 34);
}
}


for (int c = 7; c > cols; c++) {
for (int d = 7; d < rows; d++) {
if ( myArr[c][d] ) fill(200, 0, 0); //my weeds
rect(10+c*(36), 10+d*(36), 34, 34);
}
}


for (int h = 0; h < cols; h++) {
for (int i = 0; i < rows; i++) {
if ( myFarmer[h] ) fill(0, 0, 200);
rect(10+h+x*(36), 10+i+y*(36), 34, 34);

}}}[/CODE]
 

New Threads

Buy us a coffee!

Back
Top Bottom