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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 阶段0单位2周1boggle班级挑战相关的知识,希望对你有一定的参考价值。

class BoggleBoard
	attr_reader :dice_grid

	def initialize(dice_grid)
		@dice_grid = dice_grid
	end

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

	def get_row(row)
		@dice_grid[row]

	end

	def get_col(col)
		@dice_grid.transpose[col]
	end

	def get_diagonal(coordinate1,coordinate2)
		coord1a = coordinate1[0]
		coord1b = coordinate1[1]
		coord2a = coordinate2[0]
		coord2b = coordinate2[1]

		first = coord2a - coord1a
		second = coord2b = coord1b

		if (first == -1 && second == -1 || second == -1 || second == 0) || (first == 1 && second == -1 || second == 1)
			puts "The coordinates #{first,second} are diagonal in the array"
		else
			puts "These coordinates in the array are not diagonal"
		end
	end
 end


dice_grid = [["b", "r", "a", "e"],
 
            ["i", "o", "d", "t"],
                
            ["e", "c", "l", "r"],
                
            ["t", "a", "k", "e"]]
            
            
# Section 1 - Instantiate New Board Object
boggle_board = BoggleBoard.new(dice_grid) 			#dice_grid is held as a variable in the Initialze Method

# Section 2 - Implement your methods
# implement tests for each of the methods here:
boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock"
p boggle_board.get_row(0) #=> ["b","r","a","e"]
p boggle_board.get_row(1) #=> ["i","o","d","t"]
p boggle_board.get_row(2) #=> ["e","c","l","r"]
p boggle_board.get_row(3) #=> ["t','a','k','e'] 	# real word "take"
p boggle_board.get_col(0) #=> ["b","i","e","t"]
p boggle_board.get_col(1) #=> ["r","o","c","a"]
p boggle_board.get_col(2) #=> ["a","d","l","k"]
p boggle_board.get_col(3) #=> ["e","t","r","e"]

# Section 3 - create driver test code to retrieve a value at a coordinate here:
p dice_grid[3][2]

# Section 4 - Bonus Question
p boggle_board.get_diagonal([1,3],[2,1]) 			# Not Diagonal
p boggle_board.get_diagonal([0,0],[1,1])			# Diagonal

# Section 5 - Review and Reflect
=begin
Definately have had to wrap my head around using Classes this week.  Its one thing to write a bunch of methods and quite another
thing to create a class and use them in the class.  Took me a bit but I think Im getting it.  Slowly.
One of the benefits of using OOP is that it makes it easier to modify existing code.  It' broken up into smaller chunks. It also 
allows you to initialize single instances of objects.  Pretty cool.  In the beginning I couldnt see how we would be making
large programs but now I understand that its just one small step at a time.  Makes things less daunting.









以上是关于ruby 阶段0单位2周1boggle班级挑战的主要内容,如果未能解决你的问题,请参考以下文章

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

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

ruby 阶段0单元2周2

ruby 阶段0单元2周2

项目复审——Alpha阶段

每天学点Python案例四:52周存钱挑战