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

2017/5/10

Help reducing redundant map creation methods...

1
2
alittle_bit alittle_bit

2017/5/10

#
Good day, What I am aiming for in my 'Game' world class is a way of retrieving a string in some kind of variable so that the one map creating method (not world constructor) will be able to receive many different arrays depending on how the map creator is called (in its condition). Currently, I have each map being created in a separate identical method (if you were to ignore the difference of array name) & this makes creating new maps more tedious than I believe it needs to be... Any help would be appreciated, preferably one involving a way of storing a string in some kind of variable so that the method condition is easy to solve, but it most likely will involve some kind of use of array. Don't worry about suggesting the array names be changed, I'm planning to do that anyway! Below is the group of methods/arrays involved in each class (most of it is either map arrays or methods to utilise the map creation methods) :
/**
     * This World below is very important towards the placement of each level.
     * Each value represents one Wall tile. 
     
     * e.g. If you wanted to clip two walls
     * together along the x-axis, that would require the wall's x-coords to be
     * different to the ones listed here.
     
     * There has to be 18 values in the World below, so long as the values are
     * between 0 - 90, it should be accepted.
     */
    private int[] xCoordinate=
    {
        2,7,12,17,22,27,32,37,42,47,52,57,62,67,72,77,82,87
    };
    
    /**
     * Worlds containing 'WWorld' are world Worlds (hopefully there is more than one)
     */
    private int[] createWorld=
    {
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,
        1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,9,
        0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,9,
        0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99    
    };
    
    private int[] messaboutWorld=
    {
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,
        0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,9,
        0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,9,
        0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,9,
        0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,9,
        0,0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,0,9,
        0,0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,0,9,
        0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,9,
        0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,9,
        0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,9,
        0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,0,7,0,9,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99
    };
    
    private int[] blankWorld=
    {
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,
        1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99    
    };
    /**
     * Key is:
     * 0 = Nothing, 1 = Wall, 2 = Enemy, 3 = Spawner, 7 = controls, 8 = Level goal,
     * 9 should not be changed, manages new row
     * 
     * (Level_goal MUST be in a different location to every other world)
     */
    private int[] mazeWorld1=
    {
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,
        1,0,0,0,0,0,0,0,0,0,0,1,0,1,2,0,0,1,9,
        1,0,1,1,1,1,1,1,0,1,0,1,0,0,0,0,0,1,9,
        1,0,1,0,0,3,0,0,0,1,0,1,0,1,2,0,0,1,9,
        1,0,1,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,9,
        1,0,1,0,1,1,1,1,0,1,0,1,0,1,1,0,1,1,9,
        1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,9,
        1,0,1,1,0,1,1,1,0,1,1,1,0,1,0,0,0,1,9,
        1,0,1,1,0,1,0,0,0,1,1,1,0,1,0,0,0,1,9,
        1,0,1,1,0,1,0,1,0,1,1,1,0,1,0,0,0,1,9,
        1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,9,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,99    
    };

    /**
     * If the condition is:
     * 1 = smiley face world
     * 2 = The world with lots of tools
     * 3 = A blank world!
     */
    public void setWorld(int worldNumber)
    {
        refreshWorld();
        switch(worldNumber)
        {
            case 1:
            createWorld();
            break;
            
            case 2:
            createMessabout();
            break;
            
            case 3:
            createBlank();
            break;
            
            case 4:
            createMaze1();
            break;
        }
        
    }
    public void refreshWorld()
    {
        removeObjects(getObjects(Enemy.class));
        removeObjects(getObjects(Wall.class));
        removeObjects(getObjects(Spawner.class));
        //removeObjects(getObjects(Level_goal.class));
    }
    /**
     * This method utilises the 'blankWorld' World. The integers used represent the following:
     * "1=Wall class", "2=Enemy class", "8 = Spawner class", "9 = begin new row"
     */
    private void createBlank()
    {
        int counter = 0;
        int ypos = 0;//xpos stands for X-Position
        int limit = 0;
        while( blankWorld[counter] != 99)
        {
            //the follwing are te 3 checks for what a tile might be
            if(blankWorld[counter] == 1)
            {
                addObject(new Wall(), xCoordinate[counter-limit] ,ypos+2);
            }
            if(blankWorld[counter] == 2)
            {
                addObject(new Enemy(movedIncrementor(false)), xCoordinate[counter-limit] ,ypos+2);
            }
            if(blankWorld[counter] == 3)
            {
                addObject(new Spawner(1), xCoordinate[counter-limit] ,ypos+2);
            }
            if(blankWorld[counter] == 7)
            {
                addObject(new Controls(), xCoordinate[counter-limit] ,ypos+2);
            }
            if(blankWorld[counter] == 8)
            {
                addObject(new Level_goal(), xCoordinate[counter-limit] ,ypos+2);
            }
            
            if(blankWorld[counter] == 9) //New row
            {
                ypos +=5;
                limit = counter+1;
            }
            counter += 1;
        }
    }
    private void createWorld()
    {
        int counter = 0;
        int ypos = 0;//xpos stands for X-Position
        int limit = 0;
        /**
        * This line of code and many like it can be explained like this:
        * The Method starts with a counter of 0 and sets the ypos (or y-axis row number)
        * to 0 in order to start from ther beginning.
        * The counter is NOT used to end the execution of this method's 'while' section.
        */
        while( createWorld[counter] != 99)
        {
            if(createWorld[counter] == 1)
            {
                addObject(new Wall(), xCoordinate[counter-limit] ,ypos+2);
            }
            if(createWorld[counter] == 2)
            {
                addObject(new Enemy(movedIncrementor(false)), xCoordinate[counter-limit] ,ypos+2);
            }
            if(createWorld[counter] == 3)
            {
                addObject(new Spawner(1), xCoordinate[counter-limit] ,ypos+2);
            }
            if(createWorld[counter] == 7)
            {
                addObject(new Controls(), xCoordinate[counter-limit] ,ypos+2);
            }
            
            
            if(createWorld[counter] == 9)
            {
                ypos +=5;
                limit = counter+1; //Allows repeated counting of
            }
            counter += 1;
        }
    }
    private void createMessabout()
    {
        int counter = 0;
        int ypos = 0;//xpos stands for X-Position
        int limit = 0;
        while( messaboutWorld[counter] != 99)
        {
            //the follwing are te 3 checks for what a tile might be
            if(messaboutWorld[counter] == 1)
            {
                addObject(new Wall(), xCoordinate[counter-limit] ,ypos+2);
            }
            if(messaboutWorld[counter] == 2)
            {
                addObject(new Enemy(movedIncrementor(false)), xCoordinate[counter-limit] ,ypos+2);
            }
            if(messaboutWorld[counter] == 3)
            {
                addObject(new Spawner(1), xCoordinate[counter-limit] ,ypos+2);
            }
            if(messaboutWorld[counter] == 6)
            {
                addObject(new Controls(), xCoordinate[counter-limit] ,ypos+2);
            }
            if(messaboutWorld[counter] == 7)
            {
                addObject(new Spawner(1), xCoordinate[counter-limit] ,ypos+2);
            }
            
            
            if(messaboutWorld[counter] == 9)
            {
                ypos +=5;
                limit = counter+1; //Allows repeated counting of
            }
            counter += 1;
        }
    }
    private void createMaze1()
    {
        int counter = 0;
        int ypos = 0;//xpos stands for X-Position
        int limit = 0;
        while( mazeWorld1[counter] != 99)
        {
            //the follwing are te 3 checks for what a tile might be
            if(mazeWorld1[counter] == 1)
            {
                addObject(new Wall(), xCoordinate[counter-limit] ,ypos+2);
            }
            if(mazeWorld1[counter] == 2)
            {
                addObject(new Enemy(movedIncrementor(false)), xCoordinate[counter-limit] ,ypos+2);
            }
            if(mazeWorld1[counter] == 3)
            {
                addObject(new Spawner(1), xCoordinate[counter-limit] ,ypos+2);
            }
            if(mazeWorld1[counter] == 7)
            {
                addObject(new Controls(), xCoordinate[counter-limit] ,ypos+2);
            }
            
            
            if(mazeWorld1[counter] == 9) //New row
            {
                ypos +=5;
                limit = counter+1;
            }
            counter += 1;
        }
    }
