class CaissaGuardian::Game
Game
namespace.
Public Class Methods
load(db, starting_position_feen)
click to toggle source
# File lib/caissa_guardian/game.rb, line 9 def self.load(db, starting_position_feen) new(db, **::FEEN::Parser.call(starting_position_feen)) end
new(db, **starting_position_params)
click to toggle source
# File lib/caissa_guardian/game.rb, line 13 def initialize(db, **starting_position_params) @db = db @positions = [ Position.new(**starting_position_params) ] end
Public Instance Methods
play!(move)
click to toggle source
# File lib/caissa_guardian/game.rb, line 32 def play!(move) @positions << try(move) end
try(move)
click to toggle source
# File lib/caissa_guardian/game.rb, line 21 def try(move) raise Error::InvalidPseudoLegalMove, move.inspect unless pseudo_legal_moves(current_position).include?(move) next_position = current_position.after(move) raise Error::RepetitionOfPosition, move.inspect if repetition?(next_position) raise Error::KingInCheck, move.inspect if in_check?(next_position) next_position end
Private Instance Methods
current_position()
click to toggle source
# File lib/caissa_guardian/game.rb, line 38 def current_position @positions.fetch(-1) end
in_check?(position)
click to toggle source
# File lib/caissa_guardian/game.rb, line 46 def in_check?(position) king_square_id = position.enemy_king_square_id return false if king_square_id.nil? next_pseudo_legal_moves = pseudo_legal_moves(position) next_pseudo_legal_moves.any? do |next_pseudo_legal_move| next_pseudo_legal_actions = next_pseudo_legal_move.each_slice(4) next_pseudo_legal_actions.any? do |next_pseudo_legal_action| next_pseudo_legal_action.fetch(1) == king_square_id end end end
pseudo_legal_moves(position)
click to toggle source
@param position [Position] The state of the game.
# File lib/caissa_guardian/game.rb, line 62 def pseudo_legal_moves(position) ::Ugoki::Selector.new(@db, position).call end
repetition?(next_position)
click to toggle source
# File lib/caissa_guardian/game.rb, line 42 def repetition?(next_position) @positions.map(&:to_s).include?(next_position.to_s) end