Skip to content

Instantly share code, notes, and snippets.

@yclim95
Forked from nextacademy-private/boggle_board.rb
Last active January 14, 2016 15:48
Show Gist options
  • Save yclim95/cd80a58daa838e1f60b1 to your computer and use it in GitHub Desktop.
Save yclim95/cd80a58daa838e1f60b1 to your computer and use it in GitHub Desktop.
class BoggleBoard
LETTERS = ('A'...'Z').to_a
def initialize(size=4, length=16)
@size=size
@length=length
@board=Array.new(@size) { Array.new(@size) }
end
def shake!
boogle_board = Array.new(@length)
boogle_board.each_with_index{|item,index|
words=LETTERS.sample
words = "Qu" if words == "Q" #Subsitute Qu instead of Q
boogle_board[index] = words #Assign the RANDOM CHAR for 16cells
}
@size.times{|index|
@board[index]= boogle_board.shift(@size) #extract the 1st 4 elements to form 4 columns in a row (4 X 4)
}
end
# Print out the table as STRING
def to_s
board_str = ""
@board.each_with_index {|row,index|
row.each_with_index{|char,index|
board_str << char
board_str << " " unless char == "Qu"
board_str << " "
}
board_str << "\n"
}
board_str
end
end # END Of Class
#Objectives 1 Stupid Boogle Board
board = BoggleBoard.new
board.shake!
puts board
puts
# Objectives 2 Smart(er) Boogle BOard
boogle_board = BoggleBoard.new
boogle_board.shake!
puts boogle_board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment