编辑:我使用 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");
}
}
}
}
}
.
package com.projects.aoa;
import java.util.*;
public class Itemslot extends Item{
List<Item> stack = new ArrayList<Item>();
Item oi = new Item();
boolean occupied, isArray, full;
}
.
public class Inventory {
int useableSlots, slots = 50;
Itemslot[] itemSlot = new Itemslot[slots];
...}
我有一个由 Item
对象数组组成的库存。我试图在占用另一个库存空间之前将类似的元素堆叠到一定的限制。我一定是以完全错误的方式来解决这个问题的,因为我的每一次尝试都遇到了同样的问题。
生命药水堆栈限制为 25,(27 生命药水将占用两个库存空间,一个有 25 个,另一个有 2 个。)
主要
playerOne.backPack.addItem(hpPotion, 20);
playerOne.backPack.printInventory();
playerOne.backPack.addItem(hpPotion, 15);
playerOne.backPack.printInventory();
Item.java
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);
}
}
库存输出:第三次尝试
addItem(hpPotion, 20);
[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
addItem(hpPotion, 15);
[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;
}
}
}
}
}
最佳答案
看起来您将相同的对象放入两个库存槽中,因此当您更改 .count 成员时,它会在两个槽中更改它。
第一个迹象是您正在将现有项目与使用 == 插入的项目进行比较。当你溢出时,尝试改变
itemslot[i] = new Item(item);
而不是
itemslot[i] = item;
假设您有一个复制构造函数。
关于java - 在玩家库存中堆叠元素(Java、Array/Arraylist),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8449150/