Skip to content

Instantly share code, notes, and snippets.

@mustmodify
Last active April 21, 2016 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustmodify/04f4b41d65f5d74e06c6f2bb83a701ae to your computer and use it in GitHub Desktop.
Save mustmodify/04f4b41d65f5d74e06c6f2bb83a701ae to your computer and use it in GitHub Desktop.
many implementations of one interface... @board[x, y]
class Board
def [](row, col)
@grid[[row, col]]
end
private
def default_grid
{}
end
end
class Board
def [](row, col)
@grid[row][col]
end
private
def default_grid
Array.new(10){ Array.new(10) }
end
end
class Board
def [](row, col)
i = (row * 10) + col # so it's just a single array stacked 123456789012345678901234567890
@grid[i]
end
private
def default_grid
Array.new
end
end
class Board
def grid(*atts)
@grid[atts]
end
def default_grid
Hash.new
end
end
# and once you get really fancy and you want to host this game on 10 servers using round-robin
class Board
attr_accessor :id
def key_for( x, y )
"spot_#{id}_#{x}_#{y}"
end
def grid(x,y)
lookup_in_memcache( key_for( x, y ) )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment