Adam Howitt's Blog

Sep 17
2003

Designing a scrabble engine

After I came across WordBlog on Bump I offered to build a Flash MX version of the daily puzzle for the author to add to the site.

I envisaged a Flash app which would accept a collection of "squares" , a collection of fixed tiles on the "board" and then a "rack" of "tiles". The display would be identical to the current graphics on the site with the addition of a score keeper and some extra buttons to reset the board, a score panel and drag and drop functionality to move tiles from the rack to the board.

From there my mind moved on to the bigger idea of coding a scrabble game using my newly acquired Java development and design skills. In this and future posts I will describe my design and coding process both as a personal development diary and as a tutorial for development. If anyone has any suggestions along the way about the way things are progressing I would encourage you to add comments to the relevant post.

Initial step
After having the initial idea, my first thought was to get some ideas down on paper. I know that use cases are typically a good starting point but there are two issues here. First, I want to implement the application for WordBlog's website in Flash but am seeing the design in Java. This isn't a problem since Flash MX is (loosely) object oriented so I should be able to port some concepts over. This has created the issue of over-engineering and scope creep straight off the bat. I want to design the application correctly for future enhancements and expansions (good idea) but as a result i've had to expand the scope of the design to incorporate these features. I should then be able to take the class design for the elements essential to this phase of the design and implement them, in the knowledge that further development won't require a rework of the base classes.

Here's the initial design notes I planned out last night:
Games:
Play a whole game
Play a single player rack
Play multiplayer racks //each player faces the same random racks as the other. points deducted/added for speed e.g. 10 seconds = 100%, 20 seconds = 80%, 30 seconds = 60%, 40 secs 40%, 50 seconds 20%, 1 minute 0 and bad words forfeit rack
Play a single player whole game //play out the bag with random pics

Activities:
Invite a player
Register as a player
Design a board
Design a rack
Place Hint //enter a word on your rack and find matching locations
Word Hint //Gives you suggested words for your rack
WordPlace Hint //what to spell and where to put it

Implementing Methods:
Pass in the letter list and square definitions, including fixed tiles.
Initialize board
Drag a piece onto the board
Drag a piece off the board
Score the word
Lookup the word
Wait for network players

Classes:
Letter : letter, value
Tile : letter, getValue
Board : 2d array of square objects, resetBoard(),
BoardSegment : Pointer to a start square and a count
Square : Tile object, bonusMultiplier(1/2/3), bonusType (w/l)
Bag : Collection of tile objects, resetBag(), removeRand(), removeSpecific(), pickTiles(Int numTiles)
Rack : Collection of tile objects, removeSpecificTile(), addTile(Tile), addTile(Tile[]), move(tile,square), move() //skip go
Move : tile, moveCounter, player and square
Players : Circular linked list of player objects
Player : rack, score, name
Game : players, finished flag, history
History : Array of moves, deleteMove(), playNextMove(),rollBackMoves(Int numberToRoll), rollBackMoves() //resets board,

This morning while I was creating an excel file with the list of classes, methods and data members I was able to see the picture a little clearer. I'll add the excel file in a subsequent post but basically:

  • We have a class GameSystem which has many games.
  • We have a class Game which has many Seats (gameplayers), MoveHistory, GameTiles, a Board and a Bag.
  • GameTiles is a collection of Tiles for the game
  • The Board is a collection of Squares
  • A board segment has a start Square, a direction and a number of squares to define a view of a piece of the board
  • Each Square has a current Tile, bonus multiplier and a bonus type
  • Each Bag has a subset of the GameTiles collection (Note: Bag + sum of Racks + sum of Tiles on Squares = GameTiles collection)
  • Each Tile has a Letter associated with it.
  • Each Letter has a pointsvalue assigned to it based on the Game setup.
  • The MoveHistory contains many Turns (a collection of one piece moves comprising a specific players turn
  • Each turn has a GamePlayer associated with it. (Note: if a player is switched out of a seat, the system should still track the fact that the previous player was responsible for the turn)
  • Each Seat has a Rack and a Player who is currently playing the rack.
  • Each Rack has a set number of Tiles determined by the Game setup

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment] [Subscribe to Comments]
  1. MoveHistory is probably better described as TurnHistory. TurnHistory contains a collections of Turns. Each Turn contains a sequence of Moves. Each Move is described as a Tile being placed on a Square.

  2. A Turn is made by a Player and not the Seat.

  3. I didn't get your message about building a Flash version of the game. Drop me a line, we'd like to have a Flash version of each puzzle on the site. There are a few criteria though for any Flash version we put on the site.

    The main requirement is that it can be easily updated (preferably by passing parameters on a URL), for example

    http://blah.com/scrabble.html?letters=ABCDEFG&letter_on_board=H&position_on_board=4

    We also need a way of being able to easily link to the correct comments page for adding your comments

    It would be nice if there was a button to take the user off to the word checker to see if the word was valid

    The tiles should of course be draggable and not stackable (thus losing one tile under another)

    Please drop me a line at snowman()everythinghurts dot com

  4. Hi, I need to develop scrabble in Flex / Actionscript or Flash. Looking to buy some source code form you, Thanks, Rahul.

[Add Comment]