I’ve been trying to beat my wife at Scramble with Friends, one of those Zynga Android games, since Christmas time when my brother-in-law introduced us to the game. It was strangely addicting, and stranger yet when I couldn’t beat her. Since then, I’ve been working on relentlessly beating my wife. Strange when you put it that way…
After trying to do it the good old fashioned way, practice and/or a dictionary, I realized it would be much more fun to make a bot. So I thought long and hard about what language I should use. By long and hard I mean I cded to my Go directory and got to work.
I’ve been playing around with Go for a couple months now, but I had only made a couple basic web apps. As suspected, it was much more fun to dive into a real problem!
If you haven’t heard of Scramble with Friends, it’s a social Boggle rip off. Check out the full rules in the link, but the game is simple. You have a 4x4 board of letters and you try and make words. You can start at any letter on the board and go in any direction. You just can’t reuse a letter along the way. Nice, simple, computer likey.
I’ll go into my full solutions with tesseract-ocr and Android’s monkeyrunner in later posts. But for this post, let’s say we have a Game struct with the board represented in a 2d slice of strings.
We also have a simple Position struct and a Segment struct representing current board positions and the partial (maybe complete) word those positions make up
To traverse the board and try and form words, we need to move around. My first approach was simple, but I felt miserable as I typed it.
Of course I would have factored out the if into a function, but you still have to figure out where to go for the next statment. I had done this before in programming competitions and it had never felt right. That’s when I remebered an awesome difference between Go and those languages I had used. We have first class functions!
I thought this approach was much cleaner. What do you think? Is this idiomatic Go? Is there a more elegant way to solve these kinds of problems?