Class: Chess::Game
Overview
This class rappresents a chess game.
Class Method Summary collapse
-
.load_fen(fen) ⇒ Game
Creates a new game from a FEN string.
-
.load_pgn(file) ⇒ Game
Creates a new game from a file in PGN format.
Instance Method Summary collapse
-
#active_player ⇒ Symbol
Returns
:white
if the active player is the white player,:black
otherwise. -
#inactive_player ⇒ Symbol
Returns
:white
if the inactive player is the white player,:black
otherwise. -
#initialize(moves = []) ⇒ Game
constructor
A new instance of Game.
-
#move(notation) ⇒ String
(also: #move=, #<<)
Make a move.
-
#moves=(moves) ⇒ Object
Make the array of moves.
-
#over? ⇒ Boolean
Returns
true
if the game is over. -
#pgn ⇒ Chess::Pgn
Returns the PGN rappresenting the game.
-
#status ⇒ Symbol
Returns the status of the game.
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.
7 8 9 10 |
# File 'lib/chess/game.rb', line 7 def initialize(moves = []) super() moves.each { |m| move(m) } end |
Class Method Details
.load_fen(fen) ⇒ Game
This game do not have history before the FEN placement.
Creates a new game from a FEN string.
40 41 42 43 44 45 46 |
# File 'lib/chess/game.rb', line 40 def self.load_fen(fen) raise InvalidFenFormatError.new(fen) unless /^((?:[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+)$/.match?(fen) game = Chess::Game.new game.set_fen!(fen) return game end |
.load_pgn(file) ⇒ Game
Creates a new game from a file in PGN format.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/chess/game.rb', line 18 def self.load_pgn(file) pgn = Chess::Pgn.new(file) game = Chess::Game.new pgn.moves.each { |m| game.move(m) } unless 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_player ⇒ Symbol
Returns :white
if the active player is the white player, :black
otherwise.
83 84 85 |
# File 'lib/chess/game.rb', line 83 def active_player self.board.active_color ? :black : :white end |
#inactive_player ⇒ Symbol
Returns :white
if the inactive player is the white player, :black
otherwise.
90 91 92 |
# File 'lib/chess/game.rb', line 90 def inactive_player self.board.active_color ? :white : :black end |
#move(notation) ⇒ String Also known as: move=, <<
This add a new Board in the Chess::Game.
Make a move.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/chess/game.rb', line 59 def move(notation) = (notation) if [:from] move2([:from], [:to], [:promotion]) else super([:name], [:dis], [:to], [:promotion]) end rescue IllegalMoveError raise IllegalMoveError.new("Illegal move '#{notation}'\nStatus: #{self.status}\nPlayer turn #{self.active_player}\n#{self}") if ENV['DEBUG'] raise IllegalMoveError.new("Illegal move '#{notation}'") end |
#moves=(moves) ⇒ Object
Make the array of moves.
76 77 78 |
# File 'lib/chess/game.rb', line 76 def moves=(moves) moves.each { |m| move(m) } end |
#over? ⇒ Boolean
Returns true
if the game is over.
129 130 131 |
# File 'lib/chess/game.rb', line 129 def over? return self.result != '*' end |
#pgn ⇒ Chess::Pgn
Returns the PGN rappresenting the game.
135 136 137 138 139 140 |
# File 'lib/chess/game.rb', line 135 def pgn pgn = Chess::Pgn.new pgn.moves = self.moves pgn.result = self.result return pgn end |
#status ⇒ Symbol
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_move_rule
: draw for fifty-move rule. -
threefold_repetition
: draw for threefold repetition.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/chess/game.rb', line 107 def status case self.result when '*' return :in_progress when '1-0' return :white_won if self.board.checkmate? return :white_won_resign when '0-1' return :black_won if self.board.checkmate? return :black_won_resign when '1/2-1/2' return :stalemate if self.board.stalemate? return :insufficient_material if self.board.insufficient_material? return :fifty_move_rule if self.board.fifty_move_rule? return :threefold_repetition if self.threefold_repetition? end end |