3 Replies - 477 Views - Last Post: 27 March 2010 - 09:35 AM Rate Topic: -----

#1 wickedkc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-December 09

Suposable full code, can't make it work

Posted 27 March 2010 - 08:21 AM

I want to make a different version of the zombie simulation but I can't make the original code work that he has linked to the site, even though he says its the full code. I get 73 errors. If you can make it work for you without changing code let me know how you set it up.

http://kevan.org/proce55ing/zombies/

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 

public class zombies extends BApplet {
// Zombie Infection Simulation
// Kevan Davis, 16/8/03

int freeze;
int num=1000;
int speed=1;
int panic=5;
int human;
int panichuman;
int zombie;
int wall;
Being[] beings = new Being[4000];

void setup() 
{ 
  int x,y; 
  size(250,250); 
  human=color(200,0,200);
  panichuman=color(255,120,255);
  zombie=color(155,155,155);
  wall=color(50,50,60);

  noBackground(); 

  fill(color(0,0,0));
  rect(0,0,width,height); 
  fill(wall);
  stroke(color(0,0,0));
  for (int i=0; i<100; i++)
  {
    rect((int)random(width-1)+1, (int)random(height-1)+1,
         (int)random(60)+10, (int)random(60)+10); 
  }
  fill(color(0,0,0));
  for (int i=0; i<30; i++)
  {
    rect((int)random(width-1)+1, (int)random(height-1)+1,
         (int)random(20)+20,(int)random(20)+20); 

    x=(int)random(width-1)+1;
    y=(int)random(height-1)+1;
  }

  for(int i=0; i<4000; i++)
  { beings[i] = new Being(); beings[i].position(); }
  beings[1].infect();
  freeze=0;
} 
 
void loop() 
{ 
  if (freeze==0)
  {
    for(int i=0; i<num; i++) { 
      beings[i].move(); 
    } 
    if (speed==2) { delay(20); }
    else if (speed==3) { delay(50); }
    else if (speed==4) { delay(100); }
  }
} 

int look(int x, int y,int d,int dist)
{
  for(int i=0; i<dist; i++)
  {
    if (d==1) { y--; }
    if (d==2) { x++; }
    if (d==3) { y++; }
    if (d==4) { x--; }

    if (x>width-1 || x<1 || y>height-1 || y<1)
    { return 3; }
    else if (getPixel(x,y) == wall)
    { return 3; }
    else if (getPixel(x,y) == panichuman)
    { return 4; }
    else if (getPixel(x,y) == human)
    { return 2; }
    else if (getPixel(x,y) == zombie)
    { return 1; }
  }
  return 0;
}

void keyPressed() 
{ 
  if(key == ' ')
  {
    for(int i=0; i<num; i++)
    { beings[i].uninfect(); }
    beings[1].infect();
  } 
  if(key == 's' || key == 'S')
  {
    speed++;
    if (speed>4) { speed=1; }
  }
  if(key == 'p' || key == 'P')
  {
    panic = 5 - panic;
  }
  if(key == 'g' || key == 'G')
  {
    if (zombie==color(155,155,155))
    { zombie=color(0,255,0); }
    else
    { zombie=color(155,155,155); }
  }
  if(key == '+' && num < 4000) { num += 100; key='z'; }
  if(key == '-' && num > 100) { num -= 100; key='z'; }
  if(key == 'z' || key == 'Z')
  {
    freeze=1;
    setup(); 
  }
}

class Being
{ 
  int xpos, ypos, dir;
  int type, active;

  Being()
  {
    dir = (int)random(4)+1;
    type = 2;
    active = 0;
  }

  void position()
  {
    for (int ok=0; ok<100; ok++)
    {
      xpos = (int)random(width-1)+1; 
      ypos = (int)random(height-1)+1;
      if (getPixel(xpos,ypos)==color(0,0,0)) { ok = 100; }
    }
  }

  void infect(int x, int y)
  {
    if (x==xpos && y==ypos)
    { type = 1; }
  }

  void infect()
  { type = 1; setPixel(xpos, ypos, zombie); }

