#1gam Jan 2017 - Part 1
Theme: Friends

Ok. So I didn’t finish my November 2016 entry and totally skipped the December entry for #1gam as well. I can come up with a bunch of excuses, but that doesn’t change the fact that I didn’t deploy a game. It’s a new year, this time its going to be different. Right?

The theme of the month is “Friends.” Here’s what @McFunkypants said:

Make a game with the word “friends” in the title, explore friendship of any form, and tell a story about the bonds that connect us to others. Explore loneliness, or imagine a future where everyone gets along.

Lately, I’ve been playing a whole bunch of Skyrim Special Edition. With EnaiSiaion’s amazing collection of mods installed, I made a Khajiit dragonborn specializing in pickpocketing, potions, and punching faces. I always enjoyed the idea of reverse-pickpocketing items onto enemies; sneaking behind people and manipulating them without them noticing. I thought maybe I could make a game based on that.

I came up with a name for the game, “How to Hack Friends and Influence Robots.” The general concept is that the player controls a robot who wants to befriend other robots. Every robot has a set number of personality traits. The player has an inventory of personality traits that they can equip/unequip at will. You can “hack” a robot and swap their personality with one in your inventory. In order to befriend a robot, your equipped personality must be similar to him/her. If you equip any personality traits that are opposite of your current friends, they will stop being your friend.

The rules make sense in my head, but that honestly really doesn’t mean anything until I have a prototype working and can play it.

I wanted to have a list of personality traits ready, so I took a list from this site http://examples.yourdictionary.com/examples-of-personality-traits.html.

I hacked together a simple java program that reads a text file of words, pulls synonyms and acronyms from the pydictionary API, and then stores all of that into a json file. Every word needed an array of synonyms and acronyms, and any new word found with the pydictionary API needed to be added as a key in the json file. If I had a list of synonyms S and a list of acronyms A, all words in S would be synonyms of each other and would share acronyms in A. All words in A would be synonyms of each other and would share acronyms in S. Words should not be contained in their own list of acronyms and synonyms.

It is a bit challenging to explain, but if I had the word “Rude” with synonyms [boorish, insulting, abusive, vulgar, impolite] and antonyms [polite, kind, mannerly, respectful, gradual], I wanted a json that looks like this:

{
  "boorish": {
    "synonyms":["insulting","abusive","vulgar","impolite","rude"],
    "antonyms":["polite","kind","mannerly","respectful","gradual"]
  },
  "polite":
  {
    "synonyms":["kind","mannerly","respectful","gradual"],
    "antonyms":["boorish","insulting","abusive","vulgar","impolite","rude"]
  },
  "impolite":
  {
    "synonyms":["boorish","insulting","abusive","vulgar","rude"],
    "antonyms":["polite","kind","mannerly","respectful","gradual"]
  },
  "rude":
  {
    "synonyms":["boorish","insulting","abusive","vulgar","impolite"],
    "antonyms":["polite","kind","mannerly","respectful","gradual"]
  },
  "mannerly":
  {
    "synonyms":["polite","kind","respectful","gradual"],
    "antonyms":["boorish","insulting","abusive","vulgar","impolite","rude"]
  },
  "gradual":
  {
    "synonyms":["polite","kind","mannerly","respectful"],
    "antonyms":["boorish","insulting","abusive","vulgar","impolite","rude"]
  },
  "abusive":
  {
    "synonyms":["boorish","insulting","vulgar","impolite","rude"],
    "antonyms":["polite","kind","mannerly","respectful","gradual"]
  },
  "vulgar":
  {
    "synonyms":["boorish","insulting","abusive","impolite","rude"],
    "antonyms":["polite","kind","mannerly","respectful","gradual"]
  },
  "kind":
  {
    "synonyms":["polite","mannerly","respectful","gradual"],
    "antonyms":["boorish","insulting","abusive","vulgar","impolite","rude"]
  },
  "insulting":
  {
    "synonyms":["boorish","abusive","vulgar","impolite","rude"],
    "antonyms":["polite","kind","mannerly","respectful","gradual"]
  },
  "respectful":
  {
    "synonyms":["polite","kind","mannerly","gradual"],
    "antonyms":["boorish","insulting","abusive","vulgar","impolite","rude"]
  }
}