alittle_bit alittle_bit

2017/5/10

#
Almost forgot to add in the world constructor (though, is not really needed):
public Game()
    {    
        super(90, 60, 10);
        thePlayer = new Player();
        addObject(thePlayer,2,32);
        setWorld(3);
        addObject(new Controls(), 83, 55);
    }
Thanks for reading! p.s. The 'Level_goal' & Controls class just moves itself between levels in order to retain which level number I'm up to, so needing him in map construction methods
SR20 SR20

2017/5/10

#
The join() method also joins all array elements into a string. It behaves just like toString(), but in addition you can specify the separator: var fruits = ; document.getElementById("demo").innerHTML = fruits.join(" * "); When you work with arrays, it is easy to remove elements and add new elements. This is what popping and pushing is: Popping items out of an array, or pushing items into an array. try this <?php function array_rotate($array, $shift) { if(!is_array($array) || !is_numeric($shift)) { if(!is_array($array)) error_log(__FUNCTION__.' expects first argument to be array; '.gettype($array).' received.'); if(!is_numeric($shift)) error_log(__FUNCTION__.' expects second argument to be numeric; '.gettype($shift)." `$shift` received."); return $array; } $shift %= count($array); //we won't try to shift more than one array length if($shift < 0) $shift += count($array);//handle negative shifts as positive return array_merge(array_slice($array, $shift, NULL, true), array_slice($array, 0, $shift, true)); } ?> A few simple tests: <?php $array=array("foo"=>1,"bar"=>2,"baz"=>3,4,5); print_r(array_rotate($array, 2)); print_r(array_rotate($array, -2)); print_r(array_rotate($array, count($array))); print_r(array_rotate($array, "4")); print_r(array_rotate($array, -9)); ?>
SR20 SR20

2017/5/11

#
.
SR20 SR20

2017/5/11

#
After further research this should solve your problem!' class ArrayDemo { public static void main(String args) { // declares an array of integers int anArray; // allocates memory for 10 integers anArray = new int; // initialize first element anArray = 100; // initialize second element anArray = 200; // and so forth anArray = 300; anArray = 400; anArray = 500; anArray = 600; anArray = 700; anArray = 800; anArray = 900; anArray = 1000; System.out.println("Element at index 0: " + anArray); System.out.println("Element at index 1: " + anArray); System.out.println("Element at index 2: " + anArray); System.out.println("Element at index 3: " + anArray); System.out.println("Element at index 4: " + anArray); System.out.println("Element at index 5: " + anArray); System.out.println("Element at index 6: " + anArray); System.out.println("Element at index 7: " + anArray); System.out.println("Element at index 8: " + anArray); System.out.println("Element at index 9: " + anArray); } } The output from this program is: Element at index 0: 100 Element at index 1: 200 Element at index 2: 300 Element at index 3: 400 Element at index 4: 500 Element at index 5: 600 Element at index 6: 700 Element at index 7: 800 Element at index 8: 900 Element at index 9: 1000 Hope this helps! I have only just started greenfoot and coding about a month ago, but i think i have got the hang of it!
Yehuda Yehuda

2017/5/11

#
@SR20 You should use code tags when posting code (or program output, but that's more opinion/optional). The coding in your class doesn't make any sense.
danpost danpost

2017/5/11

#
You could use a general method that starts like this:
private void createMap(int[] map)
and use 'map' instead of the individual names of your maps within the method. Then, here is an example of creating a 'blankWorld' (for line 110 above):
createMap(blankWorld)
Yehuda Yehuda

2017/5/11

#
(continued @SR20) You created one int variable on line 4 then on line 7 you did something which is a syntax error. The output you said will be shown won't because in all the printlns you did the same variable. Lastly I don't think the topic of this discussion was how to create and use arrays, I think that knowledge is already known as you should be able to see in the given code.
danpost danpost

2017/5/11

#
@Yehuda @SR20 The coding SR20 is providing is not java code -- it appears to be javascript code, which is different from what greenfoot uses. The second code-set given IS java code; however, when using greenfoot, the normal 'public static void main(String vars)' method is not (normally) created by the user -- and certainly not by those who know nothing about java and are just starting out in greenfoot. @SR20, try to keep things as simple as possible for most questions are asked by novices to java and greenfoot. @Yehuda, without code tags the second code-set where you said the same variable was being used to print probably had array indices that were not shown (because no code tags were used. I think if you 'quote' the post and view it, you will see the array indices.
Yehuda Yehuda

2017/5/11

#
danpost wrote...
@Yehuda, without code tags the second code-set where you said the same variable was being used to print probably had array indices that were not shown (because no code tags were used. I think if you 'quote' the post and view it, you will see the array indices.
That's true, I didn't think of that possibility (that everything in-between brackets gets hidden). But either way this:
SR20 wrote...
After further research this should solve your problem!'
isn't true. I don't see how SR20 creating a plain java class with an array in it will solve any problem.
danpost danpost

2017/5/11

#
After the last map (starting at line 89), you could combine the maps into an array of maps:
private int[][] maps = { createWorld, messaboutWorld, blankWorld, mazeWorld1 };
Then, your 'setWorld' method can simply be this:
public void setWorld(int worldNumber)
{
    refreshWorld();
    createMap(maps[worldNumber-1]);
}
SR20 SR20

2017/5/11

#
Everyone is roasting me, i was only trying to help ;(
alittle_bit alittle_bit

2017/5/11

#
Thanks for the help guys! thanks @SR20 for giving a solution a shot anyhow & I suppose knowing the background as to how arrays go around outputting their elements is interesting/useful to know if/when I progress to learning either javascript or straight up java. @danpost your solution makes sense, I just hadn't thought that setting the condition of 'setWorld' to requiring an array would be allowed by Greenfoot, just didn't check myself *derp*. Your solution at
private int maps = { createWorld, messaboutWorld, blankWorld, mazeWorld1 };
means that 'maps' is a 2d array doesn't it? Anyway, I'll test out the solution with & without the second set of square brackets to determine that for myself this time, thanks for the community support!
SR20 SR20

2017/5/12

#
@Yehuda Guess what yehuda, I don't give a fucking shit about what you think cunt lmao! @danpost Get a life faggot, you just don't know my intellectual capacity in java code retard!
Kronos Kronos

2017/5/12

#
I don't know much about greenfoot, but I just have one question/suggestion. Would a boolean statement work?
There are more replies on the next page.
1
2