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

CW Listicle Notes

Turns out I've got rather a lot to say about a rather short piece. Specifically, the Car Wars vignette in Pyramid #89. It's all in general pursuit of world-building, but there's probably as much world-building going on in this one as all of my previous vignettes put together. Four out of the five locations make glancing reference to notable aspects of the world of the new Car Wars, so there are some significant bits of history and culture to tease out there. The fifth...well, that was mostly just me amusing myself.

There's also a certain amount of--pardon the expression--reality in there. In figuring out where to put the various arenas, I looked at a lot of maps, lists of roadside attractions, locations of current sports venues, and other such materials. Each place has a definite location in the real world, sometimes to the point of using existing buildings. Here's where everything came from:

Big Swede Arena: Parking garage at the Emeryville Ikea. We went there a fe…

Panel Discussion Addendum

I was late to Douglas Cole's panel discussion of SJ Games staff and frequent contributors to Pyramid magazine, so I missed a lot of the questions asked directly to the authors, and technical issues prevented me from answering some others once I got there. These, then, are some things I might have said, had I been able:

Douglas: How well does that mission statement resonate with you guys? Do you like to write crazy stuff? Do you like to get out there with expansions on existing material? How do you tie what you’re writing to either the themes Steven has suggested or what’s in your heart.

I like to write a range of stuff; how I do it varies. I get an idea and go where that takes me. On one end, there's the pure historicals. In things like "A Brief History of the Thieves' Guild," it's mostly ideas and background information. Maybe I mention a skill or advantage to give it a fig-leaf of GURPS, but that's about it. At the other end, there are the crunch-heavy,…

The Occasional Dungeon: Overview

In order to get some more GURPS out there and play with some maps, I started toying with something. I've worked up a large map ("ground level" is below; I may need to poke around with image hosting to keep enough maps at the proper scales) of a dungeon complex. From time to time, I'll post magnified excerpts from the map with details in GURPS terms, with specific reference to Dungeon Fantasy (that is, mostly stocked with things from GURPS Dungeon Fantasy 8: Treasure Tables and the Dungeon Fantasy Monsters volumes, but occasional pointers elsewhere). They may prove useful to somebody somewhere under some odd set of circumstances.


This dungeon is set in a fairly steep, rocky hill. The natural caves underneath it have long been home to a variety of creatures, natural and otherwise, but pretty much all horrible. There's also a large natural cavern accessible through a very large opening at the top of the hill where the surface caved in. It has been home to a number …