  void uninfect()
  { type = 2; }

  void move()
  {
    int r = (int)random(10);
    if ((type==2 && (active>0 || r>panic)) ||
        r==1)
    {
      setPixel(xpos, ypos, color(0,0,0));

      if (look(xpos,ypos,dir,1)==0)
      {
        if (dir==1) { ypos--; }
        if (dir==2) { xpos++; }
        if (dir==3) { ypos++; }
        if (dir==4) { xpos--; }
      }
      else
      {
        dir = (int)random(4)+1; 
      }

      if (type == 1)
      { setPixel(xpos, ypos, zombie); }
      else if (active > 0)
      { setPixel(xpos, ypos, panichuman); }
      else
      { setPixel(xpos, ypos, human); }
      if (active>0) {active--;}
    }

    int target = look(xpos,ypos,dir,10);

    if (type==1)
    {
      if (target==2 || target==4) { active = 10; }

      if (active==0 && target!=1) { dir = (int)random(4)+1; }
 
      int victim = look(xpos,ypos,dir,1);
      if (victim == 2 || victim==4)
      {
        int ix = xpos; int iy = ypos;
        if (dir==1) { iy--; }
        if (dir==2) { ix++; }
        if (dir==3) { iy++; }
        if (dir==4) { ix--; }
        for(int i=0; i<num; i++)
        { 
          beings[i].infect(ix,iy); 
        }
      }  
    }
    if (type==2)
    {
      if (target==1 || target==4)
      { active=10; }
      if (target==1)
      {       
        dir = dir + 2; if (dir>4) { dir = dir - 4; } 
      }
      if ((int)random(8)==1) { dir = (int)random(4)+1; }
    }
  }
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Suposable full code, can't make it work

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,471
  • Joined: 27-December 08

Re: Suposable full code, can't make it work

Posted 27 March 2010 - 08:51 AM

Without seeing the errors, I can't tell you for sure what's causing them. However, I do know that BApplet isn't a class that appears in the JDK. So that could be causing you a cannot find symbol error.

Quote

If you can make it work for you without changing code let me know how you set it up.

We will be happy to help you debug code YOU wrote, not fix other people's code for you.
Was This Post Helpful? 0
  • +
  • -

#3 wickedkc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-December 09

Re: Suposable full code, can't make it work

Posted 27 March 2010 - 09:10 AM

I can't write my code until I fix his and I actually found a variation of the code that is closer to what I want but it still uses that BApplet and yes I'm getting a whole bunch of cannot find symbol errors.


This is the variation:
import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*;
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 

public class zombies extends BApplet {
// Zombie Infection Simulation
// Kevan Davis, 16/8/03

int freeze;
int num=1000;
int numweps = 6;
int speed=1;
int panic=5;
int human;
int panichuman;
int zombie;
int wall;
int armedhuman, corpse, chainsaw;
Being[] beings = new Being[4000];
Weapon[] weapons = new Weapon[10];

void setup() 
{ 
  int x,y; 
  size(250,250); 
  human=color(200,0,200);
  panichuman=color(255,120,255);
	 zombie=color(0,255,0);
  wall=color(50,50,60);
	chainsaw = color(0,0,255);
	armedhuman = color(255,255,255);
	corpse = color(100, 100, 100);

  noBackground(); 

  fill(color(0,0,0));
  rect(0,0,width,height); 
  fill(wall);
  stroke(color(0,0,0));
  for (int i=0; i<100; i++)
  {
    rect((int)random(width-1)+1, (int)random(height-1)+1,
         (int)random(60)+10, (int)random(60)+10); 
  }
  fill(color(0,0,0));
  for (int i=0; i<30; i++)
  {
    rect((int)random(width-1)+1, (int)random(height-1)+1,
         (int)random(20)+20,(int)random(20)+20); 

    x=(int)random(width-1)+1;
    y=(int)random(height-1)+1;
  }

  for(int i=0; i<4000; i++)
  { beings[i] = new Being(); beings[i].position(); }
	for(int i = 0; i < numweps; i ++ ) {
		weapons[i] = new Weapon();
		weapons[i].position();
	}
  beings[1].infect();
  freeze=0;
} 
 
void loop() 
{ 
  if (freeze==0)
  {
    for(int i=0; i<num; i++) { 
      beings[i].move(); 
    } 
		for(int i = 0; i < numweps; i++) {
			weapons[i].move();
		}
    if (speed==2) { delay(20); }
    else if (speed==3) { delay(50); }
    else if (speed==4) { delay(100); }
  }
} 


int look(int x, int y,int d,int dist)
{
	float ratio = 0.25f;
	int vx=0, vy=0;
	boolean seenWall = false;
  for(int i=1; i<=dist; i++)
  {
		for(int j = (int)(-ratio * i); j <= (int)(ratio * i); j++) {
			switch(d) {
				case 1:
					vx = x + j;
					vy = y - i;
					break;
				case 2:
					vx = x + i;
					vy = y + j;
					break;
				case 3:
					vx = x + j;
					vy = y + i;
					break;
				case 4:
					vx = x - i;
					vy = y + j;
			}

			if (vx>width-1 || vx<1 || vy>height-1 || vy<1) {
				seenWall = true;
				continue;
			}
			int seeing = getPixel(vx,vy);
			if(seeing == chainsaw) 
				return 6;
			if(seeing == armedhuman)
				return 5;
			if(seeing == panichuman)
				return 4;
			if(seeing == human)
				return 2;
			if(seeing == zombie)
				return 1;
			if(seeing == wall)
				seenWall = true;
  	}
	}
  return seenWall ? 3 : 0;
}

void keyPressed() 
{ 
  if(key == ' ')
  {
    for(int i=0; i<num; i++)
    { beings[i].uninfect(); }
    beings[1].infect();
  } 
  if(key == 's' || key == 'S')
  {
    speed++;
    if (speed>4) { speed=1; }
  }
  if(key == 'p' || key == 'P')
  {
    panic = 5 - panic;
  }
  if(key == 'g' || key == 'G')
  {
    if (zombie==color(155,155,155))
    { zombie=color(0,255,0); }
    else
    { zombie=color(155,155,155); }
  }
  if(key == '+' && num < 4000) { num += 100; key='z'; }
  if(key == '-' && num > 100) { num -= 100; key='z'; }
  if(key == 'z' || key == 'Z')
  {
    freeze=1;
    setup(); 
  }
	if(key == 'b' || key == 'B') {
		for(int i = 0; i < num; i ++) {
			if(beings[i].type == 2) {
				beings[i].type = 5;
			}
		}
	}
}

class Being
{ 
  int xpos, ypos, dir;
	int avoid;
  int type, active;
	int panicimmunity;

