All elements change in Arraylist - Processing - arrays

My program is a zombie survival game, set in a 2d array of blocks.
I use an arraylist - my first attempt - to store the zombies, and each in tern "checks" if there is a block above, below and around it, to detect it's movement.
I'll post the relevant code here, and upload the sketch folder separately.
ArrayList zombies;
void setup() {
zombies = new ArrayList();
}
void draw() {
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.draw();
}
}
void keyPressed() {
if (key == 'z') {
zombies.add(new Zombie());
}
}
class Zombie
{
int posX = 20;
int posY = 10;
boolean fall;
boolean playerOnBlock;
Zombie() {
posX = 10;
posY = 590;
fall = false;
}
void draw() {
grid.blockCheck(posX, posY, 2);
fill(0, 255, 0);
rect(posX, posY, 10, 10);
}
void fall() {
posY += 5;
println("zombiefall"+posY);
}
void move(boolean left, boolean right, boolean above) {
if (left == true && player.playerX < posX) {
posX -= 1;
}
if (right == true && player.playerX > posX) {
posX += 1;
}
}
}
class Grid {
void blockCheck(int x, int y, int e) {
for (int i = 0; i < l; i++)
for (int j = 0; j < h; j++)
{
grid[i][j].aroundMe (x, y, i, j, e);
}
}
}
class Block {
void aroundMe(int _x, int _y, int _i, int _j, int entity) {
int pGX = _x/10;
int pGY = _y/10;
if (entity == 1) {
if (pGY+1 == _j && pGX == _i && state == 4) {
player.fall();
}
if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
leftOfMe = true;
}
else
{
leftOfMe = false;
}
if (pGX+1 == _i && _y == posY && state == 4) {
rightOfMe = true;
}
else
{
rightOfMe = false;
}
if (pGY-1 == _j && _x == posX && state ==4) {
aboveOfMe = true;
}
else
{
aboveOfMe = false;
}
player.controls(leftOfMe, rightOfMe, aboveOfMe);
}
if (entity == 2) {
if (pGY+1 == _j && pGX == _i && state == 4) {
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.fall();
}
}
if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
ZleftOfMe = true;
}
else
{
ZleftOfMe = false;
}
if (pGX+1 == _i && _y == posY && state == 4) {
ZrightOfMe = true;
}
else
{
ZrightOfMe = false;
}
if (pGY-1 == _j && _x == posX && state ==4) {
ZaboveOfMe = true;
}
else
{
ZaboveOfMe = false;
}
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.move(ZleftOfMe, ZrightOfMe, ZaboveOfMe);
}
}
Sketch is here: http://www.mediafire.com/?u5v3117baym846v
I believe the problem lies in specifying which element of an arraylist I am referring to, as I can observe the issues to be:
All "zombies" fall when one detects that it should fall.
Zombie's speed increases with each additional zombie added - somehow treating all the zombie elements as one zombie object?
This might be a similar issue:
All elements of An ArrayList change when a new one is added?
But I've fiddled with my project and I can't seem to get it working still.
Please don't hesitate to ask for more information on my project. I will be with my computer all evening so should be able to reply quickly. Thanks in advance.
Thanks for your help.
I'm using it like this:
ArrayList <Zombie> zombies = new ArrayList <Zombie>();
-------------------------------------------
void setup(){
zombies = new ArrayList();
-------------------------------------------
void draw(){
for (Zombie z:zombies) {
z.draw();
}
}
-------------------------------------------
void keyPressed() {
if (key == 'z') {
for (int i = 0; i< 1; i++) {
zombies.add(new Zombie(i));
}
}
-------------------------------------------
class Zombie
{
int posX = 20;
int posY = 10;
boolean fall;
boolean playerOnBlock;
int z;
Zombie(int _z) {
posX = 10;
posY = 590;
fall = false;
z = _z;
}
void draw() {
grid.blockCheck(posX, posY, 2);
fill(0, 255, 0);
rect(posX, posY, 10, 10);
}
void fall() {
posY += 5;
println("zombiefall"+posY);
}
void move(boolean left, boolean right, boolean above) {
if (left == true && player.playerX < posX) {
posX -= 1;
}
if (right == true && player.playerX > posX) {
posX += 1;
}
}
}

Here is the idea :)
//use generics to keep it simple. Only Zoombies in this list
ArrayList <Zoombie> samp = new ArrayList <Zoombie>();
void setup() {
//a regular for loop to use the i as id
for (int i = 0; i< 100; i++) {
samp.add(new Zoombie(i));
}
//run just once
noLoop();
}
void draw() {
//a enhanced for, no need for use get()
// z will represent each element in the collection
for (Zoombie z:samp) {
z.d();
}
println("done");
}
// a dummy class ;)
class Zoombie {
int id;
Zoombie (int _id)
{
id =_id;
}
void d()
{
println(id);
}
}

Related

How to get checkbox value inside datagridview?

I want to check where checkbox is checked or not inside "dataGridView1". Based on the value of checked box I want to calculate rtn & amt.
int rtn = 0;
int amt=0;
if (str == "GRI")
{
for (int n = 0; n <= dataGridView1.RowCount - 1; n++)
{
rtn = 0;
amt = 0;
bool chk =(bool)dataGridView1.Rows[n].Cells[4].Value;
if (chk)
{
dataGridView1.Rows[n].Cells[2].ReadOnly = false;
rtn +=Convert.ToInt32(dataGridView1.Rows[n].Cells[2].Value);
amt += Convert.ToInt32(dataGridView1.Rows[n].Cells[2].Value) * Convert.ToInt32(dataGridView1.Rows[n].Cells[3].Value);
}
else
{
}
}
}
totpcslbl.Text = rtn.ToString();
totamtrtnlbl.Text = amt.ToString();
try putting it in a Cellclick event?
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (chk.Value == chk.TrueValue)
{
//your code here
}
else
{
//do nothing
}
}
}

