0
\$\begingroup\$

I have a game with an inventory, you can pick up items by walking on them. When you walk on an item and if it isn't already in your inventory it is added to the next free slot. If it is already in your inventory it should stack the items to a maximum of 99 similar to minecraft. The problem is (for example) the first three slots are empty and the fourth has wood in it. when you pick up another piece of wood it should stack with the wood in slot four. It doesn't, instead it starts a new stack in the first empty slot. In order for the wood to stack it has to be in the first empty slot. That's bad.

My Array looks like this:

public static ArrayList<cell> c = new ArrayList<cell>();

My code:

  for (int i = 0; i < c.toArray().length; i++) {

        for (int i1 = 0; i1 < play.items.p.toArray().length; i1++) {

            if(c.get(i).holdingid == play.items.p.get(i1).holdingid){
                if(play.items.p.get(i1).r.intersects(play.p.r) && play.items.p.get(i1).remove == false && c.get(i).stack < 99){

                    c.get(i).stack+=1;
                    play.items.p.get(i1).remove = true;
                    //break;
                }
            }else
            if (c.get(i).holdingid == 0) {
                if(play.items.p.get(i1).r.intersects(play.p.r) && play.items.p.get(i1).remove == false){
                    play.items.p.get(i1).remove = true;
                    c.get(i).holdingid = play.items.p.get(i1).holdingid;
                    c.get(i).stack+=1;
                    break;
                }

            }

        }

The first "if" condition checks to see if there are any of the same item you just picked up are in your inventory. The second checks if the item isn't in your inventory and then if it adds it to the next empty slot. I have 9 slots. The problem is that if the first if checks the first slot and then if there isn't a stack of that item in your inventory already, it moves on to the next condition and checks if slot 1 is empty and then creates a new stack of that item. similar to minecraft. I need the first if loop to check all slots and if none of them have the same item that you just picked up it moves on to the second if loop. What I currently have check the 1st slot with the first if and then the second and then moves on to the next slot. How can I fix this to check all the slots with the first if and then if none of them have got the item you just picked up, it starts checking all the slots again with the second.

My slot class:

   public class cell {

public int x, y;
public int id;
public int holdingid;

public boolean sel = false;
public boolean stackable = false;
public int stack = 0;
private Image img;

public Rectangle r = new Rectangle();
public cell(int x,int y,int id,int holdingid){
    this.x = x;
    this.y = y;
    this.id = id;
    r = new Rectangle(x+3,y+3,10,10);
    this.holdingid =holdingid;
}

public void tick(){
    r = new Rectangle(x+3,y+3,10,10);

}

public void render(Graphics g){
    if (sel) {
        g.setColor(new Color(0, 168, 92, 174));
        g.fillRect(x, y, 60, 60);
        g.setColor(Color.blue);
        g.drawRect(x, y, 60, 60);
    } else {
        g.setColor(new Color(0, 168, 92, 174));
        g.fillRect(x, y, 60, 60);
        g.setColor(Color.yellow);
        g.drawRect(x, y, 60, 60);

    }

    if(holdingid != 0){
        ImageIcon i2 = new ImageIcon("res/x.png");
        img = i2.getImage();
        g.drawImage(img, x+3, y+3, 10, 10, null);

        if(r.intersects(Comp.mx,Comp.my,1,1) && Comp.ml){
            holdingid = 0;
            stack = 0;
        }
    }





    if(holdingid == 0){
        //empty
    }else if(holdingid == 1){
        ImageIcon i2 = new ImageIcon("res/shovel.png");
        img = i2.getImage();
        g.drawImage(img, x+8, y+8, 40, 40, null);

    }else if(holdingid == 2){
        ImageIcon i112 = new ImageIcon("res/axe.png");
        img = i112.getImage();
        g.drawImage(img, x+10, y+10, 40, 40, null);

    }else if(holdingid == 3){
        ImageIcon i12 = new ImageIcon("res/clippers.png");
        img = i12.getImage();
        g.drawImage(img, x+12, y+10, 40, 40, null);
    }else if(holdingid == 4){
        ImageIcon i12 = new ImageIcon("res/poo.png");
        img = i12.getImage();
        g.drawImage(img, x+12, y+10, 40, 40, null);
    }else if(holdingid == 5){
        ImageIcon i12 = new ImageIcon("res/items/acorn.png");
        img = i12.getImage();
        g.drawImage(img, x+22, y+20, 20, 20, null);
    }else if(holdingid == 6){
        ImageIcon i12 = new ImageIcon("res/items/wood.png");
        img = i12.getImage();
        g.drawImage(img, x+15, y+17, 30, 30, null);
    }else if(holdingid == 7){
        ImageIcon i12 = new ImageIcon("res/items/lax.png");
        img = i12.getImage();
        g.drawImage(img, x+12, y+10, 40, 40, null);
    }


    if(holdingid != 0 && holdingid != 1&& holdingid != 2&& holdingid != 3){
        g.setColor(Color.black);
        g.setFont(new Font("italic", Font.BOLD, 15));
        g.drawString(""+stack, x+6,y+57);

        g.setColor(Color.white);
        g.setFont(new Font("italic", Font.BOLD, 15));
        g.drawString(""+stack, x+4,y+55);

    }
}
\$\endgroup\$
4
  • \$\begingroup\$ There are no if-loops in java (neither in other programming languages) \$\endgroup\$
    – tkausl
    Commented Aug 14, 2015 at 9:15
  • \$\begingroup\$ this: if(c.get(i).holdingid == play.items.p.get(i1).holdingid){ \$\endgroup\$ Commented Aug 14, 2015 at 9:19
  • 1
    \$\begingroup\$ Thats not a loop \$\endgroup\$
    – tkausl
    Commented Aug 14, 2015 at 9:22
  • \$\begingroup\$ And that's not constructive either... @user2279603: if-else is a conditional and not a loop, even though it is getting checked within a loop. en.wikipedia.org/wiki/Conditional_(computer_programming) \$\endgroup\$
    – VaTTeRGeR
    Commented Aug 14, 2015 at 10:28

1 Answer 1

2
\$\begingroup\$

You made a simple mistake there, you go through your array, and as soon as the cell you look at does not have the specified item in it, you insert it into that cell or go on if it already has something else in it:

...else if (c.get(i).holdingid == 0) {...

instead, you could do it like this (in pseudocode):

insertItem(Item item) {
    int pos = index of cell with same type as item
              or if not found index of first free cell
    insert item in cell at index pos
}

Just go through the Array in a for loop to look at every cell and find the right value for pos

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .