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

2014/6/7

Help with 2048

midnightocean midnightocean

2014/6/7

#
So I'm coding 2048 and I can't get all the blocks to move... And I pretty much can't do anything else either. Please help!! I've attached my code. Thanks :) Also, this is under my numbers class which contains all the blocks. public void act() { String key = Greenfoot.getKey(); if(("up").equals(key)) up(); if(("down").equals(key)) down(); if(("left").equals(key)) left(); if(("right").equals(key)) right(); } public void up(){ if(getY() != 25){ setLocation(getX(), getY()-50); } add(); } public void down(){ if(getY() != 175){ setLocation(getX(), getY()+50); } add(); } public void left(){ if(getX() != 25){ setLocation(getX()-50, getY()); } add(); } public void right(){ if(getX() != 175){ setLocation(getX()+50, getY()); } add(); } public void add() { int num = gen.nextInt(4); int num2 = gen.nextInt(4); switch(num){ case 0: num = 25; break; case 1: num = 75; break; case 2: num = 125; break; case 3: num = 175; break; } switch(num2){ case 0: num2 = 25; break; case 1: num2 = 75; break; case 2: num2 = 125; break; case 3: num2 = 175; break; } if(("null").equals(getOneObjectAtOffset(num, num2, null))) getWorld().addObject(new a(), num, num2); }
danpost danpost

2014/6/8

#
Because you are trying to move multiple blocks when a key is pressed (it is bigger than just moving one block) the check for keystroke/movement code should go in the world class. That way you can go through each of the four rows (or columns) from the ending location backwards to determine if combining needs to be done and how far to move each block. You can also at the same time build a list of empty locations to randomly choose from for the newly added block. You would be using the World class method 'getObjectsAt' instead of the Actor class method 'getOneObjectAtOffset'. All the Number class would need is a value field with a 'get' and 'set' method for it (aside from a method to update its image when the value is set or changed). Also, would it not be easier to have a World object of (4, 4, 50) instead of (200, 200, 1) ?
midnightocean midnightocean

2014/6/8

#
Thanks so much!! and I also have another question. So i made an instance variable in my numbers class and this is what i have public int value; public Numbers(){ value = 2; } public int getValue(){ return value; } public void setValue(int i){ value = i; } and when I tried calling getValue() in the world, it couldn't find the method... am i doing something wrong?? This is what I wrote in the world if((getObjectsAt(0, 0, null)).getValue() == (getObjectsAt(0, 1, null)).getvalue()){ }
danpost danpost

2014/6/8

#
The 'getObjectsAt' method returns an List object. If the lists are both not empty, then you can compare the values of the elements after casting them to Numbers type (the elements in the List object are cast as Object types and the compiler will not look for the method in the Numbers class until type are cast properly).
if (!getObjectsAt(0,0,null).isEmpty() && !getObjectsAt(0,1,null).isEmpty() &&
    ((Numbers)getObjectsAt(0,0,null).get(0)).getValue() == ((Numbers)getObjectsAt(0,1,null).get(0)).getValue()){
}
midnightocean midnightocean

2014/6/9

#
This is the code I have for my up() method. There aren't any errors and I can compile it but when I run the code, the blocks won't move. New blocks are still being added though, so the method is still being called but I don't know why the blocks aren't moving.
public void up(){
        for(int i = 0; i < 4; i++) {
            if(("null").equals(getObjectsAt(i, 0, null))){
                removeObjects(getObjectsAt(i, 1, null));
                addObject(new Numbers(), i, 0);
            } 
            if (!getObjectsAt(i,0,null).isEmpty() && !getObjectsAt(i,1,null).isEmpty() && ((Numbers)getObjectsAt(i,0,null).get(0)).getValue() == ((Numbers)getObjectsAt(i,1,null).get(0)).getValue()){ 
                removeObjects(getObjectsAt(i, 1, null));
                ((Numbers)getObjectsAt(i, 0, null)).setValue(((Numbers)getObjectsAt(i,0,null)).getValue() * 2);
            }
        
            if(("null").equals(getObjectsAt(i, 1, null))){
                removeObjects(getObjectsAt(i, 2, null));
                addObject(new Numbers(), i, 1);
            } 
            if (!getObjectsAt(i, 1,null).isEmpty() && !getObjectsAt(i, 2,null).isEmpty() && ((Numbers)getObjectsAt(i, 1,null).get(0)).getValue() == ((Numbers)getObjectsAt(i, 2,null).get(0)).getValue()){ 
                removeObjects(getObjectsAt(i, 2, null));
                ((Numbers)getObjectsAt(i, 1, null)).setValue(((Numbers)getObjectsAt(i,1,null).get(0)).getValue() * 2);
            }
        
            if(("null").equals(getObjectsAt(i, 2, null))){
                removeObjects(getObjectsAt(i, 3, null));
                addObject(new Numbers(), i, 2);
            } 
            if (!getObjectsAt(i, 2,null).isEmpty() && !getObjectsAt(i, 3, null).isEmpty() && ((Numbers)getObjectsAt(i, 2,null).get(0)).getValue() == ((Numbers)getObjectsAt(i, 3,null).get(0)).getValue()){ 
                removeObjects(getObjectsAt(i, 3, null));
                ((Numbers)getObjectsAt(i, 2, null)).setValue(((Numbers)getObjectsAt(i,2,null).get(0)).getValue() * 2);
            }
            
            if((getObjectsAt(i, 2, null)).equals("null")){
                removeObjects(getObjectsAt(i, 3, null));
                addObject(new Numbers(), i, 2);
            } 
            if (!getObjectsAt(i,2,null).isEmpty() && !getObjectsAt(i,3,null).isEmpty() && ((Numbers)getObjectsAt(i,2,null).get(0)).getValue() == ((Numbers)getObjectsAt(i,3,null).get(0)).getValue()){ 
                removeObjects(getObjectsAt(i, 3, null));
                ((Numbers)getObjectsAt(i, 2, null)).setValue(((Numbers)getObjectsAt(i,2,null).get(0)).getValue() * 2);
            }
        }
        add();
    }
danpost danpost

2014/6/9

#
Again, let me say that 'getObjectsAt' always returns a List object. It never returns a null value and it never returns a String (which is actually what you are comparing it to by using 'if (("null").equals(getObjectsAt(i, 0, null)))'. Since it obviously is not equals to the string "null", it will always return a false value and the code inside the 'if' block will never execute.
danpost danpost

2014/6/9

#
An easier way to go about executing a move is to keep track of (1) how many blocks have been moved; and (2) the value of the last block moved. If moving up, then, for each column. start at the top and working down (a) check for a block and if encountered, if the last block value equals this block value, remove this block and double the last block value (the block can be located by the value of the number of blocks moved or it can be held in a field). Once a combining has occurred, set the last block value to zero. If not combining, set its location, bump number of blocks moved and set last block value to its value. Do not forget to reset the last block value and the number of blocks moved back to zero before each column.
You need to login to post a reply.