Skip to main content

Hoardomatic Code: Gem Class

Last class. Gems have their own bundle of properties, so they get their own class.

Gem


String gem_name: Name of the gem.
Double val_constant: The value constant for the stone, the V in the(C^2 + C*4) * V value formula.
Double carats: Weight of the gem in carats.

This one is similar to the previous two, but it takes on itself the task of randomly selecting a node from Gems.xml to turn into an object.   

package hoardomatic;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Gem {

    private String gem_name;
    private Double val_constant;
    private Double carats;
   
    public Gem() {
        // TODO Auto-generated constructor stub
        Node gemnode = getRandomNodeFromTable("Gems.xml");
       
        NodeList tempNodes = gemnode.getChildNodes();
        for (int j = 0; j < tempNodes.getLength(); j++) {
            Node subnode = tempNodes.item(j);

            if (subnode.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) subnode;
               
                if (element.getNodeName().contentEquals("Gem")) {
                    gem_name = element.getTextContent();
                }

                if (element.getNodeName().contentEquals("V")) {
                    val_constant =Double.parseDouble(element.getTextContent());
                }

            }

        }
       
        double dr = (double) dieRoll(1,6) + dieRoll(1,6);
        double the_carats = dr/4;
       
        int get_big = dieRoll(1,12);
        while (get_big == 12){
            the_carats = the_carats + dieRoll(1,6);
            get_big = dieRoll(1,12);
        }
       
        carats = the_carats;
       
    }
   
   
        public Node getRandomNodeFromTable(String fname) {
           
            Node return_it;
            return_it = null;

            try {
                InputStream items = new FileInputStream(new File(fname));
               
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(items);

                doc.getDocumentElement().normalize();
                // System.out.println(doc.getDocumentElement().getNodeName());
                NodeList nodes = doc.getElementsByTagName("item");
               
                Integer itemlistlen = nodes.getLength();
               
                Integer pickItm = dieRoll(0, itemlistlen - 1);
               
                // System.out.println("==========================");
//                System.out.println(pickItm);

                for (int i = 0; i < nodes.getLength(); i++) {
                    Node node = nodes.item(i);
                    if (i == pickItm) {
                        return_it = node;
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return return_it;
        }
   
   
    public Gem(Node gemnode){
       
       
        NodeList tempNodes = gemnode.getChildNodes();
        for (int j = 0; j < tempNodes.getLength(); j++) {
            Node subnode = tempNodes.item(j);

            if (subnode.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) subnode;
               
                if (element.getNodeName().contentEquals("Gem")) {
                    gem_name = element.getTextContent();
                }

                if (element.getNodeName().contentEquals("V")) {
                    val_constant =Double.parseDouble(element.getTextContent());
                }

            }

        }
       
       
        double the_carats = (dieRoll(1,6) + dieRoll(1,6))/4;
       
        int get_big = dieRoll(1,12);
        while (get_big == 12){
            the_carats = the_carats + dieRoll(1,6);
            get_big = dieRoll(1,12);
        }
       
        carats = the_carats;
       
       
    }
   
    public Double getGemCost(){
        Double return_it = 0.0;
        // (c^2 + 4 x c) ^ v
        return_it = (carats * carats + 4 * carats) * val_constant;
        return return_it;
    }
   
    public String getGemName(){
        return gem_name;
    }
   
    public Double getGemWt(){
        return carats;
    }
   
    private int dieRoll(Integer min, Integer max) {

        Random r = new Random();
        int inroll = r.nextInt(max - min + 1) + min;
        return inroll;
    }


}

Comments

Popular posts from this blog

More Nattering About Writing Historical RPGs

omeone made the mistake of asking me to expand on some things I'd said about writing historical rpg material, so you all must suffer for it. I write a lot of historical rpg stuff as well as entirely fictional setting material. So why make historically themed games rather than just making stuff up? Well, because it's an effective way of achieving the purpose of writing settings and adventures in general. The purpose of settings and adventures, I would aver, is to give players things to do in a convincing context. Convincing context doesn't necessarily mean realism. Players have different levels of understanding of what's "realistic" in different situations and are more or less sensitive to how closely those situations adhere to reality. For example, I'm a history and culture guy. Put me in SF campaigns (which happens with some regularity) and the specifics of how reaction drives, orbital mechanics, and subatomic physics work entirely escape me. I don't,...

Stuff I Wouldn't Do With Purchased Minis

One of the great things about the advent of 3d printing, when it comes to gaming, is that it allows me to take more risks and try stuff I wouldn't do otherwise. Take, for example, experimenting with painting techniques. Maybe I want to see what an unusual color scheme would look like or try doing an odd way of achieving an effect. Factory-made miniatures are expensive. Too expensive for me to risk it. Which, as someone who's not very good at painting minis and needs lots of practice, is one reason I've never gotten into them. I don't feel like I can afford the implied expense of getting better. Enter 3d printing. With the right files, I can print as many minis as I can like and mess around with them to my heart's content. Mess one up? That's fine. Resin isn't cheap , but it doesn't cost nearly as much as pre-made molded plastic. I can take it as a learning experience and move on. Which brings me to messing around with some Car Wars minis. I'd been m...

Briefly, How To Play GURPS

For a long time, I’ve maintained that GURPS, despite its reputation for complexity, is actually pretty simple in play. I was thinking recently that I should see if I can express the fundamentals of playing GURPS in a short, easily digested form, and so here I am.   This does not address a more general “how to play rpgs” for those who know nothing on the topic. How to approach GURPS, at that level, isn’t necessarily a lot different from how to approach D&D or TFT or any other RPG system. It doesn’t get into optional and campaign-specific sets of rules or equipment. Rather, this is stuff applicable to playing GURPS no matter what the campaign is. It also doesn’t address how to build GURPS characters, which is a vastly more complicated topic. Rather, this is about how to engage GURPS rules when you’ve already got your character sheet and are sitting at the table to play. It’s a trifle over 1000 words, which I think isn’t too bad.   How To Play GURPS Most of what you’ll need t...