We've now created the logic for generating the Word Puzzles, however, we need to store the generated puzzles somewhere (in a database).
Ideally I would like to store all possible paths for a given word combination/path length as a single record.
For example, I know that there are 2 possible ways to get from aback to stank in 3 intermediate steps
aback -> alack -> slack -> slank -> stank
aback -> alack -> slack -> stack -> stank
I would like a single record that has a start word of aback, an end word of stank and stores 2 possible paths (the ones above).
With that requirement, here is the domain model.
It consists of a Puzzle object and a Path object. The Puzzle object consists of information about the Puzzle (start word, end word etc) and a collection of paths.
To see this in action, here is the sample Groovy code to retrieve it
import com.wordpath.Puzzle
def puzzle = Puzzle.findByStartWordAndEndWord('aback', 'stank')
println puzzle
puzzle.paths.each{ path ->
List lst = path.toString().tokenize(',[]') //Convert a string presentation of a list to a List
println lst
}
The output is as follows
{161} aback stank possible paths = 2 [[aback, alack, slack, slank, stank], [aback, alack, slack, stack, stank]]
[aback, alack, slack, slank, stank]
[aback, alack, slack, stack, stank]
Here are the 2 domain classes. Note the unique constraint (startWord, endWord, length).
Puzzle
package com.wordpath
import com.wordpath.Path
class Puzzle {
String startWord
List paths
String endWord
Integer possiblePaths
Integer wordLength
boolean isActive
static hasMany = [paths: Path]
static constraints = {
id generator: 'identity', column: 'id'
startWord blank: false
paths nullable: true
endWord blank: false
possiblePaths nullable: false
wordLength nullable: false
isActive nullable: false
startWord(unique: ['endWord','wordLength'])
}
String toString(){
String recordInfo = "{$id} ${startWord} ${endWord} possible paths = ${possiblePaths} ${paths}"
return recordInfo
}
}
Path
package com.wordpath
import com.wordpath.Puzzle
class Path {
String path
Puzzle puzzle
static belongsTo = [puzzle: Puzzle]
String toString(){
String recordInfo = "${path}"
return recordInfo
}
}
I am using a shortcut in storing the path as a String that is easily converted to a List using Groovy (see the comment in the first code listing above)
No comments:
Post a Comment