ruby 使用类从Boggle Board创建检索Boggle字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 使用类从Boggle Board创建检索Boggle字相关的知识,希望对你有一定的参考价值。

# INITIAL CODE
class BoggleBoard
	attr_reader :board_type

	def initialize(board_type)
		@board_type = board_type
	end

	def create_word(*coords)
  	coords.map { |coord| @board_type[coord.first][coord.last]}.join("")
	end

	def get_row(row)	#=> method returns a specific row of the boggle board
		return "That row doesn't exist in the Boggle Board. Try again." if row > 3 || row < 0
		return @board_type[row].join
	end

	def get_col(col)
		x = 0
		col_array = Array.new(0)
		4.times do |x|
			col_array.push(@board_type[x][col])
			x += 1
		end
		return "This column doesn't exist in the Boggle Board.  Try again." if col_array.include?(nil) == true
		return col_array.join
	end

	def get_diag(starting_coord, end_coord)
		x = (end_coord[0] - starting_coord[0]).abs
		y = (end_coord[1] - starting_coord[1]).abs

		if x == 0 || y == 0		#=> The coordinates are in the same row or column
			return "There is no possible diagonal connection."
		
		elsif	x == y    #=> There is a diagonal combination
		  puts "There is diagonal combination."
		  diag_array = Array.new(0).push(starting_coord).push(end_coord)
		  mid_array = Array.new(0)
		  if x == 1  #=> Returns a 2 word diagonal combo
		  	diag_array.map  {|coord| @board_type[coord.first][coord.last]}.join("")
		  elsif x == 2 #=> Returns a 3 letter word diagonal combo
		  	diag_array.flatten.each do |x|
		  		if x == 0
		  			x += 1
		  			mid_array.push(x)
		  		else
		  			mid_array.push(x)
		  		end
		  	end
		  	mid_coord = [(mid_array[2] - mid_array[0]).abs, (mid_array[3] - mid_array[1]).abs]
		  	diag_array.insert(1, mid_coord).map {|coord| @board_type[coord.first][coord.last]}.join("")		  	
			else # Returns a 4 letter word diagonal combination
# NEED TO WORK ON THIS.  HAVING A TOUGH TIME.				
#				until diag_array[2] == diag_array[0] || diag_array[3] == diag_array[1] do
#					diag_array.flatten.each do |x|
#		  			if x == 0
#		  				x += 1
#		  				mid_array.push(x)
#		  			else
#		  				mid_array.push(x)
#		  			end
#		  		end
#		  		mid_coord = [(mid_array[2] - mid_array[0]).abs, (mid_array[3] - mid_array[1]).abs]
#		  		diag_array.insert(1, mid_coord)
#		  	end	
#				diag_array.map {|coord| @board_type[coord.first][coord.last]}.join("")
			end
		else
			return "No Diagonal combination is possible."
		end
	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)

 
# implement tests for each of the methods here:
 
puts boggle_board.create_word([0,1], [0,2], [1,2])  #=> returns "rad"
puts boggle_board.create_word([3,0], [3,1], [3,2], [3,3]) #=> returns "take"
puts boggle_board.create_word([0,1], [1,1], [2,1], [3,2], [3,3], [2,3])	#=> returns "rocker"
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3])	#=> returns "code"
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2])	#=> NEW TEST: returns "dock"

puts boggle_board.get_row(6) #=> returns Error message
puts boggle_board.get_row(0)  #=> returns "brae"
puts boggle_board.get_row(1)	#=> returns "iodt"
puts boggle_board.get_row(2)	#=> returns "eclr"
puts boggle_board.get_row(3) #=> returns "take"


puts boggle_board.get_col(9)	#=> returns Error statement
puts boggle_board.get_col(0)	#=> returns "biet"
puts boggle_board.get_col(1)	#=> returns "roca"
puts boggle_board.get_col(2)	#=> returns "adlk"
puts boggle_board.get_col(3)	#=> returns "etre"

puts boggle_board.get_diag([0,0], [3,3])
puts boggle_board.get_diag([0,3], [3,0])
puts boggle_board.get_diag([3,3], [0,0])
puts boggle_board.get_diag([3,0], [0,3])
puts boggle_board.get_diag([0,0], [3,2])    #=> returns "No diagonal is possible"
puts boggle_board.get_diag([0,0], [3,3])
puts boggle_board.get_diag([0,3], [2,1])


# create driver test code to retrieve a value at a coordinate here:

puts "Driver code tests:"
puts boggle_board.create_word([3,2]) == "k"
puts boggle_board.get_diag([1,1], [2,2]) == "ol"

# REFLECTION
# The main implementation difference between procedural programming and oop in this program
# is how to call the methods.  In the procedural programming, each method took a 'board' as 
# an argument.  In OOP, the board argument isn't needed, because the methods are defined within
# the BoggleBoard class.  Now, if we wanted to use these methods for something other than a BoggleBoard
# we could utilize inheritance to pass these methods to another class.  When calling the method in OOP,
# we start with our BoggleBoard objects we've created, and call the methods on these objects.
# In terms of OOP advantages, it is beginning to become clear that OOP more closely models the
# way real life would be organized.  I like procedural programming, but it seems more abstract and
# honestly requires more initial planning of your code and structure.  The main advantage I can see
# right now from creating this program is that OOP seems much more scalable.  If you have a large project,
# or want to extend these methods to many different objects, it seems easier to do than with procedural.
# Aesthetically, I prefer organizing with classes, rather than methods everywhere with more detailed 
# argument structures.

以上是关于ruby 使用类从Boggle Board创建检索Boggle字的主要内容,如果未能解决你的问题,请参考以下文章

ruby 嵌套数组的解决方案为Boggle Board建模

ruby Boggle Solver挑战赛

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单位2周1boggle班级挑战