Conversation
app/models/game.rb
Outdated
| def check? | ||
| # code | ||
| white_king = King.find_by(color: 'white') | ||
| black_king = King.find_by(color: 'black') |
There was a problem hiding this comment.
This code is looking at all of the King's in our database.
We only want the Kings inside of this game.
I think you can do... pieces.kings.find_by... (try it in a console to see) 🎉
There was a problem hiding this comment.
pieces.kings.find_by didn't seem to work, because it said kings was not a valid method.
However, I changed it to the following:
white_king = King.find_by(color: 'white', game: self)
black_king = King.find_by(color: 'black', game: self)
Please let me know if this is okay.
| queens.each do |queen| | ||
| return true if queen.valid_move?(white_king.position_x, white_king.position_y) && queen.color != white_king.color | ||
| return true if queen.valid_move?(black_king.position_x, black_king.position_y) && queen.color != black_king.color | ||
| end |
There was a problem hiding this comment.
Notice how each of these blocks are the same.
Could dry this up by doing.
pieces.where.not(type: 'King').each do |piece|
##...
end I'm not sure if this is valid Rails 5 code: pieces.where.not(type: 'King')
Give it a try in a console 😄
There was a problem hiding this comment.
pieces.where.not(type: 'King') did seem to work. I updated this part of the check? method. Please let us know if this is right.
There was a problem hiding this comment.
Rubocop changed each to find_each in pieces.where.not(type: 'King').find_each do |piece|.
There was a problem hiding this comment.
Changing this to find_each broke the check? method. I changed this back to each by doing the following, and it seemed to work:
non_king_pieces = pieces.where.not(type: 'King')
non_king_pieces.each do |piece|
spec/models/game_spec.rb
Outdated
| it 'returns true if the king is in check by knight' do | ||
| game = Game.create | ||
| game.populate_board! | ||
| king = King.find_by(color: 'black') |
There was a problem hiding this comment.
Same here, this finds ALL kings in the database. We want only kings within this game.
There was a problem hiding this comment.
Added game: game into the arguments. Tested with pry, and this seemed to work. Please let us know if this looks good.
|
All the RSpec tests for
|
|
cc @ddunbar88 - is #25 coming together? It's needed for check to be completed. |
This is a rebased version of the
checkPR, which includes the x and y axis fix.