Class: Chess::Game
Overview
This class rappresents a chess game.
Class Method Summary collapse
-
.load_fen(fen) ⇒ Object
Creates a new game from a FEN string.
-
.load_pgn(file) ⇒ Object
Creates a new game from a file in PGN format.
Instance Method Summary collapse
-
#active_player ⇒ Object
Returns
:white
if the active player is the white player,:black
otherwise. -
#inactive_player ⇒ Object
Returns
:white
if the inactive player is the white player,:black
otherwise. -
#initialize(moves = []) ⇒ Game
constructor
Create a new game.
-
#move(m) ⇒ Object
(also: #move=, #<<)
Make a move.
-
#moves=(moves) ⇒ Object
Make the array of moves.
-
#over? ⇒ Boolean
Returns
true
if the game is over. -
#pgn ⇒ Object
Returns the PGN rappresenting the game.
-
#status ⇒ Object
Returns the status of the game.
Methods inherited from CGame
#[], #current, #draw, #each, #full_moves, #move2, #move3, #moves, #resign, #result, #rollback!, #set_fen!, #size, #threefold_repetition?, #to_s
Constructor Details
#initialize(moves = []) ⇒ Game
Create a new game. If an array of moves is provided, the moves will be performed. May be raise an IllegalMoveError or BadNotationError.
10 11 12 |
# File 'lib/chess/game.rb', line 10 def initialize(moves = []) moves.each { |m| move(m) } end |
Class Method Details
.load_fen(fen) ⇒ Object
Creates a new game from a FEN string. May be raise an InvalidFenFormatError. Warning: this game do not have history before the FEN placement.
39 40 41 42 43 44 45 46 47 |
# File 'lib/chess/game.rb', line 39 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) ⇒ Object
Creates a new game from a file in PGN format. May be raise an InvalidPgnFormatError or IllegalMoveError or BadNotationError.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/chess/game.rb', line 16 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' if game.board.insufficient_material? || game.board.stalemate? || game.threefold_repetition? || game.board.fifty_rule_move? game.draw end end end return game end |
Instance Method Details
#active_player ⇒ Object
Returns :white
if the active player is the white player, :black
otherwise.
76 77 78 |
# File 'lib/chess/game.rb', line 76 def active_player self.board.active_color ? :black : :white end |
#inactive_player ⇒ Object
Returns :white
if the inactive player is the white player, :black
otherwise.
81 82 83 |
# File 'lib/chess/game.rb', line 81 def inactive_player self.board.active_color ? :white : :black end |
#move(m) ⇒ Object Also known as: move=, <<
Make a move. This add a new Board in the Storyboard. The parameter m
represents the short algebraic chess notation string of the move. m
can be from_square plus to_square (‘e2e4’, …, ‘b1c3’). This method returns a string that represents the short algebraic chess notation of the move. Raise an IllegalMoveError if the move is illegal. Raise an BadNotationError if the short algebraic chess notation is malformed.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/chess/game.rb', line 55 def move(m) begin = (m) if [:from] move2([:from], [:to], [:promotion]) else super([:name], [:dis], [:to], [:promotion]) end rescue IllegalMoveError => e raise IllegalMoveError.new("Illegal move '#{m}'\n#{self.to_s}") end end |
#moves=(moves) ⇒ Object
Make the array of moves.
71 72 73 |
# File 'lib/chess/game.rb', line 71 def moves=(moves) moves.each { |m| move(m) } end |
#over? ⇒ Boolean
Returns true
if the game is over
128 129 130 |
# File 'lib/chess/game.rb', line 128 def over? return self.result != '*' end |
#pgn ⇒ Object
Returns the PGN rappresenting the game.
133 134 135 136 137 138 |
# File 'lib/chess/game.rb', line 133 def pgn pgn = Chess::Pgn.new pgn.moves = self.moves pgn.result = self.result return pgn end |
#status ⇒ Object
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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/chess/game.rb', line 97 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 |