[Shadows of the Knight] Solution for shadows of the Knight task#10
[Shadows of the Knight] Solution for shadows of the Knight task#10Slavunderkind wants to merge 2 commits intomasterfrom
Conversation
Training/Medium/knight.rb
Outdated
| def initialize | ||
| @width, @height = gets.split(' ').collect(&:to_i) | ||
| @jumps_number = gets.to_i # maximum number of turns before game over. | ||
| @x, @y = gets.split(' ').collect(&:to_i) |
There was a problem hiding this comment.
split defaults to whitespace anyway
@x, @y = gets.split.collect(&:to_i)Same for line 8.
Training/Medium/knight.rb
Outdated
| @width, @height = gets.split(' ').collect(&:to_i) | ||
| @jumps_number = gets.to_i # maximum number of turns before game over. | ||
| @x, @y = gets.split(' ').collect(&:to_i) | ||
| @search_area = [[0, 0], [@width - 1, @height - 1]] |
There was a problem hiding this comment.
You've got attr_accessors, so you can use them:
@search_area = [[0, 0], [width - 1, height - 1]]
Training/Medium/knight.rb
Outdated
| search_area[1][1] = y - 1 if direction.include?('U') | ||
| search_area[0][1] = y + 1 if direction.include?('D') | ||
| search_area[1][0] = x - 1 if direction.include?('L') | ||
| search_area[0][0] = x + 1 if direction.include?('R') |
There was a problem hiding this comment.
You can refactor this with a case statement:
case
when direction.include?('U')
search_area[1][1] = y - 1
when direction.include?('D')
...Another thing that could be added is to have constants for each direction:
UP = 'U'
DOWN = 'D'
LEFT = 'L'
RIGHT = 'R'Then the case will be very descriptive and you can change the letters for directions easily:
case
when direction.include? UP
search_area[1][1] = y - 1
when direction.include? DOWN
...There was a problem hiding this comment.
@evgenispasov-which I was thinking about case statement at the beginning, but I choose not to use it because I have to cover 8 cases for every position
U (Up) UR (Up-Right) R (Right) DR (Down-Right) D (Down) DL (Down-Left) L (Left) UL (Up-Left)
Is it so bad in this way with if statements ?
Add constants for each direction
No description provided.