Last active
April 21, 2016 16:52
-
-
Save mustmodify/04f4b41d65f5d74e06c6f2bb83a701ae to your computer and use it in GitHub Desktop.
many implementations of one interface... @board[x, y]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Board | |
def [](row, col) | |
@grid[[row, col]] | |
end | |
private | |
def default_grid | |
{} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Board | |
def [](row, col) | |
@grid[row][col] | |
end | |
private | |
def default_grid | |
Array.new(10){ Array.new(10) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Board | |
def grid(*atts) | |
@grid[atts] | |
end | |
def default_grid | |
Hash.new | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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