玩家库存中的堆叠物品(Java、数组/数组列表)

玩家库存中的堆叠物品(Java、数组/数组列表),java,arrays,arraylist,Java,Arrays,Arraylist,编辑:我使用ArrayList void addItem(Item item, int count){ int stacks; for (int i = 0; i < useableSlots && count != 0; i++){ if (itemSlot[i].stack.size() > 0){ if (itemSlot[i].stack.get(0) == item)

编辑:我使用
ArrayList

void addItem(Item item, int count){

        int stacks;

        for (int i = 0; i < useableSlots && count != 0; i++){
            if (itemSlot[i].stack.size() > 0){
                if (itemSlot[i].stack.get(0) == item) {
                    if(itemSlot[i].stack.size() < item.stackLimit){

                        int n = itemSlot[i].stack.size();

                        for(; n < item.stackLimit && count > 0; count--, n++) {
                            itemSlot[i].stack.add(item);
                        }
                    }
                }
            }
            if (i == (useableSlots - 1) && count > 0){
                for(int n = 0; n < useableSlots && count != 0; n++){

                    stacks = ((count - (count % item.stackLimit)) / item.stackLimit);

                    if(itemSlot[n].occupied == false){
                        if(stacks == 0){
                            for(int j = 0; j < count; j++){
                                itemSlot[n].stack.add(item);
                                itemSlot[n].occupied = true;
                            }
                            count = 0;
                        }
                        else {
                            for(int j = 0; j < item.stackLimit; j++){
                                itemSlot[n].stack.add(item);
                            }
                            count -= item.stackLimit;
                            itemSlot[n].occupied = true;
                        }
                    }
                    if (n == (useableSlots - 1)){
                        println("You don't have any room in your inventory");
                    }
                }
            }
        }
    }
我有一个由
项目
对象数组组成的库存。在占用另一个库存空间之前,我试图让类似的物品堆积到一定的限度。我一定是走错了路,因为我每次尝试都遇到同样的问题。 hp药剂堆积上限为25,(27 hp药剂将占用两个库存空间,一个25,另一个2。)

主要 Item.java 库存输出:第三次尝试 附加项(hpPotion,20)

附加项(hpPotion,15)

在第二次尝试中也会发生同样的情况,只是它显示为两个相加

[3]  hpPotion(35)        [4]  hpPotion(35)
这似乎只是一个简单的修复(也许是),但我只是不知道它可能是什么。任何帮助都将不胜感激

第三次尝试
void addItem(项目,整数计数){
布尔newStack=false;
室内,总计数=0;
对于(int i=0;i<20;i++){
if(itemSlot[i]==项和计数>0){
房间=(item.stackLimit-itemSlot[i].count);
如果(房间>0){
如果(计数>=房间){
itemSlot[i]。计数+=房间;
计数-=房间;
}
否则,如果(计数>0&&计数<房间){
itemSlot[i]。计数+=计数;
计数=0;
打破
}
}
}
如果(i>=19&&count>0){
对于(int n=0;n<20;n++){
if(itemSlot[n].filled==false){
整数堆栈=((计数-(计数%item.stackLimit))/25);
println(栈);
如果(堆栈==0){
itemSlot[n]=项目;
itemSlot[n]。已填充=true;
itemSlot[n]。计数=计数;
计数=0;
打破
}
否则{
itemSlot[n]=项目;
itemSlot[n]。已填充=true;
itemSlot[n]。计数=item.stackLimit;
count-=item.stackLimit;
}
}
}
}
}
}
第二次尝试
void addItem(项目,整数计数){
布尔newStack=false;
室内,总计数=0;
outerLoopCurrentStack:
对于(int i=0;i<20;i++){
if(itemSlot[i].name==item.name){
if(itemSlot[i].count0){
计数--;
itemSlot[i].count++;
if(itemSlot[i].count==itemSlot[i].stackLimit){
断开outerLoopCurrentStack;
}
}
}
}
如果((i>=19)和(&(计数>0))为else{
newStack=true;
}
}
if(newStack=true){
outerLoopNewStack:
对于(int i=0;i<20;i++){
if(itemSlot[i].filled==false){
itemSlot[i]=项目;
itemSlot[i].filled=true;
而(计数>0){
计数--;
itemSlot[i].count++;
如果(计数=0){
newItem=false;
计数=0;
打破外环新闻钉;
}
}
}
}
}
}
}  
第一次尝试
void addItem(项目,整数计数){
布尔newStack=false;
室内,总计数=0;
对于(int i=0;i<20;i++){
if(itemSlot[i].name==item.name){
if(itemSlot[i]。计数房间){
itemSlot[i]。计数+=房间;
itemSlot[i].filled=true;
计数-=房间;
}
否则{
itemSlot[i]。计数+=计数;
打破
}
}
}
否则如果(i==19){
newStack=true;
}
}
if(newStack==true){
对于(int i=0;i<20;i++){
if(itemSlot[i].filled==false){
如果(计数>项目堆栈限制){
itemSlot[i]=项目;
itemSlot[i]。计数=item.stackLimit;
itemSlot[i].filled=true;
count-=item.stackLimit;
}
否则{
itemSlot[i]=项目;
itemSlot[i]。计数=计数;
itemSlot[i].filled=true;
打破
}
}
}
}
}
,将物品视为库存“内容”的一部分

然后,您可以拥有一个包,该包“包含”在用户的内容中,同时拥有包中包含的内容

顶层的东西,比如计算重量,将把当前容器中每个项目的重量加起来。如果这涉及到询问作为容器的物品的重量,那么容器物品将使用相同的技术计算其重量(直到最终非容器物品只报告其未计算的重量)

无论采用哪种方式,您都希望摆脱表现出行为的集合的数组,因为您不能使用数组将行为添加到集合中,而将其放在外部(在一组方法中)最终会使代码难以维护/修复/更改。

,而项目的眼睛是清单“内容”的一部分

然后,您可以拥有一个包,该包“包含”在用户的内容中,同时拥有包中包含的内容

顶层的东西,比如计算重量,将把当前容器中每个项目的重量加起来。如果要问的话
public class Inventory {

    int useableSlots, slots = 50;
    Itemslot[] itemSlot = new Itemslot[slots];


...}
playerOne.backPack.addItem(hpPotion, 20);
playerOne.backPack.printInventory();
playerOne.backPack.addItem(hpPotion, 15);
playerOne.backPack.printInventory();
package com.projects.aoa;

import static com.projects.aoa.Print.*;

import java.util.*;
import java.io.*;

class Item {

//Item
String name, type, category;
int id;
int hp, mp, str, def, duration;

//Inventory
public boolean filled;
public int count, stackLimit;

static void getAllStats(Item[] e){
    for(Item i : e){
        getItemStats(i);
    }
}

static void getItemStats(Item i){
    i.getStats();
}

void getStats(){
    try {
        //System.out.println(System.getProperty("user.dir"));

        FileInputStream fstream = new FileInputStream(System.getProperty("user.dir") 
                + "/src/com/projects/aoa/" + this.type + "_" + this.name + ".txt");

        DataInputStream in = new DataInputStream(fstream);

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String line;
        int counter = 0;

        while ((line = br.readLine()) != null) {
            if (line.length() == 0){
                break;
            }

            switch (counter) {
            case 0:
                this.hp = Integer.parseInt(line);
                counter++;
                break;
            case 1:
                this.mp = Integer.parseInt(line);
                counter++;
                break;
            case 2:
                this.def = Integer.parseInt(line);
                counter++;
                break;
            case 3:
                this.str = Integer.parseInt(line);
                counter++;
                break;
            case 4:
                this.stackLimit = Integer.parseInt(line);
                counter++;
                break;
            case 5:
                this.duration = Integer.parseInt(line);
                counter++;
                break;
            }   
        }


        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

void printStats(){
    println("[" + name + "]");
    println("Type: " + type);
    println("Duration: " + duration);
    println("HP:  " + hp);
    println("MP:  " + mp);
    println("Def: " + def);
    println("Str: " + str);
}
}
[1]  Mallet              [2]  BronzeHelmet        [3]  hpPotion(20)        [4]  Empty               [5]  Empty               

[6]  Empty               [7]  Empty               [8]  Empty               [9]  Empty               [10] Empty               

[11] Empty               [12] Empty               [13] Empty               [14] Empty               [15] Empty               

[16] Empty               [17] Empty               [18] Empty               [19] Empty               [20] Empty  
[1]  Mallet              [2]  BronzeHelmet        [3]  hpPotion(10)        [4]  hpPotion(10)        [5]  Empty               

[6]  Empty               [7]  Empty               [8]  Empty               [9]  Empty               [10] Empty               

[11] Empty               [12] Empty               [13] Empty               [14] Empty               [15] Empty               

[16] Empty               [17] Empty               [18] Empty               [19] Empty               [20] Empty 
[3]  hpPotion(35)        [4]  hpPotion(35)
void addItem(Item item, int count){

    boolean newStack = false;

    int room, totalCount = 0;

    for(int i = 0; i < 20; i++){
        if(itemSlot[i] == item && count > 0){
            room = (item.stackLimit - itemSlot[i].count);
            if(room > 0){
                if(count >= room){
                    itemSlot[i].count += room;
                    count -= room;
                }
                else if(count > 0 && count < room){
                    itemSlot[i].count += count;
                    count = 0;
                    break;
                }
            }
        }

        if(i >= 19 && count > 0){
            for(int n = 0; n < 20; n++){
                if(itemSlot[n].filled == false){
                    int stacks = ((count - (count % item.stackLimit)) / 25);
                    println(stacks);
                    if (stacks == 0){
                        itemSlot[n] = item;
                        itemSlot[n].filled = true;
                        itemSlot[n].count = count;
                        count = 0;
                        break;
                    }
                    else {
                        itemSlot[n] = item;
                        itemSlot[n].filled = true;
                        itemSlot[n].count = item.stackLimit;
                        count -= item.stackLimit;
                    }
                }
            }
        }
    }
}
    void addItem(Item item, int count){

    boolean newStack = false;

    int room, totalCount = 0;

    outerLoopCurrentStack:
    for(int i = 0; i < 20; i++) {
        if(itemSlot[i].name == item.name){
            if(itemSlot[i].count < itemSlot[i].stackLimit){

                while(count > 0){
                    count--;
                    itemSlot[i].count++;

                    if(itemSlot[i].count == itemSlot[i].stackLimit) {
                        break outerLoopCurrentStack;
                    }
                }
            }
        }
        else if((i >= 19) && (count > 0)){
            newStack = true;
        }
    }

    if(newStack = true){

        outerLoopNewStack:
        for(int i = 0; i < 20; i++){
            if(itemSlot[i].filled == false){

                itemSlot[i] = item;
                itemSlot[i].filled = true;

                while(count > 0){
                    count--;
                    itemSlot[i].count++;

                    if(count == 0){
                        newItem = false;
                        count = 0;
                        break outerLoopNewStack;
                    }
                }
            }
        }
    }
}
     }  
    void addItem(Item item, int count){

    boolean newStack = false;

    int room, totalCount = 0;

    for (int i = 0; i < 20; i++){
        if(itemSlot[i].name == item.name) {
            if(itemSlot[i].count < item.stackLimit){
                room = (item.stackLimit - itemSlot[i].count);
                if (count > room){
                    itemSlot[i].count += room;
                    itemSlot[i].filled = true;
                    count -= room;
                }
                else {
                    itemSlot[i].count += count;
                    break;
                }
            }
        }
        else if(i == 19){
            newStack = true;
        }
    }

    if (newStack == true){
        for(int i = 0; i < 20; i++){
             if(itemSlot[i].filled == false) {
                if(count > item.stackLimit){
                    itemSlot[i] = item;
                    itemSlot[i].count = item.stackLimit;
                    itemSlot[i].filled = true;
                    count -= item.stackLimit;
                }
                else{
                    itemSlot[i] = item;
                    itemSlot[i].count = count;
                    itemSlot[i].filled = true;
                    break;
                }
            }
        }
    }
     }
def addItem(item, count)

    foreach (slot in backpack)
        if (slot.type = Array<Item>)
            if (slot.count < max)

                // Enough space to add at least one more
                if (count > 1)
                    foreach (count) addItem(item, 1)
                    // yes this is recursive to get the checking
                else
                    slot.add(item)
                end

            else

                // Not enough space, create a new slot
                var newSlot
                backpack.add(newSlot)

                if (count > 1)
                    foreach (count) addItem(item, 1)
                    // yes this is recursive to get the checking
                else
                    newSlot.add(item)
                end 

            end
        end
    end
end
itemslot[i] = new Item(item);
itemslot[i] = item;