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

Determine Check #41

Merged
merged 7 commits into from Dec 4, 2015
Copy path View file
@@ -59,6 +59,11 @@ group :development, :test do

# Use FactoryGirl
gem 'factory_girl_rails'

# Use Rails Performance Test
gem 'rails-perftest'

gem 'ruby-prof'
end

group :development do
Copy path View file
@@ -144,6 +144,7 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
rails-perftest (0.0.6)
railties (4.2.4)
actionpack (= 4.2.4)
activesupport (= 4.2.4)
@@ -160,6 +161,7 @@ GEM
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
ruby-prof (0.15.8)
ruby-progressbar (1.7.5)
sass (3.4.19)
sass-rails (5.0.4)
@@ -215,11 +217,16 @@ DEPENDENCIES
pg
puma
rails (= 4.2.4)
rails-perftest
rubocop
ruby-prof
sass-rails (>= 3.2)
sdoc (~> 0.4.0)
simple_form
spring
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)

BUNDLED WITH
1.10.6
Copy path View file
@@ -16,6 +16,24 @@ def whos_turn?
return black_player_id if turn_number.odd?
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def determine_check
if turn_number.even?
opponent_pieces = pieces.where(user_id: black_player_id)
king = pieces.find_by(user_id: white_player_id, type: "King")
opponent_pieces.each do |piece|
return true if piece.valid_move?(king.row_position, king.col_position)
end
else
opponent_pieces = pieces.where(user_id: white_player_id)
king = pieces.find_by(user_id: black_player_id, type: "King")
opponent_pieces.each do |piece|
return true if piece.valid_move?(king.row_position, king.col_position)
end
end
false
end

private

def init_pawn
Copy path View file
@@ -52,4 +52,68 @@ def setup

assert_equal expected, actual
end

test "game should be in check (bishop capture king)" do
@game = Game.create(name: "Check Game", white_player_id: @user1.id, black_player_id: @user2.id, turn_number: 3)
@white_bishop = @game.pieces.create(type: "Bishop", col_position: 4, row_position: 5, user_id: @user1.id)
@white_king = @game.pieces.create(type: "King", col_position: 4, row_position: 0, user_id: @user1.id)
@black_king = @game.pieces.create(type: "King", col_position: 3, row_position: 0, user_id: @user2.id)

@white_bishop.move_to!(3, 6)

expected = true
actual = @game.determine_check
assert_equal expected, actual
end

test "game should be in check (queen capture king)" do
@game = Game.create(name: "Check Game", white_player_id: @user1.id, black_player_id: @user2.id, turn_number: 3)
@white_queen = @game.pieces.create(type: "Queen", col_position: 3, row_position: 7, user_id: @user1.id)
@white_king = @game.pieces.create(type: "King", col_position: 4, row_position: 0, user_id: @user1.id)
@black_king = @game.pieces.create(type: "King", col_position: 3, row_position: 0, user_id: @user2.id)

@white_queen.move_to!(5, 3)

expected = true
actual = @game.determine_check
assert_equal expected, actual
end

test "game should be in check (queen capture king 2)" do
@game = Game.create(name: "Check Game", white_player_id: @user1.id, black_player_id: @user2.id, turn_number: 3)
@white_queen = @game.pieces.create(type: "Queen", col_position: 2, row_position: 0, user_id: @user1.id)
@white_bishop = @game.pieces.create(type: "Bishop", col_position: 2, row_position: 1, user_id: @user1.id)
@black_king = @game.pieces.create(type: "King", col_position: 2, row_position: 3, user_id: @user2.id)

@white_bishop.move_to!(4, 3)

expected = true
assert_equal expected, @game.determine_check
end

test "game should not be in check (pawn blocking queen)" do
@game = Game.create(name: "Check Game", white_player_id: @user1.id, black_player_id: @user2.id, turn_number: 3)
@white_queen = @game.pieces.create(type: "Queen", col_position: 3, row_position: 7, user_id: @user1.id)
@black_king = @game.pieces.create(type: "King", col_position: 3, row_position: 0, user_id: @user2.id)
@black_pawn = @game.pieces.create(type: "Pawn", col_position: 3, row_position: 1, user_id: @user2.id)

@white_queen.move_to!(5, 3)

expected = false
actual = @game.determine_check
assert_equal expected, actual
end

test "game should not be in check (pawn blocking bishop)" do
@game = Game.create(name: "Check Game", white_player_id: @user1.id, black_player_id: @user2.id, turn_number: 3)
@white_bishop = @game.pieces.create(type: "Bishop", col_position: 4, row_position: 5, user_id: @user1.id)
@black_pawn = @game.pieces.create(type: "Pawn", col_position: 4, row_position: 1, user_id: @user2.id)
@black_king = @game.pieces.create(type: "King", col_position: 3, row_position: 0, user_id: @user2.id)

@white_bishop.move_to!(3, 6)

expected = false
actual = @game.determine_check
assert_equal expected, actual
end
end
Copy path View file
@@ -0,0 +1,15 @@
require 'test_helper'
require 'rails/performance_test_help'

class GamePerformanceTest < ActionDispatch::PerformanceTest
# Refer to the documentation for all available options
self.profile_options = { runs: 10, metrics: [:wall_time, :cpu_time],
output: 'tmp/performance', formats: [:flat] }
# rubocop:disable Metrics/LineLength
# def setup
# @user1 = FactoryGirl.create(:user)
# @user2 = FactoryGirl.create(:user)
# @g = Game.create(name: "New Game", white_player_id: @user1.id, black_player_id: @user2.id, turn_number: 1)
# @g.populate_board!
# end
end
ProTip! Use n and p to navigate between commits in a pull request.