Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Created
December 16, 2013 17:18
-
-
Save ZoharLiran/7990658 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains hidden or 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 BoggleBoard | |
def initialize(board) | |
@board = board | |
end | |
def create_word(*coords) | |
coords.map{|coord| @board[coord.first][coord.last]}.join | |
end | |
def get_row(row) | |
if (row > 3 || row < 0) | |
return "Please provide row between 0 and 3" | |
else | |
print "[" | |
for i in 0..3 do | |
print "\"#{@board[row][i]}\"" | |
if i < 3 | |
print ", " | |
end | |
end | |
print "]" | |
puts | |
end | |
end | |
def get_col(col) | |
if (col > 3 || col < 0) | |
return "Please provide row between 0 and 3" | |
else | |
print "[" | |
for i in 0..3 do | |
print "\"#{@board[i][col]}\"" | |
if i < 3 | |
print ", " | |
end | |
end | |
print "]" | |
puts | |
end | |
end | |
def access_coord(x,y) | |
return @board[x][y] | |
end | |
def get_diagonal(*coords) | |
warning = 0 | |
#check for proper coordinates | |
coords.each do |coord| | |
for i in 0..1 | |
if coord[i] < 0 || coord[i] > 3 | |
warning = 1 | |
end | |
end | |
end | |
if warning == 1 | |
return "Please provide row between 0 and 3" | |
end | |
#check for a diagonal | |
warning = (coords.first[0] - coords.last[0]).abs - (coords.first[1] - coords.last[1]).abs | |
if warning != 0 | |
return "Please provide coordinates of a diagonal" | |
end | |
i = coords.first | |
j = coords.last | |
result = [] | |
begin | |
result << @board[i[0]][i[1]] | |
for n in 0..1 | |
i[n] = i[n] < j[n] ? i[n]+1 : i[n]-1 | |
end | |
end while i != j | |
result << @board[i[0]][i[1]] #for the last cell | |
return result | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns the word with the coords | |
p "Printing rows:" | |
for i in 0..3 | |
boggle_board.get_row(i) | |
end | |
p "Printing columns" | |
for i in 0..3 | |
boggle_board.get_col(i) | |
end | |
p boggle_board.access_coord(3,2) | |
p boggle_board.get_diagonal([0,3],[3,0]) #=> returns ["e", "d", "c", "t"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment