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

The Last Pyramid

Today saw the publication of the final issue of Steve Jackson Games's Pyramid magazine, as was announced several months ago. Broadly speaking, it was the victim of generally rough times within the gaming industry.

I'm one of what is surely a small number of people who have been published in all three iterations of Pyramid. I'd had some previous contact with SJ Games--some stuff I helped with ended up in GURPS Cyberpunk, which in turn has doubtless gotten my name on the Federal Register of Dangerous Hoodlums--but it wasn't until the later days of the paper version of Pyramid that I finally got up the nerve to try my hand at writing an article. The result was a short piece on low-tech (mostly Medieval) economies, which became my first professionally published work.

This, apparently, was enough encouragement. Having seen how painless the process actually was, I started thinking in terms of writing for publication. It didn't hurt that around this time I went to work fo…

Fraxinetum

I came across another one of those historical footnotes which would in years past have prompted a Pyramid  article. With Pyramid still gone, here we go again:
Introduction The early Middle Ages were dangerous and chaotic for much of western Europe. Vikings raided along coasts and river from the north and all the way around Europe's west coast and into the Mediterranean. Magyars attacked from the east. And Muslims (mostly but certainly not all North African Berbers) took over most of Spain and raided elsewhere along the Mediterranean coast.

But while many people know at least about the existence of Muslim Spain, fewer people are aware of Muslim France.  For a time in the 8th through 10th centuries, large stretches of the south coast of France were under Muslim rule, and parts of that were administered from one of the world's most glamorous vacation spots. This was Medieval Fraxinetum.
History In 711 AD, things were going well for the expanding Muslim caliphate. An Arab-led Berb…

What GURPS Doesn't Have

I was reading this post by Refplace about common GURPS myths: it's dead (no, it gets regular monthly support and doesn't need a new edition), there are no settings (no, there's a zillion of them), and so on. And while GURPS has lots of stuff, the no-settings meme in particular did get me thinking about notable gaps which still exist in the GURPS line. I can think of three gaps that might usefully be filled.

A vehicle design system is, of course, one of those gaps. Many games need no such thing, but general-purpose rules for stuff are very much in GURPS's lane. The 4e vehicle design book is still going through its interminable process, so I suppose we'll see it when we see it. I'm curious as to whether we'll ever see a similar gun-design system, which would also be appropriate.

Then there's a setting line. Yes, yes, I know. GURPS has settings. It has many settings. I would respectfully submit that there's no setting line for 4e in the way there is fo…