| 1 | package Game::RoyalUr; | ||
| 2 | |||
| 3 | 12 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 12 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 12 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 12 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 12 | ▶ | use Scalar::Util (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Object::Proto::Sugar; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | use Game::RoyalUr::Engine qw(EMPTY SIDE_LIGHT SIDE_DARK ROUTE_SHORT ROUTE_LONG piece_of); | ||
| 12 | use Game::RoyalUr::Rules; | ||
| 13 | use Game::RoyalUr::Variant; | ||
| 14 | use Game::RoyalUr::Dice (); | ||
| 15 | use Game::RoyalUr::Notation (); | ||
| 16 | use Game::RoyalUr::Error; | ||
| 17 | use Game::RoyalUr::Result; | ||
| 18 | |||
| 19 | our $VERSION = '0.03'; | ||
| 20 | |||
| 21 | my $E = 'Game::RoyalUr::Engine'; | ||
| 22 | my $X = 'Game::RoyalUr::Error'; | ||
| 23 | |||
| 24 | my (%SIDE_OF, %ROUTE_OF, %OTHER); | ||
| 25 | BEGIN { | ||
| 26 | %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK); | ||
| 27 | %ROUTE_OF = (short => ROUTE_SHORT, long => ROUTE_LONG); | ||
| 28 | %OTHER = (light => 'dark', dark => 'light'); | ||
| 29 | } | ||
| 30 | |||
| 31 | has _seed => (is => 'ro', init_arg => 'seed', private => 1); | ||
| 32 | |||
| 33 | has _rules_given => (is => 'ro', init_arg => 'rules', private => 1); | ||
| 34 | |||
| 35 | has _first_given => (is => 'ro', init_arg => 'first', private => 1); | ||
| 36 | |||
| 37 | has _position_given => (is => 'ro', init_arg => 'position', private => 1); | ||
| 38 | |||
| 39 | has _script => (is => 'ro', init_arg => 'script', private => 1); | ||
| 40 | |||
| 41 | has _variant => (is => 'rw', private => 1); | ||
| 42 | |||
| 43 | has _game => (is => 'rw', private => 1); | ||
| 44 | |||
| 45 | has _first => (is => 'rw', private => 1); | ||
| 46 | |||
| 47 | has _opening => (is => 'rw', private => 1); | ||
| 48 | |||
| 49 | has _rolls => (is => 'rw', private => 1); | ||
| 50 | |||
| 51 | has _roll => (is => 'rw', private => 1); | ||
| 52 | |||
| 53 | has _faces => (is => 'rw', private => 1); | ||
| 54 | |||
| 55 | has _number => (is => 'rw', private => 1); | ||
| 56 | |||
| 57 | has _log => (is => 'rw', private => 1); | ||
| 58 | |||
| 59 | has _choices => (is => 'rw', private => 1); | ||
| 60 | |||
| 61 | has _resigned => (is => 'rw', private => 1); | ||
| 62 | |||
| 63 | has _error => (is => 'rw', private => 1); | ||
| 64 | |||
| 65 | sub BUILD { | ||
| 66 | my ($self) = @_; | ||
| 67 | my ($seed, $script) = ($self->_seed, $self->_script); | ||
| 68 | Carp::croak('Game::RoyalUr: a game wants a seed: some bytes that decide every throw of its dice') | ||
| 69 | unless (defined $seed && !ref $seed && length $seed) || ref $script eq 'ARRAY'; | ||
| 70 | |||
| 71 | my $variant = Game::RoyalUr::Variant->of($self->_rules_given); | ||
| 72 | $self->_variant($variant); | ||
| 73 | |||
| 74 | my $position = $self->_position_given; | ||
| 75 | if (defined $position) { | ||
| 76 | my $code = Game::RoyalUr::Notation::validate_position($position); | ||
| 77 | Carp::croak("Game::RoyalUr: the position was refused, code $code") if $code; | ||
| 78 | } | ||
| 79 | |||
| 80 | my $first = $self->_first_given; | ||
| 81 | Carp::croak("Game::RoyalUr: first is 'light' or 'dark'") | ||
| 82 | if defined $first && !exists $SIDE_OF{$first}; | ||
| 83 | |||
| 84 | my @opening; | ||
| 85 | if (!defined $first && !defined $position) { | ||
| 86 | Carp::croak('Game::RoyalUr: a game with no seed must be told who moves first') if $script; | ||
| 87 | my ($side, $used, $throws) = Game::RoyalUr::Dice::opening_for($seed, $variant->dice); | ||
| 88 | $first = $side; | ||
| 89 | @opening = map { join '', @$_ } @$throws; | ||
| 90 | } | ||
| 91 | |||
| 92 | my $game = Game::RoyalUr::Rules->new( | ||
| 93 | rules => $variant->as_hash, | ||
| 94 | (defined $position ? (position => $position) : ()), | ||
| 95 | (defined $first ? (first => $first) : ()), | ||
| 96 | ); | ||
| 97 | my $consistent = $game->board->consistent($variant->pieces); | ||
| 98 | undef $game unless $consistent; | ||
| 99 | Carp::croak('Game::RoyalUr: the position does not hold ' . $variant->pieces . ' pieces a side') | ||
| 100 | unless $consistent; | ||
| 101 | |||
| 102 | $self->_game($game); | ||
| 103 | $self->_first($game->side); | ||
| 104 | $self->_opening(\@opening); | ||
| 105 | $self->_rolls(scalar @opening); | ||
| 106 | $self->_log([]); | ||
| 107 | $self->_choices([]); | ||
| 108 | $self->_settle; | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _settle { | ||
| 113 | my ($self) = @_; | ||
| 114 | my $game = $self->_game; | ||
| 115 | my $variant = $self->_variant; | ||
| 116 | $self->_roll(undef); | ||
| 117 | $self->_faces(undef); | ||
| 118 | $self->_number(undef); | ||
| 119 | |||
| 120 | while (!$self->is_over) { | ||
| 121 | my $number = $self->_rolls; | ||
| 122 | my ($roll, $faces); | ||
| 123 | if (my $script = $self->_script) { | ||
| 124 | my $next = $script->[ $number - @{ $self->_opening } ]; | ||
| 125 | return unless $next; | ||
| 126 | ($roll, $faces) = ($next->{roll}, $next->{faces}); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | my $throw = Game::RoyalUr::Dice::throw_for($self->_seed, $number, $variant->dice); | ||
| 130 | $faces = join '', @$throw; | ||
| 131 | $roll = Game::RoyalUr::Dice::roll_of(Game::RoyalUr::Dice::marked($throw), $variant); | ||
| 132 | } | ||
| 133 | $self->_rolls($number + 1); | ||
| 134 | |||
| 135 | if ($game->moves($roll)) { | ||
| 136 | $self->_roll($roll); | ||
| 137 | $self->_faces($faces); | ||
| 138 | $self->_number($number); | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | push @{ $self->_log }, { | ||
| 143 | ply => $game->ply, side => $game->side, throw => $number, | ||
| 144 | roll => $roll, faces => $faces, move => undef, | ||
| 145 | }; | ||
| 146 | $game->forfeit; | ||
| 147 | } | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _refuse { | ||
| 152 | my ($self, $code, %detail) = @_; | ||
| 153 | $self->_error($X->of($code, %detail)); | ||
| 154 | return undef; | ||
| 155 | } | ||
| 156 | |||
| 157 | my $cell_named = sub { | ||
| 158 | my ($name) = @_; | ||
| 159 | return -1 unless Game::RoyalUr::Notation::square_ok($name); | ||
| 160 | return $E->cell_of(ord(substr $name, 0, 1) - ord('a'), substr($name, 1, 1) - 1); | ||
| 161 | }; | ||
| 162 | |||
| 163 | sub _why_not { | ||
| 164 | my ($self, $from, $to) = @_; | ||
| 165 | my $game = $self->_game; | ||
| 166 | my $variant = $self->_variant; | ||
| 167 | my $side = $SIDE_OF{ $game->side }; | ||
| 168 | my $route = $ROUTE_OF{ $variant->route }; | ||
| 169 | my $board = $game->board; | ||
| 170 | my $roll = $self->_roll; | ||
| 171 | |||
| 172 | my $from_step = 0; | ||
| 173 | if ($from eq 'hand') { | ||
| 174 | return 'no_piece' unless $board->hand($side) > 0; | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | my $piece = $board->at($cell_named->($from)); | ||
| 178 | return 'no_piece' if $piece == EMPTY; | ||
| 179 | return 'not_your_piece' if $piece != piece_of($side); | ||
| 180 | $from_step = $E->route_step($route, $side, $cell_named->($from)); | ||
| 181 | return 'no_piece' unless $from_step; | ||
| 182 | } | ||
| 183 | |||
| 184 | my $length = $E->route_len($route); | ||
| 185 | my $lands = $from_step + $roll; | ||
| 186 | if ($to eq 'home') { | ||
| 187 | return 'overshoot' if $lands > $length + 1; | ||
| 188 | return 'wrong_distance' if $lands < $length + 1; | ||
| 189 | return 'bad_move'; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $cell = $cell_named->($to); | ||
| 193 | return 'wrong_distance' unless $lands <= $length && $E->route_cell($route, $side, $lands) == $cell; | ||
| 194 | my $piece = $board->at($cell); | ||
| 195 | return 'own_piece' if $piece == piece_of($side); | ||
| 196 | return 'safe_rosette' if $piece != EMPTY && $variant->safe_rosettes && $E->is_rosette($cell); | ||
| 197 | return 'bad_move'; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub play { | ||
| 201 | my ($self, $what) = @_; | ||
| 202 | $self->_error(undef); | ||
| 203 | return $self->_refuse('game_over') if $self->is_over; | ||
| 204 | return $self->_refuse('no_roll') unless defined $self->_roll; | ||
| 205 | |||
| 206 | my ($from, $to); | ||
| 207 | if (Scalar::Util::blessed($what) && $what->isa('Game::RoyalUr::Move')) { | ||
| 208 | ($from, $to) = ($what->from, $what->to); | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | my ($parsed, $why) = Game::RoyalUr::Notation::parse_move($what); | ||
| 212 | return $self->_refuse('bad_move', text => $what, why => $why) unless $parsed; | ||
| 213 | ($from, $to) = @{$parsed}{qw(from to)}; | ||
| 214 | } | ||
| 215 | |||
| 216 | my $game = $self->_game; | ||
| 217 | my $roll = $self->_roll; | ||
| 218 | my ($move) = grep { $_->from eq $from && $_->to eq $to } $game->moves($roll); | ||
| 219 | return $self->_refuse($self->_why_not($from, $to), from => $from, to => $to, roll => $roll) | ||
| 220 | unless $move; | ||
| 221 | |||
| 222 | push @{ $self->_choices }, { | ||
| 223 | depth => $game->depth, log => scalar @{ $self->_log }, | ||
| 224 | rolls => $self->_rolls, roll => $roll, faces => $self->_faces, number => $self->_number, | ||
| 225 | }; | ||
| 226 | push @{ $self->_log }, { | ||
| 227 | ply => $game->ply, side => $game->side, throw => $self->_number, | ||
| 228 | roll => $roll, faces => $self->_faces, move => $from . '-' . $to, | ||
| 229 | captures => $move->captures, rosette => $move->rosette, home => $move->home, | ||
| 230 | }; | ||
| 231 | $game->apply($move); | ||
| 232 | $self->_settle; | ||
| 233 | return $move; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub play_or_die { | ||
| 237 | my ($self, $what) = @_; | ||
| 238 | my $move = $self->play($what); | ||
| 239 | return $move if $move; | ||
| 240 | my $error = $self->_error; | ||
| 241 | Carp::croak('Game::RoyalUr: ' . $error->code . ': ' . $error->message); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub undo { | ||
| 245 | my ($self) = @_; | ||
| 246 | $self->_error(undef); | ||
| 247 | if (defined $self->_resigned) { | ||
| 248 | $self->_resigned(undef); | ||
| 249 | return 1; | ||
| 250 | } | ||
| 251 | my $choice = pop @{ $self->_choices } or return 0; | ||
| 252 | my $game = $self->_game; | ||
| 253 | $game->undo while $game->depth > $choice->{depth}; | ||
| 254 | splice @{ $self->_log }, $choice->{log}; | ||
| 255 | $self->_rolls($choice->{rolls}); | ||
| 256 | $self->_roll($choice->{roll}); | ||
| 257 | $self->_faces($choice->{faces}); | ||
| 258 | $self->_number($choice->{number}); | ||
| 259 | return 1; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub resign { | ||
| 263 | my ($self, $side) = @_; | ||
| 264 | $self->_error(undef); | ||
| 265 | return 0 if $self->is_over; | ||
| 266 | $side = $self->_game->side unless defined $side; | ||
| 267 | Carp::croak("Game::RoyalUr: a side is 'light' or 'dark'") unless exists $SIDE_OF{$side}; | ||
| 268 | $self->_resigned($side); | ||
| 269 | return 1; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub is_over { defined $_[0]->_resigned || $_[0]->_game->is_over ? 1 : 0 } | ||
| 273 | |||
| 274 | sub result { | ||
| 275 | my ($self) = @_; | ||
| 276 | return undef unless $self->is_over; | ||
| 277 | my $game = $self->_game; | ||
| 278 | my $resigned = $self->_resigned; | ||
| 279 | return Game::RoyalUr::Result->new( | ||
| 280 | winner => (defined $resigned ? $OTHER{$resigned} : $game->winner), | ||
| 281 | how => (defined $resigned ? 'resign' : $game->how), | ||
| 282 | final => $game->position, | ||
| 283 | home => { light => $game->home('light'), dark => $game->home('dark') }, | ||
| 284 | plies => $game->ply, | ||
| 285 | ); | ||
| 286 | } | ||
| 287 | |||
| 288 | sub error { $_[0]->_error } | ||
| 289 | |||
| 290 | sub side { $_[0]->is_over ? undef : $_[0]->_game->side } | ||
| 291 | |||
| 292 | sub roll { $_[0]->is_over ? undef : $_[0]->_roll } | ||
| 293 | |||
| 294 | sub throw { | ||
| 295 | my ($self) = @_; | ||
| 296 | my $faces = $self->is_over ? undef : $self->_faces; | ||
| 297 | return defined $faces ? [ split //, $faces ] : undef; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub legal { | ||
| 301 | my ($self) = @_; | ||
| 302 | my $roll = $self->roll; | ||
| 303 | return wantarray ? () : 0 unless defined $roll; | ||
| 304 | return $self->_game->moves($roll); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub variant { $_[0]->_variant } | ||
| 308 | |||
| 309 | sub first { $_[0]->_first } | ||
| 310 | |||
| 311 | sub seed { $_[0]->_seed } | ||
| 312 | |||
| 313 | sub rolls { $_[0]->_rolls } | ||
| 314 | |||
| 315 | sub opening { [ map { [ split //, $_ ] } @{ $_[0]->_opening } ] } | ||
| 316 | |||
| 317 | sub ply { $_[0]->_game->ply } | ||
| 318 | |||
| 319 | sub position { $_[0]->_game->position } | ||
| 320 | |||
| 321 | sub key { $_[0]->_game->key } | ||
| 322 | |||
| 323 | sub hand { $_[0]->_game->hand($_[1]) } | ||
| 324 | |||
| 325 | sub home { $_[0]->_game->home($_[1]) } | ||
| 326 | |||
| 327 | sub at { | ||
| 328 | my ($self, $name) = @_; | ||
| 329 | my $cell = $cell_named->($name); | ||
| 330 | Carp::croak("Game::RoyalUr: there is no square called '" . ($name // 'undef') . "'") if $cell < 0; | ||
| 331 | my $piece = $self->_game->board->at($cell); | ||
| 332 | return $piece == EMPTY ? undef : $piece == piece_of(SIDE_LIGHT) ? 'light' : 'dark'; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub chances { | ||
| 336 | my ($self) = @_; | ||
| 337 | my $variant = $self->_variant; | ||
| 338 | return $E->chances($variant->dice, $variant->zero_rolls); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub log { [ map { { %$_ } } @{ $_[0]->_log } ] } | ||
| 342 | |||
| 343 | sub forfeits_since { | ||
| 344 | my ($self, $ply) = @_; | ||
| 345 | $ply = 0 unless defined $ply; | ||
| 346 | return map { { %$_ } } grep { !defined $_->{move} && $_->{ply} >= $ply } @{ $self->_log }; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub to_record { | ||
| 350 | my ($self) = @_; | ||
| 351 | my $variant = $self->_variant; | ||
| 352 | my %record = ( | ||
| 353 | rules => (defined $variant->name ? $variant->name : $variant->as_hash), | ||
| 354 | first => $self->_first, | ||
| 355 | (defined $self->_position_given ? (position => $self->_position_given) : ()), | ||
| 356 | turns => [ map { { | ||
| 357 | side => $_->{side}, roll => $_->{roll}, move => $_->{move}, | ||
| 358 | (defined $_->{faces} ? (faces => $_->{faces}) : ()), | ||
| 359 | } } @{ $self->_log } ], | ||
| 360 | ); | ||
| 361 | if (defined $self->_seed) { | ||
| 362 | $record{seed} = unpack 'H*', $self->_seed; | ||
| 363 | $record{opening} = scalar @{ $self->_opening } if @{ $self->_opening }; | ||
| 364 | } | ||
| 365 | if (my $result = $self->result) { | ||
| 366 | $record{result} = { winner => (defined $result->winner ? $result->winner : 'draw'), how => $result->how }; | ||
| 367 | } | ||
| 368 | return Game::RoyalUr::Notation::format_record(\%record); | ||
| 369 | } | ||
| 370 | |||
| 371 | sub replay { | ||
| 372 | my ($class, $record, %option) = @_; | ||
| 373 | my $bad = sub { return (undef, $X->of('bad_record', @_)) }; | ||
| 374 | |||
| 375 | if (!ref $record) { | ||
| 376 | my ($parsed, $problem) = Game::RoyalUr::Notation::parse_record($record); | ||
| 377 | return $bad->(line => $problem->{line}, why => $problem->{error}) unless $parsed; | ||
| 378 | $record = $parsed; | ||
| 379 | } | ||
| 380 | my @turns = @{ $record->{turns} || [] }; | ||
| 381 | my $opening = $record->{opening} || 0; | ||
| 382 | my $rules = exists $option{rules} ? $option{rules} : $record->{rules}; | ||
| 383 | |||
| 384 | my $game; | ||
| 385 | if (defined $record->{seed}) { | ||
| 386 | $game = $class->new( | ||
| 387 | seed => pack('H*', $record->{seed}), | ||
| 388 | rules => $rules, | ||
| 389 | (defined $record->{position} ? (position => $record->{position}) : ()), | ||
| 390 | ($opening ? () : (first => $record->{first})), | ||
| 391 | ); | ||
| 392 | return $bad->(ply => 0, why => 'opening') | ||
| 393 | unless $game->first eq $record->{first} && @{ $game->opening } == $opening; | ||
| 394 | } | ||
| 395 | else { | ||
| 396 | $game = $class->new( | ||
| 397 | script => [ map { { roll => $_->{roll}, faces => $_->{faces} } } @turns ], | ||
| 398 | rules => $rules, | ||
| 399 | (defined $record->{position} ? (position => $record->{position}) : ()), | ||
| 400 | first => $record->{first}, | ||
| 401 | ); | ||
| 402 | } | ||
| 403 | |||
| 404 | for my $i (0 .. $#turns) { | ||
| 405 | my $turn = $turns[$i]; | ||
| 406 | my $made = $game->_log->[$i]; | ||
| 407 | if ($made) { | ||
| 408 | return $bad->(ply => $i, why => 'move') if defined $turn->{move} || defined $made->{move}; | ||
| 409 | return $bad->(ply => $i, why => 'side') unless $made->{side} eq $turn->{side}; | ||
| 410 | return $bad->(ply => $i, why => 'roll') unless $made->{roll} == $turn->{roll}; | ||
| 411 | next; | ||
| 412 | } | ||
| 413 | return $bad->(ply => $i, why => 'over') if $game->is_over; | ||
| 414 | return $bad->(ply => $i, why => 'forfeit') unless defined $turn->{move}; | ||
| 415 | return $bad->(ply => $i, why => 'side') unless $game->side eq $turn->{side}; | ||
| 416 | return $bad->(ply => $i, why => 'roll') unless defined $game->roll && $game->roll == $turn->{roll}; | ||
| 417 | unless ($game->play($turn->{move})) { | ||
| 418 | my $error = $game->error; | ||
| 419 | return (undef, $X->of($error->code, %{ $error->detail }, ply => $i)); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | if (my $result = $record->{result}) { | ||
| 424 | if ($result->{how} eq 'resign') { | ||
| 425 | return $bad->(ply => scalar @turns, why => 'result') if $game->is_over; | ||
| 426 | $game->resign($OTHER{ $result->{winner} }); | ||
| 427 | } | ||
| 428 | else { | ||
| 429 | my $ended = $game->result; | ||
| 430 | my $winner = $ended && defined $ended->winner ? $ended->winner : 'draw'; | ||
| 431 | return $bad->(ply => scalar @turns, why => 'result') | ||
| 432 | unless $ended && $ended->how eq $result->{how} && $winner eq $result->{winner}; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | return ($game, undef); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub to_text { $_[0]->to_record } | ||
| 439 | |||
| 440 | sub from_text { | ||
| 441 | my ($class, $text) = @_; | ||
| 442 | Carp::croak('Game::RoyalUr: from_text wants the text to_text wrote, and was given ' | ||
| 443 | . (defined $text ? 'a reference' : 'nothing')) | ||
| 444 | if !defined $text || ref $text; | ||
| 445 | my ($game, $error) = $class->replay($text); | ||
| 446 | return $game if $game; | ||
| 447 | my $detail = $error->detail; | ||
| 448 | my $where = $detail->{line} ? " at line $detail->{line}" : defined $detail->{line} ? '' : ' at turn ' . (($detail->{ply} // 0) + 1); | ||
| 449 | Carp::croak('Game::RoyalUr: the text was refused' . $where . ': ' . $error->message | ||
| 450 | . (defined $detail->{why} ? " ($detail->{why})" : '')); | ||
| 451 | } | ||
| 452 | |||
| 453 | 1; | ||
| 454 | |||
| 455 | __END__ | ||