Get Key State in linux C

OK ive searched about for quite some time now but i simply cannot find a replacement for the GetKeyState() function in linux. All i need and want is to simply poll the arrow keys, and if they are pressed, execute something. My home PC is linux based and my students' PC is windows based, so when i worked in that, i wrote this code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <dos.h>
#include <windows.h>
int x; int y, redraw;
int i; int l, xm, ym, op, as, b, ab;
short int display[10][10];
void draw() {
if (redraw == 1) {
system("cls");
while (l < 10) {
while (i<10) {
if (display[i][l] == 0) { printf("="); }
if (display[i][l] == 1) { printf("X"); }
if (display[i][l] == 2) { printf("w"); }
if (display[i][l] == 3) { printf("0"); }
if (display[i][l] == 4) { printf("#"); }
if (display[i][l] == 5) { printf("M"); }
if (display[i][l] == 6) { printf("H"); }
if (display[i][l] == 7) { printf("8"); }
printf("|");
i++;
}
i = 0;
printf("\n");
printf("-+-+-+-+-+-+-+-+-+-+");
printf("\n");
l++;
}
l = 0;
redraw = 0;
}
}
void getkeys() {
while (b == 0) {
if (GetKeyState(VK_LEFT) & 0x8000)
{
xm = -1;
b = 1;
}
if (GetKeyState(VK_RIGHT) & 0x8000)
{
xm = 1;
b = 1;
}
if (GetKeyState(VK_UP) & 0x8000)
{
ym = -1;
b = 1;
}
if (GetKeyState(VK_DOWN) & 0x8000)
{
ym = 1;
b = 1;
}
if (GetKeyState(VK_BACK) & 0x8000)
{
op = -1;
b = 1;
}
if (GetKeyState(VK_RETURN) & 0x8000)
{
op = 1;
b = 1;
}
} b = 0; redraw = 1;
}
void cursor() {
display[x][y] = as;
x = x + xm;
xm = 0;
y = y + ym;
ym = 0;
if (x >9) { x = 0; }
if (y >9) { y = 0; }
if (x <0) { x = 9; }
if (y <0) { y = 9; }
ab = display[x][y];
as = ab;
if (as == 0) {
display[x][y] = 4;
}
if (as == 1) {
display[x][y] = 5;
}
if (as == 2) {
display[x][y] = 6;
}
if (as == 3) {
display[x][y] = 7;
}
Sleep(100);
}
void main()
{
while (i < 10) {
while (l<10) { display[l][i] = rand() % 4; l++; } l = 0; i++;
}
redraw = 1;
while (1) {
draw();
getkeys();
b = 0;
cursor();
}
}
now this basically prints an array and a cursor on it, but it does use the GetKeyState() function and i just can not find an alternative to it on linux. So is there any simple alternative to the mentioned function and is it possible to make the source code multiplatform somehow? Thanks in advance.

Converting function to use iteration

I have written a recursive function to access array elements. Every 2nd, 3rd, 4th, and so on to 150. I want to do the same thing but with iteration. I have coded something below, the first example it runs but i'm not sure its correct and works the same as the first function. I know i'm using two for loops to do this i may have placed the index var in wrong spot.
void openarray(skip)
{
int i;
for(i = skip; i < 150; i+=skip + 1)
{
if(arrayB[i] == open)
{
arrayB[i] = close;
}
else
{
arrayB[i] = open;
}
}
if(skip < 150)
{
openarray(skip + 1);
}
}
my code to turn into iteration i think i may have confused the index for step or vice versa.
int i, j;
for(i = 0; i < 150; i++)
{
for (j = 0; j < 150; j+=i + 1)
if(arrayB[i] == open)
{
arrayB[i] = close;
}
else
{
arrayB[i] = open;
}
}
Step 1: Let's put the recursive call at the very end.
void openarray(skip)
{
int i;
for(i = skip; i < 150; i+=skip + 1)
{
if(arrayB[i] == open)
{
arrayB[i] = close;
}
else
{
arrayB[i] = open;
}
}
if(skip >= 150)
return;
openarray(skip + 1);
}
Step 2: Add the loop.
void openarray(skip)
{
while (1) {
int i;
for(i = skip; i < 150; i+=skip + 1)
{
if(arrayB[i] == open)
{
arrayB[i] = close;
}
else
{
arrayB[i] = open;
}
}
if(skip >= 150)
return;
skip = skip + 1;
}
}
Step 3: Clean up
void openarray(skip)
{
for (; skip < 150; ++skip)
{
int i;
for(i = skip; i < 150; i+=skip + 1)
{
if(arrayB[i] == open)
{
arrayB[i] = close;
}
else
{
arrayB[i] = open;
}
}
}
}
We can compare this against your code to see what errors you made:
You initialized the outer loop counter to zero.
You used the outer loop counter as the array index instead of the inner loop counter.

Pointers Binary Tree Maze Solver in C

