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

2 days ago

Using an array switch to a new world

thunesm thunesm

2 days ago

#
I am looking to use an array to identify which worlds are abutting the current world. How can I use a variable stored as a string set a new world? I have created a 2D array with the world names
public String[][] warps = {{"","C1","",""},{"C2","D1","","B1"},{"","","","C1"},{"C3","","C1",""},{"B4","C3","",""},{"C4","D3","C2","B3"},{"D4","","","C3"}
    ,{"","B4","",""},{"","C4","B3","A4"},{"C5","D4","C3","B4"},{"","E4","D3","C4"},{"E5","","","D4"},{"C6","","C4",""},{"","F5","E4",""},{"","","","E5"}
    ,{"","C6","",""},{"","","C5","B6"}};
I than would like to assign those names to a set of variables
private String upWorld;
private String rightWorld;
private String downWorld;
private String leftWorld;

upWorld = warps[worldIndex][0];
rightWorld = warps[worldIndex][1];
downWorld = warps[worldIndex][2];
leftWorld = warps[worldIndex][3];
And Lastly I would like to call the world based on a location
if(getY() < 50)
        {
            Greenfoot.setWorld(upWorld);
        }
        if(getY() > getWorld().getHeight() - 50 && downWorld != "")
        {
            Greenfoot.setWorld(downWorld);
        }
danpost danpost

yesterday

#
thunesm wrote...
I than would like to assign those names to a set of variables << Code Omitted >>
First things first. Line 5, where you compare downWorld to an empty string will fail as the two strings being compared are not the SAME String object. That comparison does not compare the characters of the strings. Instead of what you used, try:
if (getY() > getWorld().getHeight()-50 && !"".equals(downWorld) )
I am looking to use an array to identify which worlds are abutting the current world. How can I use a variable stored as a string set a new world? I have created a 2D array with the world names << Code Omitted >>
Maybe use something like the following:
String worldCode= null;
if (getY() < 50 && !"".equals(upWorld)) worldCode = upWorld;
if (getX() > getWorld().getWidth()-50 && !"".equals(rightWorld) worldCode = rightWorld;
if (getY() > getWorld().getHeight()-50 && !"".equals(downWorld) worldCode = downWorld;
if (getX() < 50 && !"".equals(leftWorld) worldCode = leftWorld;
if (worldCode != null) {
    World nextWorld = null;
    int numWorld = (worldCode.charAt(0)-65)*6+(worldCode.charAt(1)-48);
    switch (numWorld) {
        case 4:  nextWorld = new A4World(); break;
        case 7:  nextWorld = new B1World(); break;
        case 9:  nextWorld = new B3World(); break;
        case 10: nextWorld = new B4World(); break;
        case 12: nextWorld = new B6World(); break;
        case 13: nextWorld = new C1World(); break;
        case 14: nextWorld = new C2World(); break;
        case 15: nextWorld = new C3World(); break;
        case 16: nextWorld = new C4World(); break;
        case 17: nextWorld = new C5World(); break;
        case 19: nextWorld = new D1World(); break;
        case 21: nextWorld = new D3World(); break;
        case 22: nextWorld = new D4World(); break;
        case 28: nextWorld = new E4World(); break;
        case 29: nextWorld = new E5World(); break;
        case 35: nextWorld = new F5World(); break;
    }
    if (nextWorld != null) Greenfoot.setWorld(nextWorld);
}
I know I probably did not get the World subclass names correct.
danpost danpost

yesterday

#
I updated my last post here to correct the code after testing. It should work properly now.
thunesm thunesm

yesterday

#
Thank you. It works as expected. How do you determine the case number used on lines 10-25
danpost danpost

yesterday

#
thunesm wrote...
Thank you. It works as expected. How do you determine the case number used on lines 10-25
Line 8 computes that value. You have a grid of worlds that takes up 6 worlds across (A through F) and 6 worlds high (1 through 6). So multiplying one by 6 and adding the other should give each world a unique value. It was then just a matter of calculating what value is produced for each actual world. If you are wondering about the subtracting of 48 and 65, you should be aware that the character code for '1' is 48 and the character code for a capital 'A' is 65. Subtracting those brings both values down to a range of between 0 and 5, inclusive. All final values will be unique and will be between 0 and 35, inclusive.
danpost danpost

yesterday

#
Looks like I missed one case world:
case 18:  nextWorld = new C6World(); return;
And actually, the final range is 1 to 36 as 49 is the character code for '1' (not 48) which shifts everything up by 1. (48 is the character code for a '0' (zero)).
You need to login to post a reply.