Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,21 @@ def populate_board!
y2 = 7
color = 0
end

end

def in_check?(color)
king = pieces.find_by(type: 'king', color: color)
opponents_pieces = pieces(!color)

opponents_pieces.each do |piece|
if piece.valid_move?(king.x_position, king.y_position)
@checking_piece = piece
return true
else
return false
end
end
end

end
2 changes: 1 addition & 1 deletion app/views/shared/_chessboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<% 1.upto(8) do |col| %>
<td class= "<%= board_squares(row) %>">
<%= link_to piece_path(id: @select_pc.id, x_position: col, y_position: row), method: :put do %>
<div class="squares">
<div class="squares draggable">
<% @pieces.detect do |piece| %>
<% if piece.x_position == col && piece.y_position == row %>
<%= link_to piece_color(piece), piece_path(piece.id), :class => hilite(piece) %>
Expand Down
16 changes: 16 additions & 0 deletions test/models/in_check_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'

class GameInCheckTest < ActiveSupport::TestCase
test "game in check state" do
g = Game.create
expected = true

p = g.pieces.find_by(x_position: 4, y_position: 2, type: 'pawn', color: "black")
k = g.pieces.find_by(x_position: 5, y_position: 1, color: "white", type: 'King')

p.valid_move?(king.x_position: 5, king.y_position: 1)

assert_equal true, g.in_check?(1)
end

end