  Being()
  {
    dir = (int)random(4)+1;
		avoid = -1;
    type = 2;
    active = 0;
  }

  void position()
  {
    for (int ok=0; ok<100; ok++)
    {
      xpos = (int)random(width-1)+1; 
      ypos = (int)random(height-1)+1;
      if (getPixel(xpos,ypos)==color(0,0,0)) break;
    }
  }

  boolean infect(int x, int y)
  {
    if (x==xpos && y==ypos)
    { 
			type = 1; 
			return true;
		}
		return false;
  }

	boolean attack(int x, int y, Being attacker)
	{
		if(type == 1) {
/*			// There's a 10% chance that the zombie will kill the attacker
			if(random(100) <= 10) {
				// BRAINS!
				attacker.infect();
			} else { */
				type = 7; // Corpse
				setPixel(xpos, ypos, corpse);
				return true;
//			}
		}
		return false;
	}

  boolean infect()
  { 
		type = 1; 
		setPixel(xpos, ypos, zombie); 
		avoid = -1;
		return true;
	}

  void uninfect()
  { type = 2; }

  void move()
  {
		// Corpses don't move
		if(type == 7) {
			setPixel(xpos, ypos, corpse);
			return;
		}
    int r = (int)random(10);
    if (((type==2 || type == 5) && (active>0 || r>panic)) ||
        r==1)
    {
      setPixel(xpos, ypos, color(0,0,0));

      if (look(xpos,ypos,dir,1)==0)
      {
        if (dir==1) { ypos--; }
        if (dir==2) { xpos++; }
        if (dir==3) { ypos++; }
        if (dir==4) { xpos--; }
      }
      else // We're blocked, so change direction
      {
				do {
	        dir = (int)random(4)+1; 
				} while(dir == avoid);
      }

      if (type == 1)
      { setPixel(xpos, ypos, zombie); }
      else if (type == 2 && active > 0)
      { setPixel(xpos, ypos, panichuman); }
			else if (type == 5) {
				setPixel(xpos, ypos, armedhuman); 
			}
      else
      { setPixel(xpos, ypos, human); }
      if (active>0) {
				active--;
				if(active == 0) {
					avoid = -1;
				}
			}
    }

    int target = look(xpos,ypos,dir,10);

		switch(type) {
			case 1: // Zombie
      	if (target==2 || target==4 || target == 5) { active = 10; }

	      if (active==0 && target!=1) { dir = (int)random(4)+1; }
 
  	    int victim = look(xpos,ypos,dir,1);
    	  if (victim == 2 || victim==4 || victim == 5)
      	{
        	int ix = xpos; int iy = ypos;
	        if (dir==1) { iy--; }
  	      if (dir==2) { ix++; }
    	    if (dir==3) { iy++; }
      	  if (dir==4) { ix--; }
        	for(int i=0; i<num; i++)
	        { 
  	        if(beings[i].infect(ix,iy)) break; 
    	    }
      	}
				break;
			case 2: // Unarmed Human
				// If we've spotted a zombie or a panicked human
  	    if (target==1 || target==4) {
    	  	active = 10;
				}
      	if (target==1) {       
	        dir = dir + 2; if (dir>4) { dir = dir - 4; } 
					avoid = dir;
					panicimmunity = 0;
    	  } else if (target == 4) {
					panicimmunity += 10;
					if(panicimmunity > 1000) {
						// We've gotten used to panicking people and have calmed down again
						active = 0;
					}
				} else {
					panicimmunity -= 1;
				}
      	if ((int)random(8)==1) { dir = (int)random(4)+1; }
				victim = look(xpos, ypos, dir, 1);
				if (victim == 6) { // Chainsaw!
					int ix = xpos, iy = ypos;
					switch(dir) {
						case 1: iy--; break;
						case 2: ix++; break;
						case 3: iy++; break;
						case 4: ix--; break;
					}
					for(int i = 0; i < numweps; i++) {
						if(weapons[i].pickup(ix, iy) == true) {
							type = 5;
							break;
						}
					}
				}
				break;
			case 5: // Armed human
				if (target == 1) {
					active = 10;
				}
				victim = look(xpos, ypos, dir, 1);
				if (victim == 1) { // Zombie sighted
					int ix = xpos, iy = ypos;
					switch(dir) {
						case 1: iy--; break;
						case 2: ix++; break;
						case 3: iy++; break;
						case 4: ix--; break;
					}
					for(int i = 0; i< num; i++) {
						if(beings[i].attack(ix, iy, this)) break;
					}
				}
				break;
		}
  }
}
	public class Weapon extends Being {
		boolean pickup(int x, int y) {
			if(x == xpos && y == ypos) {
				setPixel(xpos, ypos, color(0,0,0));
				position();
				move();
				return true;
			}
			return false;
		}

  Weapon()
  {
    dir = (int)random(4)+1;
		avoid = -1;
    type = 6;
    active = 0;
  }

		void move() {
			setPixel(xpos, ypos, chainsaw);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,471
  • Joined: 27-December 08

Re: Suposable full code, can't make it work

Posted 27 March 2010 - 09:35 AM

You still haven't shown us the errors. Do you have compiler errors to post? Also, the code could stand to be aesthetically cleaner. Could you improve upon the spacing and indentation conventions?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1