Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a list of ints to a word

I am working on a game, and there is a component to this game that will assign a specific name to a creature depending on its attributes. Specifically, I have a set of ints that tell me which attributes the creature in the game possesses, and I need to formulate a name from these particular attributes.

What I was thinking was: assign a syllable to each number 1-47 (via Hashmap) and this way depending on which attribute the animal has the string would be different. However one obvious flaw is that the string would be way too long. I wrote an algorithm that essentially combines syllables if there are over 4 syllables. However this type of approach creates many similar results especially after around 20 syllables have been shrunk down into 4. Does anyone have any ideas on how I could turn a series of integers into a legible word that is somewhat unique? (Some copies are okay, but for the most part I want a unique word for each combination of numbers)

like image 834
Paul Perepel Avatar asked Feb 10 '23 13:02

Paul Perepel


1 Answers

I'm turning the list of ints into one 16-bit int using hashcode. And then converting that int into a string by pretending it's a base n number system where n is the number of syllables and each digit is a syllable. (By restricting the int to 16 bits you have fewer syllables). I don't know if that makes sense but I hope the output will satisfy your requirements.

    import java.util.Arrays;
import java.util.List;

public class NameGenerator {

    final static List<String> syllables = Arrays.asList("ka", "sa", "ta", "na", "ha", "ma", "ya", "ra", "wa",
            "ki", "si", "ti", "ni", "hi", "mi", "yi", "ri", "wi",
            "ku", "su", "tu", "nu", "hu", "mu", "yu", "ru", "wu",
            "ke", "se", "te", "ne", "he", "me", "ye", "re", "we",
            "ko", "so", "to", "no", "ho", "mo", "yo", "ro", "wo");
    final static int maxSyllable = syllables.size() - 1;

    public static void main(String[] args) {
        int[] attributes = new int[]{25, 325, 4, 2, 11, 98, 23};
        String name = toName(attributes);
        System.out.println("name = " + name);
    }

    public static String toName(int[] attributes) {
        int hashCode = Arrays.hashCode(attributes);
        int smallHashCode = (hashCode >> 16) ^ (hashCode & 0xffff);
        return toName(smallHashCode);
    }

    public static String toName(int i) {
        if (i < 0) {
            i = -i;
        }
        StringBuilder buf = new StringBuilder();
        while (i > maxSyllable) {
            buf.append(syllables.get(i % maxSyllable));
            i = i / maxSyllable;
        }
        buf.append(syllables.get(i));
        return buf.toString();
    }
}
like image 79
mikeyreilly Avatar answered Feb 13 '23 02:02

mikeyreilly