This site requires JavaScript, please enable it in your browser!
Greenfoot back
LittleMiiMe
LittleMiiMe wrote ...

2016/4/11

Trouble with Randomizer

LittleMiiMe LittleMiiMe

2016/4/11

#
Hi I need to program a game for my end work for school, I've the basic concept in mind of what I need to program and how I need to do it. I've tried a not of things but I can't find the problem. One of the things I tried is a little messy. First, I know my code that I've written can be better, more compact. And my var naming is different. If you know the solution to my problem can you please just write it in text with No example of code. Just point out the things I need to change. Thank you. So the problem: I need to spawn at a given interval 1 enemy or ally, they will spawn in an 75 pixel area (X randomly), and at a random Y. This part of the spawning works, the trouble I'm having now is instead of spawning one enemy or Ally, it spawns 30+ (way to much). This part of the program runs on a clock for each 100 acts Greenfoot does, 1 'second' will be counted. then the messy thing I've written does: for every second another var will go to 1 (I also could have used a boolean), at the end of the randomizer this will be set back to zero. this var is supposed to let the randomizer algorithm go only once a second. There is another var called tSC which counts how manytimes the randomizer algorithm has been run. this one also lets the program decide which tear (the first 3 if's in the randomizer) of difficulty to pick and which enemy or ally it will be. The 3 following if's are the intervals an enemy or ally can spawn and it decides randomly where to spawn that enemy or ally. The last IF is when the algorithm has ended it's lap (6 seconds) and the second counter will be set to 0, the tSC var will be added by one. thank you for pointing out the things i need to change to make the randomizer work.
public class MyWorld extends World
{
    private int j = 0;
    private int tS = 0;
    private int tSC = 0;

    private int aS1 = 0;
    private int aS2 = 0;
    private int aS3 = 0;
    
    private int tF1 = 0;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(750, 349, 1);
        
        addObject(new Cannon(), 40, 175);
    }
    
    public void act()
    {
        /*
         * Randomizer for Enemy and Ally spawn in world
         * Chooses randomly an enemy type or ally and a Y coordinate to spawn it
         * The X coordinate has a 75 pixel area to spawn and all enemies and allies move in the same direction
         * One algorithm lap takes 6 seconds: One spawn each 2 seconds (not in the beginning)
         * In the beginning easy enemies and allies will be spawned after … times the spawn algorithm has run more difficult enemies will spawn
         * After … times an enemy or ally has spawned more will spawn
         * This part only handles the spawning of enemies and allies
           */

          // Clock to convert acts/second into 1 second
          
          j = j + 1;

          if ( j == 100 )
          {
              tS = tS + 1;
              j = 0;
          }
          
          // spawn algorithm can only fire once a lap (one lap = tS++)
          if (tS == 1)
          {
               tF1 = 1;
          }
          
          if (tS == 2)
          {
              tF1 = 1;
          }
          
          if (tS == 3)
          {
              tF1 = 1;
          }
          
          if (tS == 4)
          {
              tF1 = 1;
          }
          
          if (tS == 5)
          {
              tF1 = 1;
          }
          
          if (tS == 6)
          {
              tF1 = 1;
          }

          // spawn algorithm (randomizer)
          
          if ( tF1 == 1 )
          {
          if (tSC >= 0 && tSC <= 8)
          {
              aS1 = Greenfoot.getRandomNumber(2);
              aS2 = Greenfoot.getRandomNumber(2);
          }

          if (tSC >= 8 && tSC <= 14)
          {
              aS1 = Greenfoot.getRandomNumber(2);
              aS2 = Greenfoot.getRandomNumber(3);
              aS3 = Greenfoot.getRandomNumber(3);
          }

          if (tSC >= 14)
          {
              aS1 = Greenfoot.getRandomNumber(4);
              aS2 = Greenfoot.getRandomNumber(4);
              aS3 = Greenfoot.getRandomNumber(4);
          }

          if (tS == 1)
          {
                if (aS1 == 0)
                {
                    addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }

                if (aS1 == 1)
                {
                    addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }

                if (aS1 == 2)
                {
                    addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }

                if (aS1 == 3)
                {
                    addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }
          }

          if (tS == 3)
          {
              if (aS2 == 0)
              {  
                  addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
              }

              if (aS2 == 1)
              {
                  addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
              }

              if (aS2 == 2)
              {
                  addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
              }

              if (aS2 == 3)
              {
                  addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
              }
          }

          if (tS == 5)
          {
              if (aS3 == 0)
              {
                  addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }

              if (aS3 == 1)
              {
                  addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
              }

              if (aS3 == 2)
              {
                  addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
              }

              if (aS3 == 3)
              {
                  addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
              }
          }

          if (tS == 6)
          {
              tS = 0;
              tSC = tSC + 1;
          }
          
          tF1 = 0;
          
          }
    }
}
danpost danpost

