Skip to content

[Shadows of the Knight] Solution for shadows of the Knight task#10

Open
Slavunderkind wants to merge 2 commits intomasterfrom
shadows_of_the_knight
Open

[Shadows of the Knight] Solution for shadows of the Knight task#10
Slavunderkind wants to merge 2 commits intomasterfrom
shadows_of_the_knight

Conversation

@Slavunderkind
Copy link
Copy Markdown
Owner

No description provided.

@Slavunderkind Slavunderkind requested review from a user and vasil-gochev January 19, 2017 11:33
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)
Copy link
Copy Markdown

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.

@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]]
Copy link
Copy Markdown

@ghost ghost Jan 20, 2017

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]]

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')
Copy link
Copy Markdown

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
  ...

Copy link
Copy Markdown
Owner Author

@Slavunderkind Slavunderkind Jan 23, 2017

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant