Skip to content

Instantly share code, notes, and snippets.

@breun
Last active September 23, 2015 21:27
Show Gist options
  • Save breun/618271 to your computer and use it in GitHub Desktop.
Save breun/618271 to your computer and use it in GitHub Desktop.
A Groovy Scrabble scorer based on the Scala scorer at http://gist.github.com/618308
#!/usr/bin/env groovy
/**
* A Groovy port of the Scala script at http://log4p.com/2010/10/09/scala-shellscripts/
*/
if (args.length < 1) {
println "usage: ./scrabble <filename>"
return
}
new File(args[0]).eachLine { w ->
println "$w\t${scoreWord w}"
}
def scoreWord(word) {
word.inject(0) { score, c -> score + scoreChar(c) }
}
def scoreChar(c) {
switch (c) {
case 'a': return 1
case 'b': return 3
case 'c': return 5
case 'd': return 2
case 'e': return 1
case 'f': return 4
case 'g': return 3
case 'h': return 4
case 'i': return 1
case 'j': return 4
case 'k': return 3
case 'l': return 3
case 'm': return 3
case 'n': return 1
case 'o': return 1
case 'p': return 3
case 'q': return 10
case 'r': return 2
case 's': return 2
case 't': return 2
case 'u': return 4
case 'v': return 4
case 'w': return 5
case 'x': return 8
case 'y': return 8
case 'z': return 4
default: return 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment