Game-Terminal

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

Changes
bin/game
MANIFEST
Makefile.PL
README
ignore.txt
lib/Game/Terminal.pm
lib/Game/Terminal/Menu.pm
t/00-load.t
t/01-roster.t
t/02-find.t
t/03-no-io.t
t/04-installed.t
t/05-launch.t
t/06-keys.t
t/07-menu.t
t/08-bin-game.t
t/09-configure.t

bin/game  view on Meta::CPAN

		print STDERR usage();
		return 2;
	}
	if ($opt{version}) { print "game, Game::Terminal $Game::Terminal::VERSION\n"; return 0 }
	if ($opt{help})    { print usage(); return 0 }

	my %common = (ascii => $opt{ascii} ? 1 : 0, colour => $opt{colour}, pick => $opt{pick});
	my $word = shift @argv;

	if (!defined $word) {
		require Game::Terminal::Menu;
		return Game::Terminal::Menu->new(common => \%common)->start;
	}
	if ($word eq 'list') { return list() }
	if ($word eq 'help') {
		$word = shift @argv;
		if (!defined $word) { print usage(); return 0 }
		@argv = ('--help');
	}

	my $row = Game::Terminal::find($word);
	if (!$row) {

bin/game  view on Meta::CPAN

    game                        # the menu: arrows, enter, q
    game checkers --level 3     # the same as: checkers --level 3
    game sixtysix               # Schnapsen in the Sixty-Six variant
    game list
    game help oware
    game --no-colour --no-pick durak

=head1 DESCRIPTION

Lists the terminal games of the C<Game> namespace and L<Term::Maze>, and
starts any of them. With no arguments it draws a menu (L<Game::Terminal::Menu>): choose a game
with the arrow keys, set its options on the screen that follows (the level,
your side, a variant, a seed, or anything else as on the command line), and
enter on Play starts it. The menu is drawn again when the game ends, with the
options still set. With a name it starts that game at once, inside this process,
through the game's own C<run> method, and everything after the name is the
game's own command line.

C<--ascii> and C<--no-pick> are spelt in each game's words before being
passed on; C<--no-colour> sets C<NO_COLOR> for the game, since two of the
games use C<--colour> to mean a side. They are read only before the name.

=head1 EXIT STATUS

    the game's own: 0 won, drew or quit, 1 lost, 2 a bad option
    2   a bad option to game itself, or a name that is no game
    3   the game died, with the message on stderr

=head1 SEE ALSO

L<Game::Terminal>, L<Game::Terminal::Menu>

=cut

lib/Game/Terminal.pm  view on Meta::CPAN


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).

lib/Game/Terminal.pm  view on Meta::CPAN

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> >>

lib/Game/Terminal/Menu.pm  view on Meta::CPAN

package Game::Terminal::Menu;

use 5.014;
use strict;
use warnings;

use Object::Proto::Sugar -types;
use Game::Terminal;
use Game::Terminal::Kit::Form;
use Game::Terminal::Kit::Keys;
use Game::Terminal::Kit::Log;

lib/Game/Terminal/Menu.pm  view on Meta::CPAN

	die $error unless $ok;
	return 0;
}

1;

__END__

=head1 NAME

Game::Terminal::Menu - the menu the game command draws

=head1 VERSION

Version 0.02

=head1 SYNOPSIS

    use Game::Terminal::Menu;

    exit Game::Terminal::Menu->new->start;

    my $menu = Game::Terminal::Menu->new(
        common    => { ascii => 1, colour => 0 },
        keysource => sub { shift @keys },
        runner    => sub { my ($row, $argv, %seam) = @_; ... ; return (0, undef) },
    );

=head1 DESCRIPTION

The roster of L<Game::Terminal> as a list on the screen: a cursor, the
highlighted game's class and version on a line underneath, the result of the
last game, and a legend. Enter opens the game under the cursor's options on a second screen

t/00-load.t  view on Meta::CPAN

#!perl
use 5.014;
use strict;
use warnings;
use Test::More;

my @modules = qw/
	Game::Terminal
	Game::Terminal::Menu
/;

plan tests => scalar(@modules) + 1;

use_ok($_) for @modules;

my %version;
no strict 'refs';
$version{${"${_}::VERSION"}}++ for @modules;
use strict 'refs';

t/03-no-io.t  view on Meta::CPAN

use warnings;
use lib 't/lib';
use Test::More;

use Game::Terminal;
use Game::Terminal::Test::Handle;

subtest 'the roster does not pull the menu or a game in with it' => sub {
	plan tests => 3;
	ok $INC{'Game/Terminal.pm'}, 'the roster is loaded';
	ok !$INC{'Game/Terminal/Menu.pm'}, 'the menu is not';
	is_deeply [ grep { m{^(?:Game/(?!Terminal)|Term/Maze)} } sort keys %INC ], [],
		'and no game class is';
};

subtest 'the roster with both handles taken away' => sub {
	plan tests => 2;
	my $error;
	my $found = 0;
	{
		tie *STDOUT, 'Game::Terminal::Test::Handle';

t/06-keys.t  view on Meta::CPAN

#!perl
use 5.014;
use strict;
use warnings;
use Test::More;

use Game::Terminal::Menu;

sub menu {
	my ($keys, %option) = @_;
	my @chars = split //, defined $keys ? $keys : '';
	my $output = '';
	open my $out, '>', \$output or die "in memory handle: $!";
	my $menu = Game::Terminal::Menu->new(
		out => $out,
		interactive => 0,
		colour => 0,
		keysource => sub { shift @chars },
		picking => 1,
		%option
	);
	return ($menu, \$output, \@chars);
}

t/07-menu.t  view on Meta::CPAN

#!perl
use 5.014;
use strict;
use warnings;
use Test::More;

use Game::Terminal;
use Game::Terminal::Menu;

my @ALL = Game::Terminal::games();

sub menu {
	my ($keys, %option) = @_;
	my @chars = split //, defined $keys ? $keys : '';
	my $output = '';
	open my $out, '>', \$output or die "in memory handle: $!";
	my @ran;
	my $menu = Game::Terminal::Menu->new(
		out => $out,
		interactive => 0,
		colour => 0,
		keysource => sub { shift @chars },
		picking => 1,
		games => [ map { { %{$_}, installed => 1 } } @ALL ],
		runner => sub { my ($row, $argv, %seam) = @_; push @ran, [ $row->{key}, @{$argv} ]; return (0, undef) },
		%option
	);
	return ($menu, \$output, \@ran);

t/07-menu.t  view on Meta::CPAN

	$menu->at($#ALL);
	like $menu->list_lines->[0], qr/further up/, 'and at the end the head says how many are above';
};

subtest 'the prompt, without keys' => sub {
	plan tests => 5;
	my @ran;
	my $output = '';
	open my $out, '>', \$output or die $!;
	open my $in, '<', \"3\n\nchekers\nCheckers\n\n\n" or die $!;
	my $menu = Game::Terminal::Menu->new(
		in => $in, out => $out, interactive => 0, colour => 0, picking => 0,
		games => [ map { { %{$_}, installed => 1 } } @ALL ],
		runner => sub { push @ran, $_[0]{key}; return (0, undef) },
	);
	is $menu->start, 0, 'a blank line leaves';
	is_deeply \@ran, ['checkers', 'checkers'], 'row 3 and the name both played checkers, each with a blank options line';
	like $output, qr/Which game\? \(number, name, or q\)/, 'the prompt was asked';
	like $output, qr/No game called 'chekers'\. Did you mean checkers\?/, 'a typo was corrected';
	unlike $output, qr/\e\[/, 'and nothing was an escape code';
};

t/09-configure.t  view on Meta::CPAN

#!perl
use 5.014;
use strict;
use warnings;
use Test::More;

use Game::Terminal;
use Game::Terminal::Menu;

my @ALL = Game::Terminal::games();

my %FIELDS = (
	checkers => [
		{ name => 'level', type => 'int', min => 1, max => 5, help => 'the strength of the bot' },
		{ name => 'side', type => 'enum', choices => [qw(black white random)], help => 'your side; black moves first' },
		{ name => 'hotseat', type => 'flag', help => 'two people at one keyboard, no bot' },
		{ name => 'bot-vs-bot', type => 'flag', help => 'watch two bots play' },
		{ name => 'seed', type => 'str', help => 'the bot varies its easy levels with it' },

t/09-configure.t  view on Meta::CPAN

		{ name => 'seed', type => 'str', help => 'the same seed, the same game' },
	],
);

sub menu {
	my ($keys, %option) = @_;
	my @chars = split //, defined $keys ? $keys : '';
	my $output = '';
	open my $out, '>', \$output or die "in memory handle: $!";
	my @ran;
	my $menu = Game::Terminal::Menu->new(
		out => $out,
		interactive => 0,
		colour => 0,
		keysource => sub { shift @chars },
		picking => 1,
		games => [ map { { %{$_}, installed => 1, options => $FIELDS{ $_->{key} } || [] } } @ALL ],
		runner => sub { my ($row, $argv, %seam) = @_; push @ran, [ $row->{key}, @{$argv} ]; return (0, undef) },
		%option
	);
	return ($menu, \$output, \@ran);

t/09-configure.t  view on Meta::CPAN

	is_deeply $ran, [['cribbage']], 'cribbage played';
	like ${$output}, qr/extra\s+\(none\)/, 'with an extra line offered';
};

subtest 'the prompt without keys asks for an options line' => sub {
	plan tests => 3;
	my @ran;
	my $output = '';
	open my $out, '>', \$output or die $!;
	open my $in, '<', \"3\n--level 2 --seed \"x y\"\n3\n\n\n" or die $!;
	my $menu = Game::Terminal::Menu->new(
		in => $in, out => $out, interactive => 0, colour => 0, picking => 0,
		games => [ map { { %{$_}, installed => 1 } } @ALL ],
		runner => sub { my ($row, $argv) = @_; push @ran, [ $row->{key}, @{$argv} ]; return (0, undef) },
	);
	is $menu->start, 0, 'a blank line at the game prompt leaves';
	is_deeply \@ran, [['checkers', '--level', 2, '--seed', 'x y'], ['checkers']],
		'the options line is split like a shell, and blank means none';
	like $output, qr/Options for checkers, as on the command line/, 'the line was asked for';
};



( run in 1.145 second using v1.01-cache-2.11-cpan-60884a8df76 )