File Coverage

blib/lib/Games/Sudoku/CLI.pm
Criterion Covered Total %
statement 25 49 51.0
branch 2 14 14.2
condition 0 6 0.0
subroutine 6 7 85.7
pod 0 3 0.0
total 33 79 41.7


line stmt bran cond sub pod time code
1             package Games::Sudoku::CLI;
2 1     1   56418 use strict;
  1         4  
  1         35  
3 1     1   8 use warnings;
  1         3  
  1         34  
4 1     1   36 use 5.010;
  1         5  
5              
6 1     1   600 use Games::Sudoku::Component::Controller;
  1         11996  
  1         872  
7              
8             our $VERSION = '0.01';
9              
10             sub new {
11 1     1 0 1257 my ($class) = @_;
12              
13 1         9 return bless {}, $class;
14             }
15              
16             sub play {
17 1     1 0 4 my ($self) = @_;
18              
19 1         12 $self->{ctrl} = Games::Sudoku::Component::Controller->new(size => 9);
20 1         8592 $self->{ctrl}->solve;
21 1         6637233 $self->{ctrl}->make_blank(2);
22              
23 1         880 while (1) {
24 1         8 $self->print_as_grid;
25 1         2084 $self->get_input();
26 1 50       14 if ($self->{input} eq 'x') {
27 0         0 $self->msg('BYE');
28 0         0 return;
29             }
30 1 50       6 if ($self->{input} eq 'q') {
31 1         7 $self->msg('quit game');
32 1         9 last;
33             }
34 0         0 $self->{ctrl}->set(@{ $self->{step} });
  0         0  
35              
36 0 0       0 if ($self->{ctrl}->table->is_finished) {
37 0         0 $self->print_as_grid;
38 0         0 $self->msg('DONE');
39 0         0 last;
40             }
41             }
42 1         5 return;
43             }
44              
45             sub get_input {
46             my ($self) = @_;
47              
48             while (1) {
49             print 'Enter your choice (row, col, value) or [q-quit game, x-exit app]: ';
50             $self->{input} = lc ;
51             chomp $self->{input};
52             last if $self->verify_input();
53             }
54             return;
55             }
56              
57             sub verify_input {
58 0     0 0   my ($self) = @_;
59              
60 0 0         if ($self->{input} =~ /^[xq]$/) {
61 0           return 1;
62             }
63              
64 0           (my $spaceless_input = $self->{input}) =~ s/\s+//g;
65 0           my ($row, $col, $value) = $spaceless_input =~ /^(\d+),(\d+),(\d+)$/;
66 0 0         if (not defined $value) {
67 0           $self->msg("Invalid format: '$self->{input}'");
68 0           return;
69             }
70 0 0 0       if ($row > 9 or $col > 9 or $value > 9) {
      0        
71 0           $self->msg("Invalid values in: '$self->{input}'");
72 0           return;
73             }
74 0 0         if (not $self->{ctrl}->table->cell($row,$col)->is_allowed($value)) {
75 0           $self->msg("Value $value is not allowed in ($row, $col)");
76 0           return;
77             }
78              
79 0           $self->{step} = [$row, $col, $value];
80              
81 0           return 1;
82             }
83              
84             sub print_as_grid {
85             my ($self) = @_;
86              
87             my $table = $self->{ctrl}->table;
88              
89             my $size = $table->{size};
90             my $digit = int(log($size) / log(10)) + 1;
91            
92             print " ";
93             for my $c (1 .. $size) {
94             print " $c ";
95             if ($c % 3 == 0 and $c < 9) {
96             print ' | ';
97             }
98             }
99             print "\n";
100             say ' |' . '-' x 33;
101            
102             foreach my $row (1..$size) {
103             print " $row |";
104             foreach my $col (1..$size) {
105             my $value = $table->cell($row, $col)->value;
106             print $value ? " $value " : ' ';
107             if ($col % 3 == 0 and $col < 9) {
108             print ' | ';
109             }
110             }
111             print "\n";
112             if ($row % 3 == 0 and $row < 9) {
113             say ' |' . '-' x 33;
114             }
115             }
116             return;
117             }
118              
119             sub msg {
120             my ($self, $msg) = @_;
121             say $msg;
122             return;
123             }
124              
125              
126             1;
127              
128             =pod
129              
130             =head1 NAME
131              
132             Games::Sudoku::CLI - play Sudoku on the command line
133              
134             =cut
135              
136