I need to create a Robot Simulator programmed in C. The Robot has to find the Exit of a 2d labirinth using a Recursive Backtracker algorithm, i understood how does this algorithm work but i don't know how to implement it. I Think i can use a Binary Tree using Pointers but i don't know how to do this, can you try to explain it to me?
This is the program that i've created, now the Robot is entering a loop because of the method that changes direction
#ifdef __unix__
#include <unistd.h>
#elif defined _WIN32
#include <windows.h>
#define sleep(x) Sleep(1000 * x)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void goUp();
void goDown();
void goLeft();
void goRight();
typedef struct robot {
int direction;
bool is_moving;
}robot;
typedef struct room {
robot robot;
bool is_robot;
int obstacle;
}room;
room Room[20][20];
int r = 12;
int c = 10;
void generation(room matrix[20][20])
{
srand(time(NULL));
int x,i,j;
x=0;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
matrix[i][j].is_robot=false;
x=rand()%100+1;
if(x==1||x==50||x==100)
{
matrix[i][j].obstacle=1;
}
else
{
matrix[i][j].obstacle=0;
}
}
}
}
void print_matrix(room matrix[20][20])
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(matrix[i][j].obstacle==0)
{
if(matrix[i][j].is_robot==true)
{
printf("I");
}
else
{
printf(" ");
}
}
else
{
if(matrix[i][j].is_robot==true)
{
printf("I");
}
else
{
printf("o");
}
}
}
printf("\n");
}
}
bool changeDirection(room Room[20][20],int i,int j)
{
if(Room[i][j].robot.direction == 1)
{
if(Room[i-1][j].obstacle == 1 || i-1 == 0)
{
if(Room[i+1][j].obstacle == 1 || i+1 == 19)
{
Room[i][j].robot.direction = 2;
return true;
}
else
{
Room[i][j].robot.direction = 4;
return true;
}
}
else
{
Room[i][j].robot.direction = 3;
return true;
}
}
if(Room[i][j].robot.direction == 2)
{
if(Room[i-1][j].obstacle == 1 || i-1 == 0)
{
if(Room[i+1][j].obstacle == 1 || i+1 == 19)
{
Room[i][j].robot.direction = 1;
return true;
}
else
{
Room[i][j].robot.direction = 4;
return true;
}
}
else
{
Room[i][j].robot.direction = 3;
return true;
}
}
if(Room[i][j].robot.direction == 3)
{
if(Room[i][j+1].obstacle == 1 || j+1 == 19)
{
if(Room[i][j-1].obstacle == 1 || j-1 == 0)
{
Room[i][j].robot.direction = 4;
return true;
}
else
{
Room[i][j].robot.direction = 2;
return true;
}
}
else
{
Room[i][j].robot.direction = 1;
return true;
}
}
if(Room[i][j].robot.direction == 4)
{
if(Room[i][j+1].obstacle == 1 || j+1 == 19)
{
if(Room[i][j-1].obstacle == 1 || j-1 == 0)
{
Room[i][j].robot.direction = 3;
return true;
}
else
{
Room[i][j].robot.direction = 2;
return true;
}
}
else
{
Room[i][j].robot.direction = 1;
return true;
}
}
}
void goRight()
{
c=c+1;
Room[r][c].robot.direction=1;
Room[r][c].is_robot=true;
Room[r][c-1].is_robot=false;
}
void goLeft()
{
c=c-1;
Room[r][c].robot.direction=2;
Room[r][c].is_robot=true;
Room[r][c+1].is_robot=false;
}
void goUp()
{
r=r-1;
Room[r][c].robot.direction=3;
Room[r][c].is_robot=true;
Room[r+1][c].is_robot=false;
}
void goDown()
{
r=r+1;
Room[r][c].robot.direction=4;
Room[r][c].is_robot=true;
Room[r-1][c].is_robot=false;
}
int main()
{
generation(Room);
Room[r][c].robot.direction = 1;
Room[r][c].robot.is_moving = true;
Room[r][c].is_robot = true;
do
{
Room[r][c].robot.is_moving = true;
if (Room[r][c].robot.direction == 1 && Room[r][c].robot.is_moving == true) // destra
{
if(Room[r][c +1].obstacle == 1 || c+1 == 19)
{
changeDirection(Room,r,c);
}
else
{
goRight();
}
}
if (Room[r][c].robot.direction == 2 && Room[r][c].robot.is_moving == true) // sinistra
{
if(Room[r][c -1].obstacle == 1 || c-1 == 0)
{
changeDirection(Room,r,c);
}
else
{
goLeft();
}
}
if (Room[r][c].robot.direction == 3 && Room[r][c].robot.is_moving == true) // su
{
if(Room[r-1][c].obstacle == 1 || r-1 == 0)
{
changeDirection(Room,r,c);
}
else
{
goUp();
}
}
if (Room[r][c].robot.direction == 4 && Room[r][c].robot.is_moving == true) // giu
{
if(Room[r+1][c].obstacle == 1 || r+1 == 19)
{
changeDirection(Room,r,c);
}
else
{
goDown();
}
}
print_matrix(Room);
sleep(0.1);
system("cls");
}
while(1);
print_matrix(Room);
}
I'm having a hard time understanding how a binary tree would be useful in finding a path in a labyrinth (maybe it's used to represent the labyrinth?) but maybe I'm blind. I would simply make a 2d int array and let 0 mean the position is blocked (there's a wall there or something) and 1 mean it's open (you can move there). The brute force backtrack procedure, going off orthogonal movement (left, right, up, down) would be:
f(x,y){
// you found the place your want to go to
if (x,y) is (destinationX,destinationY)
return true
block the position (x,y) // i.e. mark current position as visited
if there is an open spot at (x,y-1) AND f(x,y-1)
return true
if there is an open spot at (x,y+1) AND f(x,y+1)
return true
if there is an open spot at (x-1,y) AND f(x-1,y)
return true
if there is an open spot at (x+1,y) AND f(x+1,y)
return true
return false
}
Suppose you had the labyrinth looking like:
"+" is where you start ([1][1])
"-" is your destination ([3][1])
"#" is a blocked region
===========
|#|#|#|#|#|
|#|+| |#|#|
|#|#| |#|#|
|#|-| | |#|
|#|#|#|#|#|
===========
Using the above idea I have:
#include <stdio.h>
#define width 5
#define height 5
// print maze
void print(char arr[][width]){
for (int i = 0; i < 2*width+1; i++) printf("=");
printf("\n");
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("|%c",arr[i][j]);
}
printf("|\n");
}
for (int i = 0; i < 2*width+1; i++) printf("=");
}
// starting from (x,y) to (destX,destY)
int path(int arr[][width],int x,int y,int destX,int destY,char toDest[][width]){
if (x==destX && y==destY) {
toDest[y][x] = '*';
print(toDest);
return 1;
}
// mark current position as visited
arr[y][x] = 0;
toDest[y][x] = '*';
// left
if (arr[y][x-1] && path(arr,x-1,y,destX,destY,toDest))
return 1;
// right
if (arr[y][x+1] && path(arr,x+1,y,destX,destY,toDest))
return 1;
// up
if (arr[y-1][x] && path(arr,x,y-1,destX,destY,toDest))
return 1;
// down
if (arr[y+1][x] && path(arr,x,y+1,destX,destY,toDest))
return 1;
return 0;
}
int main () {
// use this to store path
// and then print it out if found
char toDest[height][width] = {
{'#','#','#','#','#'},
{'#',' ',' ','#','#'},
{'#','#',' ','#','#'},
{'#',' ',' ',' ','#'},
{'#','#','#','#','#'}
};
// 0 -> position is blocked
// 1 -> position is open
int maze[height][width] = {
{0,0,0,0,0},
{0,1,1,0,0},
{0,0,1,0,0},
{0,1,1,1,0},
{0,0,0,0,0}
};
path(maze,1,1,1,3,toDest);
}
Output:
===========
|#|#|#|#|#|
|#|*|*|#|#|
|#|#|*|#|#|
|#|*|*| |#|
|#|#|#|#|#|
===========
In output the path is designated by the *s

