Hello everyone, I'm still kind of new to programming and I need some help on a project I just started..
How it works:
The 'game' class has three objects: inventory, items and miner
Inventory has 20 slot objects and items has an arraylist which contains item objects
gamelogic.png
Now..
I want the miner object to be able to check the amount of slots which are free in the inventory, but I'm not sure about how to do this
Should I pass the inventory object to each new miner object I create? Or is there another way to do this?
Right now the only arguments which are given to a new miner object are the rock it should mine and the amount of ores it has to mine, for example:
Miner miner = new Miner(Ore.COAL, 20); would mine 20 coal ores.
LONG STORY SHORT:
I need the best way to let the miner object get the 'slotsOccupied' variable from the inventory object
What I have so far:
Game
public class Game { private Items items; private Inventory inventory; public Game() { items = new Items(); for (Rock rock : Rock.values()) items.addItem(rock.toString(), rock.toString() + " ore", rock.getPrice()); items.addItem("Pickaxe", "It's a pickaxe", 1000); inventory = new Inventory(); mineStuff(); } public void mineStuff() { Miner miner = new Miner(Rock.TIN, 20); } }
Items
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Items { List<Item> items = new ArrayList<Item>(); public void addItem(String name, String description, int price) { items.add(new Item(items.size(), name, description, price)); } public void deleteItem(int itemID) { items.remove(itemID); } public int getItemID(String name) { Iterator<Item> it = items.iterator(); while (it.hasNext()) { Item item = it.next(); if (item.getName().equalsIgnoreCase(name)) return item.getItemID(); } return -1; } public String getItemName(int itemID) { return items.get(itemID).getName(); } public String getItemDescription(int itemID) { return items.get(itemID).getDescription(); } public int getItemPrice(int itemID) { return items.get(itemID).getPrice(); } }
Item
public class Item { private int itemID; private String name; private String description; private int price; public Item(int itemID, String name, String description, int price) { this.itemID = itemID; this.name = name; this.description = description; this.price = price; } public int getItemID() { return itemID; } public String getName() { return name; } public String getDescription() { return description; } public int getPrice() { return price; } }
Inventory
public class Inventory { public static final int MAX_SLOTS = 20; private int slotsOccupied = 0; private Slot[] inventory = new Slot[MAX_SLOTS]; public boolean put(Item item, int amount) { if (slotsOccupied < MAX_SLOTS) { inventory[slotsOccupied++] = new Slot(item, amount); return true; } else { return false; } } public Item getItem(int slot) { return inventory[slot].getItem(); } public int getSlotsOccupied() { return slotsOccupied; } @Override public String toString() { StringBuilder s = new StringBuilder(); for (int i = 0; i < inventory.length; i++) s.append(inventory[i].getItem().getName() + " x " + inventory[i].getAmount() + "\n"); return s.toString(); } }
Slot
public class Slot { private Item item; private int amount; public Slot(Item item, int amount) { this.item = item; this.amount = amount; } public void setAmount(int amount) { this.amount = amount; } public void setItem(Item item) { this.item = item; } public int getAmount() { return amount; } public Item getItem() { return item; } }
Rock
enum Rock { TIN(1, 10), COPPER(1, 10), IRON(5, 15), COAL(30, 25), MITHRIL(45, 80), ADAMANT(60, 120), RUNE(85, 200); private int requiredLevel; private int price; private Rock(int requiredLevel, int price) { this.requiredLevel = requiredLevel; this.price = price; } public int getRequiredLevel() { return requiredLevel; } public int getPrice() { return price; } @Override public String toString() { return name().toLowerCase(); } }
Miner
public class Miner { private Rock rock; private int amount; public Miner(Rock rock, int amount) { if (amount < Inventory.MAX_SLOTS) { //here I need the slotsOccupied for! this.rock = rock; this.amount = amount; } } /* public void start() { try { } catch (InterruptedException ex) { System.out.println(ex.getMessage()); } } */ }
If you have any questions, please ask!
Thank you for your time (: