Class: ChessEngine::Game

Inherits:
Object
  • Object
show all
Includes:
MoveValidator
Defined in:
lib/chess_engine/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MoveValidator

#en_passant_coords, #fatal_move?, #pawn_valid_moves, #relative_coords, #repeated_move, #safe_moves, #valid_move?, #valid_moves, #valid_moves_recursive

Constructor Details

#initializeGame

Returns a new instance of Game.



13
14
15
16
17
18
19
20
# File 'lib/chess_engine/game.rb', line 13

def initialize
  @board = Board.new
  @board.set_default
  @current_color = :white
  @last_piece = nil
  @name = nil
  @promotion_coord = false
end

Instance Attribute Details

#current_colorObject (readonly)

Returns the value of attribute current_color.



10
11
12
# File 'lib/chess_engine/game.rb', line 10

def current_color
  @current_color
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/chess_engine/game.rb', line 9

def name
  @name
end

Instance Method Details

#[](str) ⇒ Object



43
44
45
46
47
# File 'lib/chess_engine/game.rb', line 43

def [](str)
  letters = ("a".."h").to_a
  return nil unless /[a-h][1-8]/.match?(str)
  @board.at([letters.find_index(str[0]), str[1].to_i - 1])
end

#castling(length) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chess_engine/game.rb', line 62

def castling(length)
  row = @current_color == :white ? 0 : 7
  king = @board.at([4, row])
  if length == :short
    rook = @board.at([7, row])
    line = [5, 6]
    moves = [Move.new(@board, [4, row], [6, row]),
      Move.new(@board, [7, row], [5, row])]
  else
    rook = @board.at([0, row])
    line = [1, 2, 3]
    moves = [Move.new(@board, [4, row], [2, row]),
      Move.new(@board, [0, row], [3, row])]
  end
  raise InvalidMove, "Invalid castling" unless
    king && rook && king.moves_count == 0 && rook.moves_count == 0 &&
    line.all? { |x| @board.at([x, row]).nil? }

  moves.each { |move| move.commit }
  if king_attacked?
    moves.each { |move| move.rollback }
    raise InvalidMove, "Fatal move"
  end
  @last_piece = nil
  next_player
end

#check?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/chess_engine/game.rb', line 99

def check?
  king_attacked?
end

#drawObject



49
50
51
# File 'lib/chess_engine/game.rb', line 49

def draw
  @board.to_s
end

#move(string) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chess_engine/game.rb', line 22

def move(string)
  from, to = Game.string_to_move(string)
  piece = @board.at(from)
  raise InvalidMove, "Game is over" if over?
  raise InvalidMove, "#{@current_color} player should execute pawn promotion first" if needs_promotion?
  raise InvalidMove, "Empty square is chosen" if piece.nil?
  raise InvalidMove, "This is not your piece" unless piece.color == @current_color
  raise InvalidMove, "Invalid move" unless valid_moves(from).include?(to)
  move = Move.new(@board, from, to)
  move.commit
  if king_attacked?
    move.rollback
    raise InvalidMove, "Fatal move"
  end

  @last_piece = piece
  piece.moves_count += 1
  @promotion_coord = to and return if piece.pawn? && [7, 0].include?(to[1])
  next_player
end

#needs_promotion?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/chess_engine/game.rb', line 95

def needs_promotion?
  !!@promotion_coord
end

#over?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/chess_engine/game.rb', line 89

def over?
  @board.piece_coordinates(@current_color).all? do |coord|
    safe_moves(coord).empty?
  end
end

#promotion(class_name) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/chess_engine/game.rb', line 53

def promotion(class_name)
  unless ["rook", "knight", "elephant", "queen"].include?(class_name.downcase)
    raise InvalidMove, "Invalid promotion"
  end
  @board.set_at(@promotion_coord, Module.const_get("ChessEngine::#{class_name.capitalize}").new(@current_color))
  @promotion_coord = nil
  next_player
end