-
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
663c31b
attempted to write capture logic
dennisgit42 9cfb725
Fixed piece.rb syntax error, started writing tests for move_to! method
dennisgit42 f75f125
Added rspec tests and refactor code for capture logic
dennisgit42 f869a93
test if rubocop will accept guard clause in piece.rb line 7
dennisgit42 78b6104
second attempt at writing guard clause in piece.rb line 7
dennisgit42 bc30b66
third attempt at writing guard clause at piece.rb line 7
dennisgit42 66b84b2
attempt at writing guard clause at line 10
dennisgit42 f8e3797
edits for all rubocop errors in piece.rb
dennisgit42 04800bd
fixed remaining rubocop errors
dennisgit42 78edfc6
Added find_by method in line 26 of piece.rb
dennisgit42 91e6558
Merge branch 'master' of github.com:Infinite-Loops-Firehose/chess int…
dennisgit42 ee170e8
add find_by to line 26 of piece.rb
dennisgit42 34a3a01
Update index.html.erb
dennisgit42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
class Knight < Piece; end | ||
class Knight < Piece | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,38 @@ | ||
class Piece < ApplicationRecord | ||
belongs_to :game | ||
|
||
def move_to!(new_x, new_y) | ||
unless square_occupied?(new_x, new_y) | ||
update_attributes(x_position: new_x, y_position: new_y) | ||
return | ||
end | ||
occupying_piece = get_piece_at_coor(new_x, new_y) | ||
unless (occupying_piece.is_white && is_white?) || (!occupying_piece.is_white && !is_white?) | ||
capture_piece(occupying_piece) | ||
update_attributes(x_position: new_x, y_position: new_y) | ||
return | ||
end | ||
raise ArgumentError, 'That is an invalid move. Cannot capture your own piece.' | ||
end | ||
|
||
def square_occupied?(new_x, new_y) | ||
piece = game.pieces.find_by(x_position: new_x, y_position: new_y) | ||
return false if piece.nil? | ||
return true | ||
end | ||
|
||
def get_piece_at_coor(x, y) | ||
Piece.all.find_each do |piece| | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. can you update this to use |
||
|
||
def capture_piece(piece_captured) | ||
piece_captured.update(x_position: nil, y_position: nil) | ||
end | ||
end | ||
|
||
PAWN = 'Pawn'.freeze | ||
ROOK = 'Rook'.freeze | ||
KNIGHT = 'Knight'.freeze | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,75 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Piece, type: :model do | ||
describe "square_occupied? method" do | ||
it "returns true if the square your piece is moving to is occupied by another piece" do | ||
game1 = FactoryGirl.create(:game) | ||
piece1 = Piece.create(game_id: game1.id, is_white: false, type: PAWN, x_position: 1, y_position: 2) | ||
occupied = piece1.square_occupied?(piece1.x_position, piece1.y_position) | ||
expect(occupied).to eq(true) | ||
end | ||
it "returns false if the square your piece is moving to is not occupied" do | ||
game1 = FactoryGirl.create(:game) | ||
piece1 = Piece.create(game_id: game1.id, is_white: false, type: QUEEN, x_position: 1, y_position: 2) | ||
occupied = piece1.square_occupied?(2, piece1.y_position) | ||
expect(occupied).to eq(false) | ||
end | ||
end | ||
|
||
describe "get_piece_at_coor method" do | ||
it "returns the piece object at given square" do | ||
game1 = FactoryGirl.create(:game) | ||
piece1 = Piece.create(game_id: game1.id, is_white: false, type: PAWN, x_position: 1, y_position: 2) | ||
piece_found = piece1.get_piece_at_coor(1,2) | ||
expect(piece_found).to eq(piece1) | ||
end | ||
it "returns nil if there is no piece at specified square" do | ||
game1 = FactoryGirl.create(:game) | ||
piece1 = Piece.create(game_id: game1.id, is_white: false, type: KNIGHT, x_position: 7, y_position: 1) | ||
piece_found = piece1.get_piece_at_coor(1,1) | ||
expect(piece_found).to eq(nil) | ||
end | ||
end | ||
|
||
describe "capture_piece method" do | ||
it "sets piece x and y coor to nil if piece can be captured" do | ||
game1 = FactoryGirl.create(:game) | ||
piece1 = Piece.create(game_id: game1.id, is_white: false, type: KNIGHT, x_position: 7, y_position: 1) | ||
piece1.capture_piece(piece1) | ||
expect(piece1.x_position).to eq(nil) | ||
expect(piece1.y_position).to eq(nil) | ||
end | ||
end | ||
|
||
describe "move_to! method" do | ||
it "Changes x and y coor of moving piece to x and y of destination square if that square is empty" do | ||
game1 = FactoryGirl.create(:game) | ||
piece1 = Piece.create(game_id: game1.id, is_white: false, type: KING, x_position: 2, y_position: 4) | ||
piece1.move_to!(3,4) | ||
expect(piece1.x_position).to eq(3) | ||
expect(piece1.y_position).to eq(4) | ||
end | ||
it "updates x and y of moving piece to new square and captures enemy piece at that square" do | ||
game1 = FactoryGirl.create(:game) | ||
black_king = Piece.create(game_id: game1.id, is_white: false, type: KING, x_position: 2, y_position: 4) | ||
white_queen = Piece.create(game_id: game1.id, is_white: true, type: QUEEN, x_position: 1, y_position: 4) | ||
black_king.move_to!(1,4) | ||
expect(black_king.x_position).to eq(1) | ||
expect(black_king.y_position).to eq(4) | ||
white_queen.reload | ||
expect(white_queen.x_position).to eq(nil) | ||
expect(white_queen.y_position).to eq(nil) | ||
end | ||
it "raises argument error if player tries to capture or move into a friendly piece" do | ||
game1 = FactoryGirl.create(:game) | ||
black_pawn = game1.pieces.create(game_id: game1.id, is_white: false, type: PAWN, x_position: 2, y_position: 5) | ||
black_knight = game1.pieces.create(game_id: game1.id, is_white: false, type: KNIGHT, x_position: 3, y_position: 6) | ||
expect{ black_pawn.move_to!(3, 6) }.to raise_error(ArgumentError) | ||
black_knight.reload | ||
expect(black_pawn.x_position).to eq(2) | ||
expect(black_pawn.y_position).to eq(5) | ||
expect(black_knight.x_position).to eq(3) | ||
expect(black_knight.y_position).to eq(6) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 becausegame
is an association onPiece
and we are currently inside of aPiece
).find_by
will find the record or return nil.