Class: ChessValidator::GameLogic

Inherits:
Object
  • Object
show all
Defined in:
lib/game_logic.rb

Class Method Summary collapse

Class Method Details

.checkmate_value(fen, board, no_moves) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/game_logic.rb', line 21

def checkmate_value(fen, board, no_moves)
  king, occupied_spaces = MoveLogic.find_king_and_spaces(board, fen.active)
  in_check = !MoveLogic.king_is_safe?(fen.active, board, king.position, occupied_spaces)

  if no_moves && in_check
    fen.active == 'w' ? '0-1' : '1-0'
  end
end

.draw?(fen, board, no_moves) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/game_logic.rb', line 30

def draw?(fen, board, no_moves)
  [fen.halfmove == '50', no_moves, insufficient_material?(board)].any?
end

.find_game_result(fen_notation) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/game_logic.rb', line 8

def find_game_result(fen_notation)
  fen = PGN::FEN.new(fen_notation)
  board = BoardLogic.build_board(fen)
  no_moves = MoveLogic.next_moves(fen).empty?
  checkmate_result = checkmate_value(fen, board, no_moves)

  if checkmate_result
    checkmate_result
  elsif draw?(fen, board, no_moves)
    '1/2-1/2'
  end
end

.insufficient_material?(board) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/game_logic.rb', line 34

def insufficient_material?(board)
  if board.size < 4
    board.values.none? { |piece| ['q', 'r', 'p'].include?(piece.piece_type.downcase) }
  end
end