| 1 | # Copyright 2017, 2018, 2019, 2020, 2021 Kevin Ryde | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This file is part of Graph-Maker-Other. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # This file is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 6 | # under the terms of the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||||||
| 8 | # version. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # This file is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||||||||||||||||||||
| 13 | # for more details. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||||||
| 16 | # with Graph-Maker-Other. See the file COPYING. If not, see | ||||||||||||||||||||||||||||
| 17 | # <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | package Graph::Maker::NoughtsAndCrosses; | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use 5.004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Graph::Maker; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use vars '$VERSION','@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | $VERSION = 19; | |||||||||||||||||||||||||||
| 26 | 1 | @ISA = ('Graph::Maker'); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 29 | # use Smart::Comments '###'; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 23 | ▶ | sub _make_graph { | ||||||||||||||||||||||||||
|
Subroutine
_make_graph: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 33 | 23 | my ($params) = @_; | |||||||||||||||||||||||||||
| 34 | 23 | ▶ | if (my $graph_maker = delete($params->{'graph_maker'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | return $graph_maker->(%$params); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 23 | require Graph; | |||||||||||||||||||||||||||
| 38 | 23 | return Graph->new(%$params); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 23 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 23 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 23 | my $N = delete $params{'N'}; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (! defined $N) { $N = 3; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 23 | my $rotate = delete $params{'rotate'}; | |||||||||||||||||||||||||||
| 47 | 23 | my $reflect = delete $params{'reflect'}; | |||||||||||||||||||||||||||
| 48 | 23 | my $players = delete $params{'players'}; | |||||||||||||||||||||||||||
| 49 | 23 | ▶ | if (! defined $players) { $players = 2; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | ### $N | ||||||||||||||||||||||||||||
| 52 | ### $players | ||||||||||||||||||||||||||||
| 53 | ### $rotate | ||||||||||||||||||||||||||||
| 54 | ### $reflect | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 23 | my $graph = _make_graph(\%params); | |||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 23 | my $name = "Noughts and Crosses ${N}x$N"; | |||||||||||||||||||||||||||
| 59 | 23 | ▶ | if ($players != 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 17 | ▶ | $name .= ", $players Player" . ($players==1 ? '' : 's'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 23 | ▶ | if ($rotate || $reflect) { $name .= ' up to '; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rotate or $reflect
MC/DC100%2/2
$rotate or $reflect
$rotate
$reflect
Truth table100%3/3
A or B
A= $rotateB= $reflect
| |||||||||||||||||||||||||||||
| 63 | 23 | ▶ | $name .= join(' and ', | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 64 | ($rotate ? ('Rotation') : ()), | ||||||||||||||||||||||||||||
| 65 | ($reflect ? ('Reflection') : ())); | ||||||||||||||||||||||||||||
| 66 | 23 | $graph->set_graph_attribute (name => $name); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # FIXME: should N=0 board be empty graph or single vertex? | ||||||||||||||||||||||||||||
| 70 | 23 | ▶ | if ($N >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | # $players = min($players, $N*$N); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 23 | ▶ | my $join = ($players < 10 ? '' : ','); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 23 | my $hi = $N-1; | |||||||||||||||||||||||||||
| 75 | 2856 | ▶ | my $state_to_str = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 2856 | my ($state) = @_; | |||||||||||||||||||||||||||
| 77 | 2856 | return join($join, @$state); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # return $state; | ||||||||||||||||||||||||||||
| 80 | # for (my $pos = $N*$hi; $pos > 0; $pos -= $N) { | ||||||||||||||||||||||||||||
| 81 | # substr($state,$pos,0, '-'); | ||||||||||||||||||||||||||||
| 82 | # } | ||||||||||||||||||||||||||||
| 83 | # return $state; | ||||||||||||||||||||||||||||
| 84 | 23 | }; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 23 | my $initial_state = [ ('0') x ($N*$N) ]; | |||||||||||||||||||||||||||
| 87 | 23 | my $initial_state_str = $state_to_str->($initial_state); | |||||||||||||||||||||||||||
| 88 | 23 | $graph->add_vertex($initial_state_str); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 23 | ▶ | if ($players >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | ### $N | ||||||||||||||||||||||||||||
| 92 | ### $hi | ||||||||||||||||||||||||||||
| 93 | ### $players | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 5682 | ▶ | my $xy_to_n = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 5682 | my ($x,$y) = @_; | |||||||||||||||||||||||||||
| 97 | 5682 | return $x + $N*$y; | |||||||||||||||||||||||||||
| 98 | 11 | }; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 11 | my @lines; | |||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 11 | @lines = ([ map {$xy_to_n->($_,$_)} 0..$hi ], # leading diag | |||||||||||||||||||||||||||
| 103 | 11 | [ map {$xy_to_n->($hi-$_,$_)} 0..$hi ]); # opposite diag | |||||||||||||||||||||||||||
| 104 | 11 | foreach my $y (0 .. $hi) { | |||||||||||||||||||||||||||
| 105 | 24 | push @lines, [ map {$xy_to_n->($_,$y)} 0..$hi ]; # row | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 11 | foreach my $x (0 .. $hi) { | |||||||||||||||||||||||||||
| 108 | 24 | push @lines, [ map {$xy_to_n->($x,$_)} 0..$hi ]; # column | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 1817 | ▶ | my $state_is_winning = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 112 | 1817 | my ($state, $player) = @_; | |||||||||||||||||||||||||||
| 113 | 1817 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 114 | 11279 | ▶ | if ($N == grep {$state->[$_] == $player} @$line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 779 | return 1; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 1038 | return 0; | |||||||||||||||||||||||||||
| 119 | 11 | }; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # 2 3 -> 0 2 | ||||||||||||||||||||||||||||
| 122 | # 0 1 1 3 | ||||||||||||||||||||||||||||
| 123 | 279 | ▶ | my $rotate_state = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 124 | 279 | my ($state) = @_; | |||||||||||||||||||||||||||
| 125 | 279 | my @new_state; | |||||||||||||||||||||||||||
| 126 | 279 | foreach my $y (0 .. $hi) { | |||||||||||||||||||||||||||
| 127 | 816 | foreach my $x (0 .. $hi) { | |||||||||||||||||||||||||||
| 128 | 2406 | my $rx = $hi-$y; | |||||||||||||||||||||||||||
| 129 | 2406 | my $ry = $x; | |||||||||||||||||||||||||||
| 130 | # map: "$x,$y -> $rx,$ry" | ||||||||||||||||||||||||||||
| 131 | 2406 | push @new_state, $state->[$xy_to_n->($rx,$ry)]; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 279 | return \@new_state; | |||||||||||||||||||||||||||
| 135 | 11 | }; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # r: $rotate_state->('0123') | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 350 | ▶ | my $reflect_state = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 140 | 350 | my ($state) = @_; | |||||||||||||||||||||||||||
| 141 | 350 | my @new_state; | |||||||||||||||||||||||||||
| 142 | 350 | foreach my $y (0 .. $hi) { | |||||||||||||||||||||||||||
| 143 | 1044 | foreach my $x (0 .. $hi) { | |||||||||||||||||||||||||||
| 144 | 3120 | my $rx = $hi - $x; | |||||||||||||||||||||||||||
| 145 | 3120 | push @new_state, $state->[$xy_to_n->($rx,$y)]; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 350 | return \@new_state; | |||||||||||||||||||||||||||
| 149 | 11 | }; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 99 | ▶ | my $all_symmetries = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 152 | 99 | my ($state) = @_; | |||||||||||||||||||||||||||
| 153 | 99 | my @ret = ($state); | |||||||||||||||||||||||||||
| 154 | 99 | ▶ | if ($rotate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 93 | foreach (2 .. 4) { | |||||||||||||||||||||||||||
| 156 | 279 | push @ret, $rotate_state->($ret[-1]); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | # rotates: @ret | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 99 | ▶ | if ($reflect) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 92 | @ret = map {$_, $reflect_state->($_)} @ret; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 99 | return @ret; | |||||||||||||||||||||||||||
| 164 | 11 | }; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 11 | my %canonical; | |||||||||||||||||||||||||||
| 167 | 1817 | ▶ | my $state_to_canonical = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 168 | 1817 | my ($state) = @_; | |||||||||||||||||||||||||||
| 169 | 1817 | ▶ | if ($rotate || $reflect) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rotate or $reflect
MC/DC100%2/2
$rotate or $reflect
$rotate
$reflect
Truth table100%3/3
A or B
A= $rotateB= $reflect
| |||||||||||||||||||||||||||||
| 170 | 288 | ▶ | if (defined(my $new_state = $canonical{$state_to_str->($state)})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 189 | return $new_state; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 99 | foreach my $sym_state ($all_symmetries->($state)) { | |||||||||||||||||||||||||||
| 174 | 728 | $canonical{$state_to_str->($sym_state)} = $state; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 1628 | return $state; | |||||||||||||||||||||||||||
| 178 | 11 | }; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # Recursion goes down to the depth of the graph, which means up to N^2. | ||||||||||||||||||||||||||||
| 181 | # Could do this in an array instead of on the stack, but expect only | ||||||||||||||||||||||||||||
| 182 | # graphs of smallish N are practical anyway, | ||||||||||||||||||||||||||||
| 183 | 11 | my $recurse; | |||||||||||||||||||||||||||
| 184 | 379 | ▶ | $recurse = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 185 | 379 | my ($state, $state_str, $player) = @_; | |||||||||||||||||||||||||||
| 186 | 379 | $player++; | |||||||||||||||||||||||||||
| 187 | 379 | ▶ | if ($player > $players) { $player = 1; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | ### recurse: "$state_str player $player" | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 379 | my %this_seen; | |||||||||||||||||||||||||||
| 191 | 379 | foreach my $pos (0 .. $#$state) { | |||||||||||||||||||||||||||
| 192 | 2896 | ▶ | if ($state->[$pos]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | ### already filled: $pos | ||||||||||||||||||||||||||||
| 194 | 1079 | next; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | ### $pos | ||||||||||||||||||||||||||||
| 197 | 1817 | my $new_state = [@$state]; | |||||||||||||||||||||||||||
| 198 | 1817 | $new_state->[$pos] = $player; | |||||||||||||||||||||||||||
| 199 | 1817 | $new_state = $state_to_canonical->($new_state); | |||||||||||||||||||||||||||
| 200 | 1817 | my $new_state_str = $state_to_str->($new_state); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 1817 | ▶ | my $stop_here = $state_is_winning->($new_state, $player) | ||||||||||||||||||||||||||
|
Condition100%3/3
&$state_is_winning($new_state, $player) || $graph->has_vertex($new_state_str)
MC/DC100%2/2
&$state_is_winning($new_state, $player) || $graph->has_vertex($new_state_str)
&$state_is_winning($new_state, $player)
$graph->has_vertex($new_state_str)
Truth table100%3/3
A || B
A= &$state_is_winning($new_state, $player)B= $graph->has_vertex($new_state_str)
| |||||||||||||||||||||||||||||
| 203 | || $graph->has_vertex($new_state_str); | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # only one edge if symmetry makes same destinations | ||||||||||||||||||||||||||||
| 206 | 1817 | ▶ | unless ($this_seen{$new_state_str}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 1740 | $graph->add_edge($state_str, $new_state_str); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1817 | ▶ | unless ($stop_here) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | #### to: $new_state | ||||||||||||||||||||||||||||
| 212 | 368 | $recurse->($new_state, $new_state_str, $player); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | 11 | }; | |||||||||||||||||||||||||||
| 216 | 11 | $recurse->($initial_state, $initial_state_str, 0); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 23 | return $graph; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1 | Graph::Maker->add_factory_type('noughts_and_crosses' => __PACKAGE__); | |||||||||||||||||||||||||||
| 223 | 1 | 1; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | __END__ | ||||||||||||||||||||||||||||