2016/4/12

#
I think your main issue is that the code from 46 on should be inside the preceding if block. Otherwise, while the value of 'j' goes from 100 through to 599, the value of 'ts' will be between one and six -- so for 600 consecutive act cycles, you will be spawning something as the code is 100 cycles at a time -- 3 times (when 'ts' is one, three or five). You only want to spawn when the value of 'ts' is incremented, which would be between seconds (inside the if block after incrementing 'ts' at line 41),
bamcgregor89 bamcgregor89

2016/4/12

#
I think you're adding objects each time Act is ran as opposed to time-gating it like you were trying. After you enter your first spawn cycle, the only variable you reset is tF1, right? tS is still retaining its value, so the next time Act is run...
if (tS == 1)
          {
               tF1 = 1;
          }
Boom. tF1 gets set back to 1, and the full spawn cycle is run again. This will continue to repeat each time Act is run. Some other tips, your variables should be more clearly named. tS, tF1, aS1, etc. I can't tell what any of that means. Variable names should give the reader an idea on what the variable is supposed to do. aS1 should be addSpawn1, or something like that. It does a lot to improve readability. Break statements would help improve readability as well. Instead of:
if (aS1 == 0)
                {
                    addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }
 
                if (aS1 == 1)
                {
                    addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }
 
                if (aS1 == 2)
                {
                    addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }
 
                if (aS1 == 3)
                {
                    addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
                }
You can use:
switch (aS1) {
	case 0:
		addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
		break;
	case 1: 
		addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
		break;
	case 2:
		addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
		break;
	case 3:
		addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
		break;
}
danpost danpost

2016/4/12

#
Even better, the entire class can be simplified to this:
import greenfoot.*;

public class MyWorld extends World
{
    private int j; // act counter for spawning cycle
    private int tSC; // total count of spawn cycles
    
    public MyWorld()
    {    
        super(750, 349, 1);
        addObject(new Cannon(), 40, 175);
    }
     
    public void act()
    {
        j = (j+1)%600; // 6 "second" cycle
        if (j == 0) tSC++; // counts cycles
        if (j%100 == 0 && (j/100)%2 == 1) // after every odd "second"
        {
            Actor[] actors = { new Ally(), new Enemy(), new EnemyF(), EnemyH() }; // possibilities
            int range = tSC > 13 ? 4 : (tSC < 8 || j/100 == 1 ? 2 : 3); // limits
            int index = Greenfoot.getRandomNumber(range); // choice
            addObject(actors[index], Greenfoot.getRandomNumber(76)+650, Greenfoot.getRandomNumber(300)+25);
        }
    }
}
LittleMiiMe LittleMiiMe

2016/4/13

#
Hello Thank you for replying on my problem I had with the randomizer. @Danpost, when I read your first comment I could find the problem immediately. The problem I've with the later posts is that in school I'm only learning the basics of Greenfoot, so I don't really understand the more advanced stuff yet (I'll be learning Java (and C#) fully in college next year). But thank you for helping. I solved the problem by first adding 1 'second' to the different spawning blocks, adding 3 booleans which at each cycle are true and in each spawning block, one turns to false. other changes I made: Enemy spawn are a 2/3 to 1/3 ratio in the beginning and after 14 cycles and more 5/6 to 1/6 ratio (will probably change). When the game advances the amount of acts that equal a second get smaller from 100 to 75. There's one more question I've: As you maybe see, I kind of made a shooter, but the problem I'm having now is: When an Ally for example isAtEdge() I want that ally to be removed from the world. Only that ally needs to disappear, not all the allies on screen, as I found out with the line getWorld().removeObjects(getWorld().getObjects(Ally.class)); I pretty certain that there's a fault in there but it removes all the objects from that class on screen instead of only the one at the edge. Thank you for helping me with this one again. (please make it a simple one that I can adapt if needed) LMM
danpost danpost

2016/4/13

#
You only need to understand the concept of the 'this' keyword. It refers to the object that is currently being worked on. It is always an object that is created from the class the word is used. So, you can have an actor remove itself from the world it is in with:
getWorld().removeObject(this);
// or
this.getWorld().removeObect(this);
I added the second line example to show that when a method is called and no object is explicitly given for the method to execute for or on (which is like the first line), then that method is, by default, called on the object the executing method is executing for or on. All methods are called on some object -- static methods on the class itself and non-static methods on objects created from that class.
You need to login to post a reply.