| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | Games::2048 - An ASCII clone of the 2048 game | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | use Games::2048; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | Games::2048->new->run; | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | This module is a full clone of the L<2048 game by Gabriele Cirulli|http://gabrielecirulli.github.io/2048/>. It runs at the command-line, complete with controls identical to the original, a colorful interface, and even some text-based animations! It should work on Linux, Mac, and Windows. | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | Once installed, run the game with the command: | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2048 | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =item * Add slide and merge animations | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =item * Add button to toggle animations on/off | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =item * Add buttons to zoom the board in and out | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =item * Add colors for 256-color terminals | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =item * Abstract input system to allow for AI or replay input | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =item * Test on more systems and terminals | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | Blaise Roth <blaizer@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | This software is Copyright (C) 2014 by Blaise Roth. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | This is free software; you can redistribute and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 45 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | package Games::2048; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | use 5.012; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | use constant { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | FRAME_TIME => 1/15, | |||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | use Games::2048::Input; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | ▶ | use Games::2048::Animation; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | use Games::2048::Tile; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | ▶ | use Games::2048::Grid; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 4 | ▶ | use Games::2048::Board; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | use Games::2048::Game; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | has size => is => 'ro', default => 4; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | has start_tiles => is => 'ro', default => 2; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | has best_score => is => 'rw', default => 0; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | sub run { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=26 SCAR=65.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $quit; | |||||||||||||||||||||||||||||||||||||||||
| 78 | my $game; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $first_time = 1; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | Games::2048::Input::update_window_size; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | while (!$quit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($first_time and $game = Games::2048::Game->restore) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$first_time and $game = "Games::2048::Game"->restore
MC/DC0%0/2
$first_time and $game = "Games::2048::Game"->restore
$first_time
$game = "Games::2048::Game"->restore
Truth table0%0/3
A and B
A= $first_timeB= $game = "Games::2048::Game"->restore
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->update_best_score($game); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | undef $game if $game->lose or !$game->is_valid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$game->lose or not $game->is_valid
MC/DC0%0/2
$game->lose or not $game->is_valid
$game->lose
not $game->is_valid
Truth table0%0/3
A or not(B)
A= $game->loseB= not $game->is_valid
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | undef $game; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (!$game) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $game = Games::2048::Game->new( | |||||||||||||||||||||||||||||||||||||||||
| 92 | size => $self->size, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | best_score => $self->best_score, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $game->insert_start_tiles($self->start_tiles); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ($first_time) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $first_time = 0; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $game->draw_welcome; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | RUN: $game->draw; | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $restart; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $time = Time::HiRes::time; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | PLAY: while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (!$game->lose and !$game->win) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$game->lose or $game->win
MC/DC0%0/2
$game->lose or $game->win
$game->lose
$game->win
Truth table0%0/3
A or B
A= $game->loseB= $game->win
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | while (defined(my $key = Games::2048::Input::read_key)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $vec = Games::2048::Input::key_vector($key); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($vec) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $game->move($vec); | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | elsif ($key =~ /^[q]$/i) { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $quit = 1; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | last PLAY; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | elsif ($key =~ /^[r]$/i) { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $restart = 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | last PLAY; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $game->draw(1); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (!$game->needs_redraw and $game->lose || $game->win) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $game->needs_redraw and $game->lose || $game->win
$game->lose or $game->win
MC/DC0%0/3
not $game->needs_redraw and $game->lose || $game->win
not $game->needs_redraw
$game->lose
$game->win
Truth table0%0/4
not(A) and B or C
A= not $game->needs_redrawB= $game->loseC= $game->win
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | last PLAY; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my $new_time = Time::HiRes::time; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $delta_time = $new_time - $time; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my $delay = FRAME_TIME - $delta_time; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | $time = $new_time; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($delay > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | Time::HiRes::sleep($delay); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | $time += $delay; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | $game->draw_win; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $self->update_best_score($game); | |||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (!$quit and !$restart) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$quit or $restart
MC/DC0%0/2
$quit or $restart
$quit
$restart
Truth table0%0/3
A or B
A= $quitB= $restart
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | print $game->win ? "Keep going?" : "Try again?", " (Y/n) "; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | STDOUT->flush; | |||||||||||||||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | my $key = Games::2048::Input::poll_key; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($key =~ /^[yn]$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | print $key; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($key =~ /^[nq]$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $quit = 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | elsif ($key =~ /^[yr\n]$/i) { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | say ""; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | redo; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | say ""; | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if ($game->win) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $game->win(0); | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | goto RUN if !$quit; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $game->save; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | sub update_best_score { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | my ($self, $game) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_best_score: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if (defined $game->best_score and $game->best_score > $self->best_score) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $game->best_score and $game->best_score > $self->best_score
MC/DC0%0/2
defined $game->best_score and $game->best_score > $self->best_score
defined $game->best_score
$game->best_score > $self->best_score
Truth table0%0/3
A and B
A= defined $game->best_scoreB= $game->best_score > $self->best_score
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | $self->best_score($game->best_score); | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | $game->best_score($self->best_score); | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||