From 6c9d31ccfe133f3bda154ef204b05347b356445b Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Sat, 29 Aug 2015 01:15:34 +0000 Subject: [PATCH 1/4] add in_check? method - 1 passing test --- app/models/game.rb | 16 ++++++++++++++++ app/models/piece.rb | 2 +- test/models/in_check_test.rb | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/models/in_check_test.rb diff --git a/app/models/game.rb b/app/models/game.rb index 454d99c..0f875c8 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -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 diff --git a/app/models/piece.rb b/app/models/piece.rb index e30cebc..61f5498 100644 --- a/app/models/piece.rb +++ b/app/models/piece.rb @@ -4,7 +4,7 @@ class Piece < ActiveRecord::Base has_many :types def self.types - %w(Pawn Rook Knight Bishop Queen King) + %w(Pawn Rook Knight Bishop Queen f) end # This checks the database for a potential obstacle on a single location diff --git a/test/models/in_check_test.rb b/test/models/in_check_test.rb new file mode 100644 index 0000000..4a10cdb --- /dev/null +++ b/test/models/in_check_test.rb @@ -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 From 958d6c5760eaeb5cbd683af19e4029b77fcf4d45 Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Sat, 29 Aug 2015 01:23:11 +0000 Subject: [PATCH 2/4] add in_check? method - 1 passing test - correct piece.rb --- app/models/piece.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/piece.rb b/app/models/piece.rb index 61f5498..e30cebc 100644 --- a/app/models/piece.rb +++ b/app/models/piece.rb @@ -4,7 +4,7 @@ class Piece < ActiveRecord::Base has_many :types def self.types - %w(Pawn Rook Knight Bishop Queen f) + %w(Pawn Rook Knight Bishop Queen King) end # This checks the database for a potential obstacle on a single location From fd99622c9284b9c72fab97440a4306f9e47bdbcf Mon Sep 17 00:00:00 2001 From: vagrant Date: Sat, 29 Aug 2015 16:31:21 +0000 Subject: [PATCH 3/4] Working copy of prevent move to in_check? --- app/controllers/pieces_controller.rb | 22 ++++++++++++++++++---- app/models/game.rb | 7 +++---- app/models/piece.rb | 4 ++++ test/models/in_check_test.rb | 11 +++++++---- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/app/controllers/pieces_controller.rb b/app/controllers/pieces_controller.rb index f0e88bd..bd37f16 100644 --- a/app/controllers/pieces_controller.rb +++ b/app/controllers/pieces_controller.rb @@ -3,26 +3,40 @@ class PiecesController < ApplicationController before_action :only => :update do validate_move(:x_position, :y_position) end + before_action :only => :update do + state_of_game(:player_turn) + end def show @pieces = select_pc.game.pieces end def update - row = params[:y_position].to_i col = params[:x_position].to_i - @select_pc.move_to!(col, row) + select_pc.move_to!(col, row) redirect_to game_path(select_pc.game.id) end private + def state_of_game(color) + color == 0 ? your_color = "White" : your_color = "Black" + if select_pc.game.in_check?(color) + flash[:alert] = "#{your_color}, you are in check!" + end + end + def validate_move(x_position, y_position) row = params[:y_position].to_i col = params[:x_position].to_i - if !select_pc.valid_move?(col, row) || select_pc.nil_move?(col, row) - flash[:alert] = "That move is not allowed! Please choose your piece and try again." + if [!select_pc.valid_move?(col, row), select_pc.nil_move?(col, row), + select_pc.prevent_check?].any? + if select_pc.prevent_check? + flash[:alert] = "You cannot put yourself in check" + else + flash[:alert] = "That move is not allowed! Please choose your piece and try again." + end redirect_to game_path(select_pc.game.id) end end diff --git a/app/models/game.rb b/app/models/game.rb index 0f875c8..dd0ddbd 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -73,15 +73,14 @@ def populate_board! end def in_check?(color) - king = pieces.find_by(type: 'king', color: color) - opponents_pieces = pieces(!color) + c = color.to_s.to_i + king = pieces.find_by(type: 'King', color: c) + opponents_pieces = pieces.where.not(color: c) 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 diff --git a/app/models/piece.rb b/app/models/piece.rb index e30cebc..dfac028 100644 --- a/app/models/piece.rb +++ b/app/models/piece.rb @@ -106,6 +106,10 @@ def move_to!(new_x, new_y) end end + def prevent_check? + return true if game.in_check?(color) + end + def friendly_piece?(new_x, new_y) obstacle_piece = Piece.find_by( :x_position => new_x, :y_position => new_y, :active => 1) diff --git a/test/models/in_check_test.rb b/test/models/in_check_test.rb index 4a10cdb..21f0789 100644 --- a/test/models/in_check_test.rb +++ b/test/models/in_check_test.rb @@ -5,12 +5,15 @@ class GameInCheckTest < ActiveSupport::TestCase 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') + black_p = g.pieces.find_by(:x_position => 4, :y_position => 7, :type => 'Pawn', color: 0) + deactivate_white_pawn = g.pieces.find_by(:x_position => 4, :y_position => 2).update_attributes(:active => 0) + black_p.update_attributes(:y_position => 2) - p.valid_move?(king.x_position: 5, king.y_position: 1) + white_k = g.pieces.find_by(:x_position => 5, :y_position => 1, :color => 1, :type => 'King') + check_color = white_k.color + actual = g.in_check?(check_color) - assert_equal true, g.in_check?(1) + assert_equal expected, actual end end From 043f26fb44ef66cc76a27f74e55c3d47e13c9f18 Mon Sep 17 00:00:00 2001 From: vagrant Date: Fri, 4 Sep 2015 02:08:46 +0000 Subject: [PATCH 4/4] Minor corrections. Started show in_check to player --- app/controllers/pieces_controller.rb | 14 +------------- app/models/game.rb | 17 ++++++++++------- app/models/pawn.rb | 23 ++++++++++++----------- app/models/piece.rb | 4 ---- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/app/controllers/pieces_controller.rb b/app/controllers/pieces_controller.rb index 3b448a3..c2b061a 100644 --- a/app/controllers/pieces_controller.rb +++ b/app/controllers/pieces_controller.rb @@ -4,9 +4,6 @@ class PiecesController < ApplicationController before_action :only => :update do validate_move(:x_position, :y_position) end - #after_action :only => :update do - # state_of_game(:player_turn) - #end def show @pieces = select_pc.game.pieces @@ -21,16 +18,11 @@ def update private - def state_of_game(color) - color = (color - 1).abs - if select_pc.game.in_check?(your_color) - flash[:alert] = "#{your_color}, you are in check!" - end - end def validate_move(x_position, y_position) row = params[:y_position].to_i col = params[:x_position].to_i + #binding.pry color = select_pc.color if invalid_move?(col, row) flash[:alert] = "That move is not allowed! Please choose your piece and try again." @@ -51,10 +43,6 @@ def put_self_in_check?(color, col, row) select_pc.game.checking_piece?(color, select_pc.id, col, row) end - def your_color - color == 0 ? your_color = "White" : your_color = "Black" - end - def select_pc @select_pc = Piece.find(params[:id]) end diff --git a/app/models/game.rb b/app/models/game.rb index 16c40f8..df2f3f9 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,7 +1,11 @@ +require 'pry' class Game < ActiveRecord::Base belongs_to :user has_many :pieces + #before_action only: :show do + # :in_check(:color) + #end after_create :populate_board! @@ -73,14 +77,13 @@ def populate_board! end - #def in_check?(color) - # color = (color - 1).abs - #checking_piece?(color) - #end + def in_check?(color) + checking_piece?(color) + end def checking_piece?(color, piece_id, col, row) opponents_pieces = pieces.where.not(color: color, active: 0) - check_king(piece_id, color, col, row) + check_king(color, piece_id, col, row) opponents_pieces.each do |piece| piece.valid_move?(*@king_position) ? (return true) : false end @@ -88,8 +91,8 @@ def checking_piece?(color, piece_id, col, row) end def check_king(color, piece_id, col, row) - @king = Piece.find_by(type: 'King', color: color) - piece_moving = Piece.find_by(id: piece_id) + @king = pieces.find_by(type: 'King', color: color) + piece_moving = pieces.find_by(id: piece_id) piece_moving == @king ? (@king_position = col, row) : (@king_position = @king.x_position, @king.y_position) end end diff --git a/app/models/pawn.rb b/app/models/pawn.rb index c528478..3396836 100644 --- a/app/models/pawn.rb +++ b/app/models/pawn.rb @@ -1,9 +1,10 @@ +require 'pry' class Pawn < Piece def valid_move?(new_x, new_y) - [move_number_allowed?(new_y), - no_vertical_obstacle?(new_x, new_y), - not_a_backwards_move?(new_y), + [move_number_allowed?(new_y), + no_vertical_obstacle?(new_x, new_y), + not_a_backwards_move?(new_y), diagonal_move_allowed?(new_x, new_y)].all? end @@ -16,13 +17,13 @@ def move_number(new_y) end def first_move?(new_y) - (color == 1 && y_position == 2) || (color == 0 && y_position == 7) + (color == 1 && y_position == 2) || (color == 0 && y_position == 7) end def no_vertical_obstacle?(new_x, new_y) return true if x_position != new_x - if move_number(new_y) == 2 - !is_obstructed?(x_position, new_y) && no_vertical_adjacent_obstacle?(x_position, new_y) + if move_number(new_y) == 2 + !is_obstructed?(x_position, new_y) && no_vertical_adjacent_obstacle?(x_position, new_y) else (no_vertical_adjacent_obstacle?(x_position, new_y)) end @@ -33,13 +34,13 @@ def no_vertical_adjacent_obstacle?(x_position, new_y) end def not_a_backwards_move?(new_y) - color == 1 ? (new_y - y_position) > 0 : (new_y - y_position) < 0 + color == 1 ? (new_y - y_position) > 0 : (new_y - y_position) < 0 end def diagonal_move_allowed?(new_x, new_y) return true if new_x == x_position - if (new_x - x_position).abs == 1 && (new_y - y_position).abs == 1 - capture_diagonally?(new_x, new_y) + if (new_x - x_position).abs == 1 && (new_y - y_position).abs == 1 + capture_diagonally?(new_x, new_y) else false end @@ -52,5 +53,5 @@ def capture_diagonally?(new_x, new_y) def enemy_piece?(new_x, new_y) obstacle_piece = Piece.find_by(:x_position => new_x, :y_position => new_y) obstacle_piece.color != color - end -end \ No newline at end of file + end +end diff --git a/app/models/piece.rb b/app/models/piece.rb index fccf23e..a5c7c1e 100644 --- a/app/models/piece.rb +++ b/app/models/piece.rb @@ -106,10 +106,6 @@ def move_to!(new_x, new_y) end end - def moved_into_check?(color) - game.checking_piece?(color) - end - def friendly_piece?(new_x, new_y) obstacle_piece = game.pieces.find_by( :x_position => new_x, :y_position => new_y, :active => 1)