Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save timoshka88/8726656 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle 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
#as with the PezDispenser, i decided to try out new things, and here is what I got | |
class Board_games | |
attr_reader :board # make the board possible to view | |
def initialize (board = []) | |
puts "Board ready, if you want to mix the letters and start with a new board, just shake" | |
@board = board | |
the_choice_of_letters = ('a'..'z').to_a | |
if @board.empty? | |
index = 0 | |
while index < 4 | |
@board = @board.insert(index, the_choice_of_letters.sample(4)) | |
index += 1 | |
end | |
end | |
#in case we don't have a created board, than we would have it created for us | |
@board.each do |row| #this will give a noraml board view to which the user is used to..no arrays | |
puts row.join(" ") | |
end | |
# @board.each do |array| | |
# p array | |
# end | |
#this will give a second board view but showing the nested arrays | |
end | |
def shake_board #you can toggle the letters and they will change, so if you want to change your board's letters, you can do it by calling #shake_board | |
board_new = [] | |
index = 0 | |
while index < 4 | |
board_new = @board.insert(@board[index].replace(the_choice_of_letters.sample(4))) | |
index += 1 | |
end | |
board_new | |
end | |
def create_word(*coords) | |
coords.map {|coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
raise ArgumentError.new("You can choose only from 0 to 3") if row >= 4 | |
@board[row] | |
end | |
def get_col(row) | |
raise ArgumentError.new("You can choose only from 0 to 3") if row >= 4 | |
array_of_column_values = [] | |
@board[row].each_index{|i| array_of_column_values << @board[i][row]} | |
array_of_column_values | |
end | |
def get_diagonal(row = 0, col = 0) | |
diagonal = [] | |
while row < 4 && col < 4 # 4 because the length of the nested array is 4 | |
diagonal = diagonal.push(@board[row][col]) | |
row +=1 | |
col +=1 | |
end | |
return diagonal | |
end | |
end | |
board_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
new_boggle_board = Board_games.new(board_grid) #=>Board ready, if you want to mix the letters and start with a new board, just shake | |
#b r a e | |
#i o d t | |
#e c l r | |
#t a k e | |
p new_boggle_board.board[3][2] #=> "k". by calling the #board method | |
p new_boggle_board.create_word([2,1],[1,1],[1,2],[0,3]) #=>"code" | |
p new_boggle_board.get_col(2) #=>["a", "d", "l", "k"] | |
p new_boggle_board.get_row(1) #=> ["i", "o", "d", "t"] | |
p new_boggle_board.get_diagonal(1,2) #=> ["d", "r"] | |
p new_boggle_board.shake_board #=> | |
# z l k h | |
# u a l i | |
# i f c h | |
# y h k l | |
boggle = Board_games.new #=> Board ready, if you want to mix the letters and start with a new board, just shake | |
# i m v l | |
# v k y c | |
# s c e u | |
# r x k h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment