Commits

(no ...@ddf91b2d-1462-4000-aae5-8a08ddc4854f  committed c5e0856

Implemented ->width() and height().

  • Participants
  • Parent commits a12596d

Comments (0)

Files changed (4)

File modules/Shlomif-Sokoban-Solver/Build.PL

     dist_version_from   => 'lib/Shlomif/Sokoban/Solver.pm',
     build_requires => {
         'Test::More' => 0,
+        'Object::Tiny' => 0,
     },
     add_to_cleanup      => [ 'Shlomif-Sokoban-Solver-*' ],
     create_makefile_pl => 'traditional',

File modules/Shlomif-Sokoban-Solver/MANIFEST

 Build.PL
 Changes
+lib/Shlomif/Sokoban/Solver/Board.pm
+lib/Shlomif/Sokoban/Solver.pm
 MANIFEST
 README
-lib/Shlomif/Sokoban/Solver.pm
 t/00-load.t
+t/01-loading.t
 t/pod-coverage.t
 t/pod.t

File modules/Shlomif-Sokoban-Solver/lib/Shlomif/Sokoban/Solver/Board.pm

+package Shlomif::Sokoban::Solver::Board;
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+Shlomif::Sokoban::Solver::Board - a board for the sokosolver.
+
+=head1 SYNOPSIS
+
+For internal use by the Sokoban solver. See the test files.
+
+=cut
+
+use List::Util qw(max);
+
+use Object::Tiny qw/
+    height
+    width
+/;
+
+sub load
+{
+    my ($pkg, $contents) = @_;
+
+    # Remove trailing whitespace.
+    $contents =~ s{(\s*\n)+\z}{}ms; 
+
+    # Remove trailing whitespace from lines.
+    $contents =~ s{\s+$}{}gms;
+
+    my @lines = (map { [ split(//, $_) ] } split(/\n/, $contents));
+
+    my $self = 
+        $pkg->new(
+            height => scalar(@lines),
+            width => max(map { scalar(@$_) } @lines),
+        );
+
+    return $self;
+}
+
+=head1 METHODS
+
+=head2 width()
+
+Returns the width of the board.
+
+=head2 height()
+
+Returns the height of the board.
+
+=head2 load($board)
+
+Loads a board in standard Sokoban notation.
+
+=cut
+
+1;
+

File modules/Shlomif-Sokoban-Solver/t/01-loading.t

+use strict;
+use warnings;
+
+use Test::More tests => 2;
+
+use Shlomif::Sokoban::Solver::Board;
+
+my $board_contents = <<'EOF';
+  ####
+  #  #
+  #  ####
+###$.$  #
+#  .@.  #
+#  $.$###
+####  #
+   #  #
+   ####
+EOF
+
+{
+    my $board = Shlomif::Sokoban::Solver::Board->load($board_contents);
+
+    # TEST
+    is ($board->width(), 9, "Testing the board width");
+    # TEST
+    is ($board->height(), 9, "Testing the board height");
+}