| 1 | package Karel::UI::Text::Robot; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Karel::UI::Text::Robot | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | The role used by L<Karel::UI::Text> to introduce methods to interact | ||
| 10 | with the user and show the grid. | ||
| 11 | |||
| 12 | =head1 METHODS | ||
| 13 | |||
| 14 | =over 4 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | use feature qw{ say }; | ||
| 21 | |||
| 22 | use Cwd; | ||
| 23 | |||
| 24 | use Moo::Role; | ||
| 25 | requires qw{ grid x y direction cover run step is_running knowledge }; | ||
| 26 | |||
| 27 | |||
| 28 | =item cls | ||
| 29 | |||
| 30 | Clears the screen. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub cls { | ||
| 35 | system 'MSWin32' eq $^O ? 'cls' : 'clear'; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | =item $robot->menu($default, [ [ $command => \&callback ], ... ]) | ||
| 40 | |||
| 41 | Display a menu listing commands, run the callback associated with the | ||
| 42 | selected item. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub menu { | ||
| 47 | my ($robot, $default, $choices) = @_; | ||
| 48 | my $reply = ' '; | ||
| 49 | until ($reply =~ /^[0-9]*$/ && $reply && $reply <= @$choices) { | ||
| 50 | my $i = 1; | ||
| 51 | my $line_length = @$choices > 20 ? 0 : undef; | ||
| 52 | for my $choice (@$choices) { | ||
| 53 | if (defined $line_length) { | ||
| 54 | my $item = $i++ . ") $choice->[0] "; | ||
| 55 | if ($line_length + length $item > 80) { | ||
| 56 | print "\n"; | ||
| 57 | $line_length = 0; | ||
| 58 | } | ||
| 59 | print $item; | ||
| 60 | $line_length += length $item; | ||
| 61 | |||
| 62 | } else { | ||
| 63 | say $i++, ") $choice->[0]"; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | print "\n" if defined $line_length; | ||
| 67 | say "Default: $default" if defined $default; | ||
| 68 | chomp( $reply = <> ); | ||
| 69 | $reply = $default if q() eq $reply && $default; | ||
| 70 | } | ||
| 71 | my $action = $choices->[$reply-1][1]; | ||
| 72 | $robot->$action() | ||
| 73 | } | ||
| 74 | |||
| 75 | =item $robot->show | ||
| 76 | |||
| 77 | Draw the grid with the robot. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub show { | ||
| 82 | my ($robot) = @_; | ||
| 83 | cls(); | ||
| 84 | my $grid = $robot->grid; | ||
| 85 | for my $y (0 .. $grid->y + 1) { | ||
| 86 | for my $x (0 .. $grid->x + 1) { | ||
| 87 | if ($x == $robot->x && $y == $robot->y) { | ||
| 88 | print { N => '^', | ||
| 89 | E => '>', | ||
| 90 | S => 'v', | ||
| 91 | W => '<' }->{ $robot->direction }; | ||
| 92 | } else { | ||
| 93 | print $grid->at($x, $y); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | print "\n"; | ||
| 97 | } | ||
| 98 | say '[', $robot->cover, ']'; | ||
| 99 | } | ||
| 100 | |||
| 101 | =item $robot->load_grid_from_path($path) | ||
| 102 | |||
| 103 | List the files in the given path, load grid from the selected one. If | ||
| 104 | a directory is selected, show its contents. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub load_grid_from_path { | ||
| 109 | my ($robot, $path) = @_; | ||
| 110 | $path = getcwd() unless $path; | ||
| 111 | chdir $path; | ||
| 112 | my @files = grep './' ne $_, glob ".*/ */ *.kg"; | ||
| 113 | $robot->menu(undef, [ map { | ||
| 114 | my $f = $_; | ||
| 115 | [ $f => sub { my $r = shift; | ||
| 116 | if (-f $f) { | ||
| 117 | $r->load_grid( file => $f ); | ||
| 118 | } elsif (-d $f) { | ||
| 119 | $r->load_grid_from_path($f) | ||
| 120 | } | ||
| 121 | } ] | ||
| 122 | } @files ]); | ||
| 123 | } | ||
| 124 | |||
| 125 | =item $robot->load_commands($file) | ||
| 126 | |||
| 127 | Load commands from the given file. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub load_commands { | ||
| 132 | my ($robot, $file) = @_; | ||
| 133 | my $contents = do { | ||
| 134 | open my $IN, '<', $file or croak $!; | ||
| 135 | local $/; | ||
| 136 | <$IN> }; | ||
| 137 | $robot->learn($contents); | ||
| 138 | } | ||
| 139 | |||
| 140 | =item $robot->load_commands_from_path($path) | ||
| 141 | |||
| 142 | List the files in the given path. Load commands from the selected | ||
| 143 | one. If a directory is selected, show its contents. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub load_commands_from_path { | ||
| 148 | my ($robot, $path) = @_; | ||
| 149 | $path = getcwd() unless $path; | ||
| 150 | chdir $path; | ||
| 151 | my @files = grep './' ne $_, glob ".*/ */ *.krl"; | ||
| 152 | $robot->menu(undef, [ map { | ||
| 153 | my $f = $_; | ||
| 154 | [ $f => sub { my $r = shift; | ||
| 155 | if (-f $f) { | ||
| 156 | $r->load_commands($f); | ||
| 157 | } elsif (-d $f) { | ||
| 158 | $r->load_commands_from_path($f) | ||
| 159 | } | ||
| 160 | } ] | ||
| 161 | } @files ]); | ||
| 162 | } | ||
| 163 | |||
| 164 | =item execute | ||
| 165 | |||
| 166 | Show all the possible commands as a C<menu>, run the selected one. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub execute { | ||
| 171 | my ($robot) = @_; | ||
| 172 | my @commands = sort keys %{ $robot->knowledge || {} }; | ||
| 173 | push @commands, qw( left forward drop-mark pick-mark ); | ||
| 174 | $robot->menu(undef, [ map { | ||
| 175 | my $cmd = $_; | ||
| 176 | [ $cmd => sub { | ||
| 177 | $robot->run($cmd); | ||
| 178 | $robot->step while $robot->is_running; | ||
| 179 | } ] | ||
| 180 | } @commands ]); | ||
| 181 | } | ||
| 182 | |||
| 183 | =back | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | __PACKAGE__ |