Commits

Anonymous committed 6af6ef3

Added the is_wall() assertion and now populating the board with data.

  • Participants
  • Parent commits c5e0856

Comments (0)

Files changed (2)

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

 use Object::Tiny qw/
     height
     width
+    _data
 /;
 
+my $wall_bits = 2;
+my $dest_place_bits = 2;
+
+=head1 METHODS
+
+=head2 load($board)
+
+Loads a board in standard Sokoban notation.
+
+=cut
+
 sub load
 {
     my ($pkg, $contents) = @_;
 
     my @lines = (map { [ split(//, $_) ] } split(/\n/, $contents));
 
+    my $data = "";
+
     my $self = 
         $pkg->new(
             height => scalar(@lines),
             width => max(map { scalar(@$_) } @lines),
+            _data => \$data,
         );
 
+    foreach my $y (0 .. $#lines)
+    {
+        my $l = $lines[$y];
+        foreach my $x (0 .. $#$l)
+        {
+            my $offset = $y*$self->width()+$x;
+            if ($l->[$x] eq "#")
+            {
+                vec(${$self->_data()}, $offset, 2) = $wall_bits;
+            }
+            else
+            {
+                vec(${$self->_data()}, $offset, 2) = 0;
+            }
+        }
+    }
+
     return $self;
 }
 
-=head1 METHODS
+=head2 $board->is_wall($x,$y)
+
+Returns if the block at the position $x,$y is a wall.
+
+=cut
+
+sub is_wall
+{
+    my ($self, $x, $y) = @_;
+
+    return (vec(${$self->_data()}, $y*$self->width()+$x, 2) == $wall_bits);
+}
 
 =head2 width()
 
 
 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 Test::More tests => 3;
 
 use Shlomif::Sokoban::Solver::Board;
 
     is ($board->width(), 9, "Testing the board width");
     # TEST
     is ($board->height(), 9, "Testing the board height");
+
+    # TEST
+    ok ($board->is_wall(2, 0), "Testing if 2,0 is a wall");
 }