Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Gamelogic - variable in another class

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Gamelogic - variable in another class

    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 (:


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Gamelogic - variable in another class

    I solved it myself, I hope it's a good way.. :p

    Miner
    public class Miner {
     
        private Inventory inventory;
        private Items items;
     
        public Miner(Inventory inventory, Items items) {
            this.inventory = inventory;
            this.items = items;
        }
     
        public boolean mine(Rock rock, int amount) {
            if (inventory.getSlotsOccupied() + amount > inventory.MAX_SLOTS)
                return false;
            startMining(rock, amount);
            return true;
        }
     
        public void startMining(Rock rock, int amount) {
            try {
                for (int i = 0; i < amount; i++) {
                    Thread.sleep(1000);
                    inventory.put(items.getItem(items.getItemID(rock.toString())), amount);
                }
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
            }
        }
     
    }

    Game
    public class Game {
     
        private Items items;
        private Inventory inventory;
        private Miner miner;
     
        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();
     
            Miner miner = new Miner(inventory, items);
     
            mineStuff();
     
        }
     
        public void mineStuff() {
            miner.startMining(Rock.TIN, 15);
            miner.startMining(Rock.RUNE, 2);
            miner.startMining(Rock.COAL, 2);
        }
     
    }

    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();
        }
     
        public Item getItem(int itemID) {
            return items.get(itemID);
        }
     
    }

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. How do I use a variable from main class in another class?
    By El Batman in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2013, 07:34 AM
  3. Replies: 149
    Last Post: February 19th, 2013, 10:04 PM
  4. Replies: 1
    Last Post: February 14th, 2013, 07:30 PM
  5. Using variable in different class
    By ICEPower in forum Object Oriented Programming
    Replies: 13
    Last Post: May 12th, 2012, 12:32 PM