class Romance::Game
Public Class Methods
load(db, starting_position_feen)
click to toggle source
# File lib/romance/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/romance/game.rb, line 13 def initialize(db, **starting_position_params) @db = db @positions = [ Position.new(**starting_position_params) ] end
Public Instance Methods
current_position()
click to toggle source
# File lib/romance/game.rb, line 25 def current_position @positions.fetch(-1) end
inspect()
click to toggle source
# File lib/romance/game.rb, line 37 def inspect "<Romance::Game##{object_id} position ##{@positions.length}>" end
over?()
click to toggle source
# File lib/romance/game.rb, line 33 def over? !current_position.ally_king_on_the_board? end
play!(move)
click to toggle source
# File lib/romance/game.rb, line 21 def play!(move) @positions << current_position.after(move) if legal?(*move) end
sha256()
click to toggle source
# File lib/romance/game.rb, line 29 def sha256 ::Digest::SHA256.hexdigest(position_feens.join) end
Private Instance Methods
legal?(src_index, dst_index, moved_piece, captured = nil)
click to toggle source
# File lib/romance/game.rb, line 47 def legal?(src_index, dst_index, moved_piece, captured = nil) raise Error::TheKingHasBeenCaptured unless current_position.ally_king_on_the_board? move = [src_index, dst_index, moved_piece, captured] raise Error::InvalidPseudoLegalMove unless pseudo_legal_moves(current_position).include?(move) next_position = current_position.after(move) raise Error::RepetitionOfPosition if repetition?(next_position) true end
position_feens()
click to toggle source
# File lib/romance/game.rb, line 43 def position_feens @positions.map(&:to_feen).uniq.sort end
pseudo_legal_moves(position)
click to toggle source
Returns all pseudo-legal moves.
@param position [Position] The state of the game.
@return [Set] All pseudo-legal moves.
# File lib/romance/game.rb, line 70 def pseudo_legal_moves(position) ::Ugoki::Selector.new(@db, position).call end
repetition?(next_position)
click to toggle source
# File lib/romance/game.rb, line 61 def repetition?(next_position) @positions.map(&:to_s).include?(next_position.to_s) end