lib/Games/Sudoku/CLI.pm

filter: Statementstmt 52.9% 27 / 51 Branchbran 14.2% 2 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 3 totaltotal 41.6% 35 / 84 CC 10 SCAR 32.7
CC
10
Cov
41%
verify_input
56.0
play
5.6
BEGIN
1.0
CRAP 26.2
↑ Summary script/sudoku.pl »
1 package Games::Sudoku::CLI;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Games::Sudoku::Component::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.01';
9  
10 sub new {
11 1 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12  
13 1 return bless {}, $class;
14 }
15  
16 sub play {
17 1 ▶ my ($self) = @_;
Subroutine play: called CC=4 SCAR=17.2
Pod undocumented
18  
19 1 $self->{ctrl} = Games::Sudoku::Component::Controller->new(size => 9);
20 1 $self->{ctrl}->solve;
21 1 $self->{ctrl}->make_blank(2);
22  
23 1 while (1) {
24 1 $self->print_as_grid;
25 1 $self->get_input();
26 1 ▶ if ($self->{input} eq 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"input"} eq "x") 0 1 1
27 0 $self->msg('BYE');
28 0 return;
29 }
30 1 ▶ if ($self->{input} eq 'q') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"input"} eq "q") 1 0 1
31 1 $self->msg('quit game');
32 1 last;
33 }
34 0 $self->{ctrl}->set(@{ $self->{step} });
35  
36 0 ▶ if ($self->{ctrl}->table->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ctrl"}->table->is_finished) 0 0 0
37 0 $self->print_as_grid;
38 0 $self->msg('DONE');
39 0 last;
40 }
41 }
42 1 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 <STDIN>;
51 chomp $self->{input};
52 last if $self->verify_input();
53 }
54 return;
55 }
56  
57 sub verify_input {
58 0 ▶ my ($self) = @_;
Subroutine verify_input: not called CC=7 SCAR=40.3
Pod undocumented
59  
60 0 ▶ if ($self->{input} =~ /^[xq]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"input"} =~ /^[xq]$/) 0 0 0
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 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
67 0 $self->msg("Invalid format: '$self->{input}'");
68 0 return;
69 }
70 0 ▶ if ($row > 9 or $col > 9 or $value > 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row > 9 or $col > 9 or $value > 9) 0 0 0
Condition0%0/6
$row > 9 or $col > 9 or $value > 9
l!l&&r!l&&!r
000
$row > 9 or $col > 9
l!l&&r!l&&!r
000
MC/DC0%0/3
$row > 9 or $col > 9 or $value > 9
$row > 9 $col > 9 $value > 9
Truth table0%0/4
A or B or C
A= $row > 9B= $col > 9C= $value > 9
ABCresult
1XX1
01X1
0011
0000
71 0 $self->msg("Invalid values in: '$self->{input}'");
72 0 return;
73 }
74 0 ▶ if (not $self->{ctrl}->table->cell($row,$col)->is_allowed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ctrl"}->table->cell($row, $col)->is_allowed($value)) 0 0 0
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 1;
127  
128 =pod
129  
130 =head1 NAME
131  
132 Games::Sudoku::CLI - play Sudoku on the command line
133  
134 =cut
135  
136  
↑ Summary script/sudoku.pl »