Class: Chess::Game

Inherits:
CGame
  • Object
show all
Defined in:
lib/chess/game.rb

Overview

This class rappresents a chess game.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CGame

#[], #coord_moves, #current, #draw, #each, #full_moves, #move2, #move3, #moves, #resign, #result, #rollback!, #set_fen!, #size, #threefold_repetition?, #to_s

Constructor Details

#initialize(moves = []) ⇒ Game

Returns a new instance of Game.

Parameters:

  • moves (Array<String>) (defaults to: [])

    If an array of moves is provided, the moves will be performed.

Raises:



11
12
13
# File 'lib/chess/game.rb', line 11

def initialize(moves = [])
  moves.each { |m| move(m) }
end

Class Method Details

.load_fen(fen) ⇒ Game

Note:

This game do not have history before the FEN placement.

Creates a new game from a FEN string.

Parameters:

  • fen (String)

    The FEN string to laod.

Returns:

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/chess/game.rb', line 43

def self.load_fen(fen)
  if fen =~ /^((?:[PRNBQKprnbqk1-8]{1,8}\/){7}[RNBQKPrnbqkp1-8]{1,8})\s(w|b)\s(K?Q?k?q?|-)\s([a-h][1-8]|-)\s(\d+)\s(\d+)$/
    game = Chess::Game.new
    game.set_fen!(fen)
    return game
  else
    raise InvalidFenFormatError.new(fen)
  end
end

.load_pgn(file) ⇒ Game

Creates a new game from a file in PGN format.

Parameters:

  • file (String)

    The path of the PGN to laod.

Returns:

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chess/game.rb', line 21

def self.load_pgn(file)
  pgn = Chess::Pgn.new(file)
  game = Chess::Game.new
  pgn.moves.each { |m| game.move(m) }
  if !game.over?
    case pgn.result
    when '1-0'
      game.resign(:black)
    when '0-1'
      game.resign(:white)
    when '1/2-1/2'
      game.draw
    end
  end
  return game
end

Instance Method Details

#active_playerSymbol

Returns ‘:white` if the active player is the white player, `:black` otherwise.

Returns:

  • (Symbol)


92
93
94
# File 'lib/chess/game.rb', line 92

def active_player
  self.board.active_color ? :black : :white
end

#inactive_playerSymbol

Returns ‘:white` if the inactive player is the white player, `:black` otherwise.

Returns:

  • (Symbol)


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

def inactive_player
  self.board.active_color ? :white : :black
end

#move(m) ⇒ String Also known as: move=, <<

Note:

This add a new Board in the Chess::Game.

Make a move.

Parameters:

  • m (String)

    Represents the short algebraic chess notation string of the move. ‘m` can be also from_square plus to_square _(’e2e4’, …, ‘b1c3’)_ (coordinate chess notation).

Returns:

  • (String)

    Returns a string that represents the short algebraic chess notation of the move.

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chess/game.rb', line 64

def move(m)
  begin
    expand = expand_move(m)
    if expand[:from]
      move2(expand[:from], expand[:to], expand[:promotion])
    else
      super(expand[:name], expand[:dis], expand[:to], expand[:promotion])
    end
  rescue IllegalMoveError
    if ENV['DEBUG']
      raise IllegalMoveError.new("Illegal move '#{m}'\nStatus: #{self.status}\nPlayer turn #{self.active_player}\n#{self.to_s}")
    else
      raise IllegalMoveError.new("Illegal move '#{m}'")
    end
  end
end

#moves=(moves) ⇒ Object

Make the array of moves.

Parameters:

  • moves (Array<String>)

    The array of moves to performe.



85
86
87
# File 'lib/chess/game.rb', line 85

def moves=(moves)
  moves.each { |m| move(m) }
end

#over?Boolean

Returns ‘true` if the game is over.

Returns:

  • (Boolean)


148
149
150
# File 'lib/chess/game.rb', line 148

def over?
  return self.result != '*'
end

#pgnString

Returns the PGN rappresenting the game.

Returns:

  • (String)


154
155
156
157
158
159
# File 'lib/chess/game.rb', line 154

def pgn
  pgn = Chess::Pgn.new
  pgn.moves = self.moves
  pgn.result = self.result
  return pgn
end

#statusString

Returns the status of the game. Possible states are:

  • ‘in_progress`: the game is in progress.

  • ‘white_won`: white player has won with a checkmate.

  • ‘black_won`: black player has won with a checkmate.

  • ‘white_won_resign`: white player has won for resign.

  • ‘black_won_resign`: black player has won for resign.

  • ‘stalemate`: draw for stalemate.

  • ‘insufficient_material`: draw for insufficient material to checkmate.

  • ‘fifty_rule_move`: draw for fifty rule move.

  • ‘threefold_repetition`: draw for threefold_repetition.

  • ‘unknown`: something went wrong.

Returns:

  • (String)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chess/game.rb', line 117

def status
  case self.result
  when '*'
    return :in_progress
  when '1-0'
    if self.board.checkmate?
      return :white_won
    else
      return :white_won_resign
    end
  when '0-1'
    if self.board.checkmate?
      return :black_won
    else
      return :black_won_resign
    end
  when '1/2-1/2'
    if self.board.stalemate?
      return :stalemate
    elsif self.board.insufficient_material?
      return :insufficient_material
    elsif self.board.fifty_rule_move?
      return :fifty_rule_move
    elsif self.threefold_repetition?
      return :threefold_repetition
    end
  end
  return :unknown
end