Here’s a gist of the java program. https://gist.github.com/MyBadRyBad/5a3697773c3200a97b5fe781ba3ee7c9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Words {
    private static final String KEY_SYNONYMS = "synonyms";
    private static final String KEY_ANTONYMS = "antonyms";

    private static final String synonymURL = "http://pydictionary-geekpradd.rhcloud.com/api/synonym/";
    private static final String antonymURL = "http://pydictionary-geekpradd.rhcloud.com/api/antonym/";

    public static void main(String[] args) throws Exception {

        if (args.length != 2) {
            System.out.println("usage: java Words inputfilename.txt outputfile.json");
        } else {
            Words words = new Words();
            JSONObject object = words.generateWords(args[0]);

            FileWriter fileWriter = new FileWriter(args[1]);
            fileWriter.write(object.toJSONString());
            fileWriter.flush();
            fileWriter.close();
        }
    }

    public JSONObject generateWords(String filename) throws Exception {
        JSONObject jsonWords = new JSONObject();
        FileInputStream fstream = new FileInputStream(filename);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        JSONParser parser = new JSONParser();

        String strLine;

        while ((strLine = br.readLine()) != null) {
            if (strLine.length() > 0 && strLine.charAt(0) != '#') {
                Object synonyms = parser.parse(getSynonym(strLine));
                Object antonyms = parser.parse(getAntonym(strLine));

                JSONArray synonymsArray = (synonyms instanceof JSONArray) ? (JSONArray)synonyms : new JSONArray();
                JSONArray antonymsArray = (antonyms instanceof JSONArray) ? (JSONArray)antonyms : new JSONArray();

                // add the current word to the list of synonyms since the word is a synonym of itself
                synonymsArray.add(strLine.toLowerCase());

                // add the array of words into the json object
                for (int index = 0; index < synonymsArray.size(); index++) {
                    String word = (String)synonymsArray.get(index);
                    addWordToJSONObject(word, jsonWords, synonymsArray, antonymsArray);
                }

                // Antonyms are synonyms of themselves, so swap antonyms and synonyms array
                for (int index = 0; index < antonymsArray.size(); index++) {
                    String word = (String)antonymsArray.get(index);
                    addWordToJSONObject(word, jsonWords, antonymsArray, synonymsArray);
                }

                System.out.println(jsonWords.toJSONString());
            }
        }

        br.close();

        return jsonWords;
    }


    private void addWordToJSONObject(String word, JSONObject jsonObject, JSONArray synonymsArray, JSONArray antonymsArray) {
        if (jsonObject.containsKey(word)) {
            JSONObject wordObject = (JSONObject)jsonObject.get(word);
            JSONArray existingSynonymsArray = (JSONArray)wordObject.get(KEY_SYNONYMS);
            JSONArray existingAntonymsArray = (JSONArray)wordObject.get(KEY_ANTONYMS);

            wordObject.put(KEY_SYNONYMS, combineJSONArray(word, existingSynonymsArray, synonymsArray));
            wordObject.put(KEY_ANTONYMS, combineJSONArray(word, existingAntonymsArray, antonymsArray));

            jsonObject.put(word, wordObject);
        }  else {
            JSONObject wordObject = new JSONObject();
            wordObject.put(KEY_SYNONYMS, copyJSONArray(word, synonymsArray));
            wordObject.put(KEY_ANTONYMS, copyJSONArray(word, antonymsArray));

            jsonObject.put(word, wordObject);
        }
    }

    private JSONArray copyJSONArray(String keyWord, JSONArray array) {
        JSONArray newArray = new JSONArray();
        for (int index = 0; index < array.size(); index++) {
            String relatedWord = (String)array.get(index);
            if (!relatedWord.equals(keyWord))
                newArray.add(relatedWord);
        }

        return newArray;
    }

    private JSONArray combineJSONArray(String keyWord, JSONArray array1, JSONArray array2) {
        JSONArray newArray = new JSONArray();

        for (int index = 0; index < array1.size(); index++) {
            String relatedWord = (String)array1.get(index);
            if (!relatedWord.equals(keyWord))
                newArray.add(relatedWord);
        }

        for (int index = 0; index < array2.size(); index++) {
            String relatedWord = (String)array2.get(index);
            if (!relatedWord.equals(keyWord) && !newArray.contains(relatedWord))
               newArray.add(relatedWord);
        }

        return newArray;
    }


    private String getSynonym(String word) throws Exception {
        return requestFromURL(new URL(synonymURL + word));
    }

    private String getAntonym(String word) throws Exception {
        return requestFromURL(new URL(antonymURL + word));
    }

    private String requestFromURL(URL url) throws Exception {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println(response.toString());

        return response.toString();
    }
}

It’s not the most efficient or safest code, and it honestly took me much longer than I expected. Also, since I was using pydictionary (it was my first google search result), it probably would have been better to write in python. In fact, I have no idea why I even chose Java. I haven’t worked with Java in maybe 5-6 years. The last time was probably junior year in college. Regardless, it got the job done.

The next step is to build a playable prototype of the game. However, I admit that I have a habit of writing spurts of code like the one above and moving on to something else. I just need to be aware that I need to stay focused and prevent myself from moving on to a different project prematurely.

Written by Ryan Bruce Badilla on 11 January 2017