Game-Terminal
view release on metacpan or search on metacpan
lib/Game/Terminal.pm view on Meta::CPAN
my $flush = do { select $out; my $was = $|; select $selected; $was };
my ($status, $error);
my $ok = eval {
my $class = $loader->($row);
$status = $class->run(@argv);
1;
};
unless ($ok) {
$error = $@;
$error = 'died without a message' unless defined $error && length $error;
$error =~ s/ at \S+ line \d+\.?\s*\z//;
$error =~ s/\s+\z//;
}
select $selected;
{ select $out; $| = $flush; select $selected }
binmode $out, ':pop' while (() = PerlIO::get_layers($out)) > $depth;
Getopt::Long::Configure($getopt);
eval { Term::ReadKey::ReadMode(0) } if $INC{'Term/ReadKey.pm'};
return ($status, $error);
}
1;
__END__
=head1 NAME
Game::Terminal - one game command to rule all terminal games
=head1 VERSION
Version 0.02
=head1 SYNOPSIS
game # the menu
game checkers --level 3 # the same as: checkers --level 3
game sixtysix # Schnapsen, in the Sixty-Six variant
game list
game help oware
use Game::Terminal;
my $row = Game::Terminal::find('draughts'); # the checkers row
my ($status, $error) = Game::Terminal::launch($row, ['--level', 1]);
=head1 DESCRIPTION
A roster of the terminal games in the C<Game> namespace, L<Term::Maze> and
L<Term::Chess>,
with one command, C<game>, that lists them and starts any of them. A game is
started B<inside the same process>: its Terminal class's C<run> method takes
the command line and returns the exit status, so C<game checkers --level 3>
is C<checkers --level 3> with nothing between them, and the menu is back when
the game ends.
This module is the roster and the functions over it, and does no terminal
input or output of its own; the menu is L<Game::Terminal::Menu> and the
command is F<bin/game>.
=head2 What a game promises
Every class in the roster has a class method C<run(@argv)> that returns 0
when you won, drew or quit, 1 when you lost and 2 for a bad option, and never
calls C<exit>. C<--help> prints to C<STDOUT> and returns 0; a bad option
prints the usage to C<STDERR> and returns 2. Colour is off when C<NO_COLOR>
is set. C<launch> relies on nothing else, and guards against a game that
forgets the rest (see below).
=head2 The roster
Twenty-four rows for twenty-three games: Schnapsen is two rows, C<schnapsen> and
C<sixtysix>, because its program needs to be told the variant. Each row has
a C<key> (the word you type), the C<module> to install and the C<class> to
call, a one-line C<blurb>, a C<family> (board, card, tile, puzzle or arcade), the
number of C<players>, the C<args> the launcher puts before yours, the
C<aliases> that also find it, and the C<flags> that spell the launcher's own
C<--ascii> and C<--no-pick> in that game's words.
The aliases: C<draughts> is checkers, C<othello> is reversi, C<ur> is royalur,
C<morris> and C<mills> are merrills, C<tafl> is brandubh, C<weiqi> and
C<baduk> are go, C<chinesechess> is xiangqi, C<japanesechess> is shogi, C<chinesecheckers> is sternhalma, C<awale> and C<awari> are oware,
C<rummy> is gin, C<crib> is cribbage, C<flappy> is floppy, and C<66> is
sixtysix.
=head2 Where the options come from
The settings a person chooses before a game (the level, a side or seat, a
mode, a variant, a board size, a seed, the switches that turn a game into
hotseat play or a bot watching itself) are the game's own to list. Each
class in the roster has a class method C<options> that returns them as
fields, and L</options_for> asks it. A field is a hash of C<name>, C<type>
and C<help>, with C<min> and C<max> for an C<int> and C<choices> for an
C<enum>; L<Game::Terminal::Kit::Options> describes them in full. File paths,
replay records and layout widths are not among them; they stay on the
command line, and the menu's C<extra> line takes them.
The launcher never repeats a game's defaults: a field nobody set sends
nothing, so the game decides.
=head1 FUNCTIONS
=head2 games
The rows, in order.
=head2 names
The keys, in order.
=head2 normalise
Lowercases a name, drops a leading C<Game::> or C<game-> and every character
that is not a letter or a digit, so C<Game::Checkers>, C<game-checkers> and
C<CHECKERS> all become C<checkers>.
=head2 find
lib/Game/Terminal.pm view on Meta::CPAN
The launcher's common options spelt in the row's words: C<--ascii> becomes
C<--no-unicode> for brandubh and fanorona and nothing for maze, C<--no-pick>
is C<--no-pick> everywhere but maze, snake and floppy, which are played in real
time. Colour is not a flag: see C<no_colour>
under L</launch>.
=head2 step_value
my $next = Game::Terminal::step_value($field, $current, +1);
L<Game::Terminal::Kit::Options/step_value>, under the name it has always had
here.
The value one step to the right (C<+1>) or left (C<-1>) of C<$current> for
a field: an C<int> moves by one and stops at its ends, and from unset goes
to C<min> or C<max>; an C<enum> cycles through its choices; a C<flag>
toggles; a C<bool> cycles through unset, on and off. C<str> fields do not
step.
=head2 check_value
my ($value, $error) = Game::Terminal::check_value($field, $typed);
L<Game::Terminal::Kit::Options/check_value>, likewise.
A typed value for a field: the number, the matching choice regardless of
case, or the text, with C<$error> set and C<$value> undef when an C<int> is
out of range or an C<enum> is not a choice. Blank is unset.
=head2 argv_for
my @argv = Game::Terminal::argv_for($row, \%values);
The command line the values make for the row's fields, in the order
L</options_for> gives them, with the C<extra> text split like a shell and
appended last.
=head2 launch
my ($status, $error) = Game::Terminal::launch($row, \@argv, %seam);
Loads the row's class and calls C<run> with the row's C<args> followed by
C<@argv>, and returns the status it gave. If C<run> died, C<$status> is
undef and C<$error> is the message without the C<at FILE line N> tail.
Around the call, so that a game leaves the process as it found it for the
next one: the Getopt::Long configuration is saved and put back; the
C<INT>, C<TERM> and C<HUP> handlers are local; every layer the game pushes
onto the output handle is popped again, since a second game pushing its own
encoding layer on top of the first would double-encode; the handle's
autoflush and the selected handle are restored; and if L<Term::ReadKey> is
loaded the terminal is put back in cooked mode, for a game that died before
its own C<leave_raw>.
C<%seam> takes C<loader> (a code ref returning the class, in place of
L</load>), C<out> (the handle whose layers are guarded, C<STDOUT> by default)
and C<no_colour> (true sets C<NO_COLOR> for the duration of the call).
=head1 SEE ALSO
L<Game::Terminal::Menu>, and the games: L<Game::Backgammon>,
L<Game::Brandubh>, L<Game::Checkers>, L<Game::Cribbage>, L<Game::Dominoes>,
L<Game::Durak>, L<Game::Fanorona>, L<Game::Floppy>, L<Game::Gin>, L<Game::Go>,
L<Game::Mahjong>, L<Game::Merrills>, L<Game::Oware>, L<Game::Reversi>,
L<Game::RoyalUr>, L<Game::Schnapsen>, L<Game::Senet>, L<Game::Shogi>, L<Game::Snake>, L<Game::Sternhalma>, L<Game::Xiangqi>,
L<Term::Maze>.
=head1 AUTHOR
LNATION, C<< <email at lnation.org> >>
=head1 LICENSE AND COPYRIGHT
This software is Copyright (c) 2026 by LNATION.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut
( run in 0.856 second using v1.01-cache-2.11-cpan-60884a8df76 )