-
Notifications
You must be signed in to change notification settings - Fork 0
[Shadows of the Knight] Solution for shadows of the Knight task #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.