Class: Chess::CGame
- Inherits:
-
Object
- Object
- Chess::CGame
- Defined in:
- ext/chess/chess.c,
ext/chess/chess.c
Overview
This class rappresents a collection of boards of a single chess game.
Direct Known Subclasses
Instance Method Summary collapse
- #[](n) ⇒ Board
-
#coord_moves ⇒ Array<String>
Returns the array with all moves done in coordinate chess notation _(es: b1c3)_.
- #current ⇒ Board (also: #board)
-
#draw ⇒ nil
The game result is set to draw.
-
#each {|board, move, coord_move, index| ... } ⇒ Game, Array<String>
Cycle the Game.
- #full_moves ⇒ Object
-
#move(piece, disambiguating, to_coord, promote_in) ⇒ String
Make a move.
-
#move2(from, to, promote_in) ⇒ String
Make a move.
-
#move3(from, to, promote_in) ⇒ String
Make a move.
-
#moves ⇒ Array<String>
Returns the array with all moves done _(es: Nc3)_.
-
#resign(color) ⇒ nil
The game result is set to ‘1-0’ if ‘color` is black, otherwise is set to ’0-1’ if color is white.
-
#result ⇒ String
Returns the result of the game.
-
#rollback! ⇒ Game
Rollback last move.
-
#set_fen!(fen) ⇒ Game
Set the game position by FEN string.
-
#size ⇒ Integer
Returns the number of moves done.
-
#threefold_repetition? ⇒ Boolean
Returns ‘true` if a player can claim draw by the threefold repetition rule, `false` otherwise.
-
#to_s ⇒ String
Current Board to string.
Instance Method Details
#[](n) ⇒ Board
210 211 212 213 214 215 216 217 218 219 220 |
# File 'ext/chess/chess.c', line 210
VALUE
game_boards (VALUE self, VALUE index)
{
Game *g;
Data_Get_Struct (self, Game, g);
int n = FIX2INT (index);
Board *board = get_board (g, n);
if (board)
return Data_Wrap_Struct (board_klass, 0, 0, board);
return Qnil;
}
|
#coord_moves ⇒ Array<String>
258 259 260 261 262 263 264 265 266 267 |
# File 'ext/chess/chess.c', line 258
VALUE
game_coord_moves (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
VALUE moves = rb_ary_new ();
for (int i = 0; i < g->current; i++)
rb_ary_push (moves, rb_str_new2 (g->coord_moves[i]));
return moves;
}
|
#current ⇒ Board Also known as: board
228 229 230 231 232 233 234 |
# File 'ext/chess/chess.c', line 228
VALUE
game_current_board (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
return game_boards (self, INT2FIX (g->current-1));
}
|
#draw ⇒ nil
195 196 197 198 199 200 201 202 |
# File 'ext/chess/chess.c', line 195
VALUE
game_draw (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
g->result = DRAW;
return Qnil;
}
|
#each {|board, move, coord_move, index| ... } ⇒ Game, Array<String>
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'ext/chess/chess.c', line 339
VALUE
game_each (VALUE self)
{
if (!rb_block_given_p ())
return game_moves(self);
Game *g;
Data_Get_Struct (self, Game, g);
for (int i = 0; i < g->current; i++)
rb_yield_values (4,
Data_Wrap_Struct (board_klass, 0, 0, get_board (g, i)),
rb_str_new2 (g->moves[i]),
rb_str_new2 (g->coord_moves[i]),
INT2FIX (i));
return self;
}
|
#full_moves ⇒ Object
273 274 275 276 277 278 |
# File 'ext/chess/chess.c', line 273
VALUE
game_full_moves (VALUE self)
{
printf ("DEPRECATION WARNING: `full_moves` is deprecated and will be removed, please use `coord_moves` to get the array with all moves done in coordinate chess notation.\n");
return game_coord_moves (self);
}
|
#move(piece, disambiguating, to_coord, promote_in) ⇒ String
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'ext/chess/chess.c', line 71
VALUE
game_move (VALUE self, VALUE rb_piece, VALUE rb_disambiguating, VALUE rb_to_coord, VALUE rb_promote_in)
{
Game *g;
Data_Get_Struct (self, Game, g);
Board *board = current_board (g);
char piece = StringValuePtr (rb_piece)[0];
char *disambiguating = rb_disambiguating == Qnil ? NULL : StringValuePtr (rb_disambiguating);
char *to_coord = StringValuePtr (rb_to_coord);
char promote_in = rb_promote_in == Qnil ? '\0' : StringValuePtr (rb_promote_in)[0];
int from, to;
if (
get_coord (board, piece, disambiguating, to_coord, promote_in, &from, &to) &&
apply_move (g, from, to, promote_in)
)
return rb_str_new2 (current_move (g));
else
rb_raise (illegal_move_error, "Illegal move");
}
|
#move2(from, to, promote_in) ⇒ String
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'ext/chess/chess.c', line 106
VALUE
game_move2 (VALUE self, VALUE rb_from, VALUE rb_to, VALUE rb_promote_in)
{
Game *g;
Data_Get_Struct (self, Game, g);
Board *board = current_board (g);
int from = coord_to_square (StringValuePtr (rb_from));
int to = coord_to_square (StringValuePtr (rb_to));
char promote_in = rb_promote_in == Qnil ? '\0' : StringValuePtr (rb_promote_in)[0];
if (pseudo_legal_move (board, from, to) && apply_move (g, from, to, promote_in))
return rb_str_new2 (current_move (g));
else
rb_raise (illegal_move_error, "Illegal move");
}
|
#move3(from, to, promote_in) ⇒ String
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'ext/chess/chess.c', line 150
VALUE
game_move3 (VALUE self, VALUE rb_from, VALUE rb_to, VALUE rb_promote_in)
{
Game *g;
Data_Get_Struct (self, Game, g);
Board *board = current_board (g);
int from = FIX2INT (rb_from);
int to = FIX2INT (rb_to);
char promote_in = rb_promote_in == Qnil ? '\0' : StringValuePtr (rb_promote_in)[0];
if (pseudo_legal_move (board, from, to) && apply_move (g, from, to, promote_in))
return rb_str_new2 (current_move (g));
else
rb_raise (illegal_move_error, "Illegal move");
}
|
#moves ⇒ Array<String>
241 242 243 244 245 246 247 248 249 250 |
# File 'ext/chess/chess.c', line 241
VALUE
game_moves (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
VALUE moves = rb_ary_new ();
for (int i = 0; i < g->current; i++)
rb_ary_push (moves, rb_str_new2 (g->moves[i]));
return moves;
}
|
#resign(color) ⇒ nil
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'ext/chess/chess.c', line 173
VALUE
game_resign (VALUE self, VALUE color)
{
Game *g;
Data_Get_Struct (self, Game, g);
const char *c;
if (TYPE (color) == T_SYMBOL)
c = rb_id2name (SYM2ID (color));
else
c = StringValuePtr (color);
if (strcmp (c, "black") == 0)
g->result = WHITE_WON;
else if (strcmp (c, "white") == 0)
g->result = BLACK_WON;
return Qnil;
}
|
#result ⇒ String
306 307 308 309 310 311 312 313 314 315 |
# File 'ext/chess/chess.c', line 306
VALUE
game_result (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
char *result = result_to_s (g->result);
VALUE rb_result = rb_str_new2 (result);
free (result);
return rb_result;
}
|
#rollback! ⇒ Game
360 361 362 363 364 365 366 367 |
# File 'ext/chess/chess.c', line 360
VALUE
game_rollback (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
rollback (g);
return self;
}
|
#set_fen!(fen) ⇒ Game
36 37 38 39 40 41 42 43 |
# File 'ext/chess/chess.c', line 36
VALUE
game_set_fen (VALUE self, VALUE fen)
{
Game *g;
Data_Get_Struct (self, Game, g);
set_fen (g, StringValuePtr (fen));
return self;
}
|
#size ⇒ Integer
322 323 324 325 326 327 328 |
# File 'ext/chess/chess.c', line 322
VALUE
game_size (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
return INT2FIX (g->current);
}
|
#threefold_repetition? ⇒ Boolean
285 286 287 288 289 290 291 292 293 294 |
# File 'ext/chess/chess.c', line 285
VALUE
game_threefold_repetition (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
if (threefold_repetition (g))
return Qtrue;
else
return Qfalse;
}
|
#to_s ⇒ String
374 375 376 377 378 379 380 381 382 383 384 |
# File 'ext/chess/chess.c', line 374
VALUE
game_to_s (VALUE self)
{
Game *g;
Data_Get_Struct (self, Game, g);
Board *b = get_board (g, g->current-1);
char *s = print_board (b);
VALUE rb_s = rb_str_new2 (s);
free (s);
return rb_s;
}
|