Boolean fuction to all the elements in an array processing 3.0

I'm trying to make a program which shows some circles, and when the user puts all the circles in one place, the background changes. The problem I have is that when one circle is moved to that place the background changes, but I need that to happened once all the elements of the array had changed position..
DragMe[] drags = new DragMe[15];
PGraphics topLayer;
PGraphics toperLayer;
color backgr;
void setup() {
size(500, 500);
for (int i = 0; i < drags.length; i++) {
drags[i] = new DragMe();
}
topLayer = createGraphics(width, height, g.getClass().getName());
}
void draw() {
background(backgr);
{
topLayer.beginDraw();
topLayer.noFill();
topLayer.stroke(0 );
if (mousePressed ==true) {
topLayer.line( pmouseX, pmouseY, mouseX, mouseY );
topLayer.endDraw();
}
image( topLayer, 0, 0 );
}
for (int i = 0; i < drags.length; i++) {
drags[i].display();
drags[i].update();
}
for (int i = 0; i < drags.length; i++) {
drags[i].fondo();
}
{
ellipse(width/2.5, height/2.5, 110, 110);
fill(255);
}
}
void mousePressed() {
for (int i = 0; i < drags.length; i++) {
if (!drags[i].isOver())
drags[i].dontMove = true;
drags[i].offset_x = mouseX - drags[i].pos_x;
drags[i].offset_y = mouseY - drags[i].pos_y;
}
}
void mouseReleased() {
for (int i = 0; i < drags.length; i++) {
drags[i].locked = false;
drags[i].dontMove = false;
}
}
class DragMe {
float pos_x, pos_y, SIZE = 25;
float prev_x, prev_y;
boolean locked;
boolean dontMove;
boolean all;
color c = color (255);
float offset_x, offset_y;
DragMe() {
pos_x = random(width-SIZE);
pos_y = random(height-SIZE);
}
void update() {
if (isOver() && !locked && !dontMove || locked && !dontMove )
c = color (255);
else
c = color (255);
if (isClicked()) {
locked = true;
}
if (isIn()) {
locked = false;
all = true;
}
if (locked && !dontMove) {
pos_x = mouseX - offset_x;
pos_y = mouseY - offset_y;
}
}
void display() {
fill(c);
ellipse(pos_x, pos_y, SIZE, SIZE);
ellipseMode(CORNER);
noStroke();
}
boolean isOver() {
float right_x = pos_x + SIZE;
float bottom_y = pos_y + SIZE;
return mouseX >= pos_x && mouseX <= right_x &&
mouseY >= pos_y && mouseY <= bottom_y;
}
boolean isClicked() {
return isOver() && mousePressed && !dontMove;
}
boolean isIn() {
float right_x = pos_x + SIZE;
float bottom_y = pos_y + SIZE;
return right_x >= width/2.5 + SIZE && right_x <= width/2.5 + 100 &&
bottom_y >= height/2.5 + SIZE && bottom_y <= height/2.5 + 100;
}
void fondo() {
if (all == true)
backgr= 255;
}
}
You have 15 instances of DragMe.
Each of those instances contains a variable named all.
When one instance's all is true, you set the background to white.
Specifically, this function:
void fondo() {
if (all == true)
backgr= 255;
}
Which you call from draw():
for (int i = 0; i < drags.length; i++) {
drags[i].fondo();
}
Instead, you need to check every instance's all variable, and only change the background if they are all true (in other words, if none of them are false):
boolean allIn = true;
for (int i = 0; i < drags.length; i++) {
if (!drags[i].all) {
allIn = false;
}
}
if (allIn) {
backgr= 255;
}
For future questions, please try to make sure your code is formatted. It might also be a good idea to use better variable names- naming a variable all is a bit hard to talk about. Maybe something like isInsideWhiteCircle would be better.

Resources