-
Notifications
You must be signed in to change notification settings - Fork 1
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
attempted to write capture logic #13
Conversation
app/models/piece.rb
Outdated
end | ||
end | ||
return nil | ||
end |
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.
Clever! 😄
This code is iterating through every piece and checking the coordinates. Totally works.
To make this more efficient, can use ActiveRecord/SQL:
game.pieces.find_by(x_position: x, y_position: y)
Notice game.pieces
. This ensures we pull pieces from the current game (it knows the current game because game
is an association on Piece
and we are currently inside of a Piece
).
find_by
will find the record or return nil.
app/models/piece.rb
Outdated
@@ -1,6 +1,43 @@ | |||
class Piece < ApplicationRecord | |||
belongs_to :game | |||
|
|||
def move_to!(new_x, new_y, white_turn) |
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.
Instead of passing white_turn
.
If you call self.is_white?
, you'll be able to see if the current piece that is being moved is white. Doing that will eliminate needing to pass in white_turn
Made the changes you suggested, but rubocop is still saying that
I don't understand what is wrong with using an if statement in this case. Can I just replace the if with unless and change each condition to its negation? (For example, unless (occupying_piece.is_white && self.is_white?) || (!occupying_piece.is_white && !self.is_white?) I looked at some pages regarding guard clauses, but can't see how it would be applied in my case. rubocop/rubocop#2903 |
I fixed the issue with the guard clause and rubocop, just need to clean up the last few rubocop errors. Does anyone see an issue with the code I have so far? |
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.
Looks pretty great to me, Dennis. Have you made your updates on the Rubocop portion?
Okay, looks like all the tests pass. |
app/models/piece.rb
Outdated
return piece if piece.x_position == x && piece.y_position == y | ||
end | ||
nil | ||
end |
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.
can you update this to use find_by
(see previous note)
…o dennis Conflicts: app/controllers/games_controller.rb
Added Melsen's strf format for date game was created line 7
I tried creating two pieces for the same game in rails console and having one piece move into the square held by another piece, but rails tells me that it cannot see my method move_to! I think it is problem with way my code is organized. Can you take a look and suggest how I might rearrange?