JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
basic gameplay working
[hexbog.git] / compress.coffee
diff --git a/compress.coffee b/compress.coffee
new file mode 100644 (file)
index 0000000..6fc340f
--- /dev/null
@@ -0,0 +1,31 @@
+orig_file = 'wordlist.txt'
+compressed_file = 'wordlist_compressed.js'
+caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+fs = require 'fs'
+
+fs.readFile orig_file, 'utf8', (err, data) ->
+       if err?
+               console.log "Error reading #{orig_file}: #{err}"
+               process.exit(1)
+       else
+               fs.writeFile compressed_file, (compress data.split '\n'), (err) ->
+                       if err?
+                               console.log "Error writing to #{compressed_file}: #{err}"
+                               process.exit(1)
+
+compress = (words) ->
+       out = 'parse_word_list(['
+       chunk = ''
+       prev = ''
+       for next in words
+               match = prev.length
+               while match and next.substr(0, match) isnt prev.substr(0, match)
+                       --match
+               # now match is the number of prefix characters this word has in common with the previous
+               chunk += caps[prev.length - match]
+               if chunk.length > 2000
+                       out += "'" + chunk.substr(1) + "',"
+                       chunk = '.'
+               chunk += next.substr match
+               prev = next
+       return out + "'" + chunk.substr(1) + "']);"