#!/usr/bin/perl -w
use Data::Dumper;
$|=1;



#  012 345 678           0
#  012 345 678		 1
#  012 345 678		 2
#
#  012 345 678		 3
#  012 345 678		 4
#  012 345 678		 5
#
#  012 345 678		 6
#  012 345 678		 7
#  012 345 678		 8



my @sudo;
for my $i(0..8)
{
    for my $j(0..8)
    {	
	print "Entrez la $i eme ligne, $j ieme colonne: ";
#	my $input= <STDIN>;
#	chomp($input);
$input = "123456789";
	if ($input =~ /\d/) { $sudo[$i][$j] = $input; }
	else {    $sudo[$i][$j]= "123456789"; }
    }
}
my $all = "123456789";
@sudo = ([$all,$all,$all,   4,$all,7   ,$all,$all,9   ],
         [5   ,$all,$all,$all,$all,2   ,4   ,3   ,$all],
         [$all,4   ,1   ,9   ,$all,3   ,$all,$all,$all],	 
         [$all,2   ,$all,6   ,4   ,$all,$all,5   ,$all],	 
         [9   ,$all,3   ,$all,8   ,$all,2   ,$all,1   ],	 
         [$all,5   ,$all,$all,3   ,1   ,$all,6   ,$all],	 
         [$all,$all,$all,1   ,$all,4   ,8   ,7   ,$all],	 
         [$all,8   ,7   ,5   ,$all,$all,$all,$all,2],	 
         [2   ,$all,$all,3   ,$all,8   ,$all,$all,$all]
	 );


sub sudo_delete(@)
{

    my ($i,$j,$lx,$ly,$elt) = @_;
#    print "Del de $elt\n";    
#    print "Delete: $i,$j\n";
    for($lx..$lx+2) 
    { 
	for my $t($ly..$ly+2) 
	{ 
	    print "Del carre $elt($i,$j): $_ $t -> ";
	    if ($_ != $i || $t!=$j){ $sudo[$_][$t] =~ s/$elt//g; print "DELETED";}
	    print "\n";
	} 
    }
    for(0..8) {   
#	print "Del line col: $i $_ et  $_ $j\n";
	if ($_ != $i)
	    { 
	    $sudo[$_][$j] =~ s/$elt//g; }
	if ($_ != $j)
	    { $sudo[$i][$_] =~ s/$elt//g; }

    }


	print"\n"; 
}


sub printgame()
{
for my $i(0..8)
{
    for my $j(0..8)
    {
	if ($sudo[$i][$j] =~ /\d\d/) { print "x "; } 
	else { print $sudo[$i][$j]." ";  }
    }
    print "\n";
}
print "\n\n";
}


sub check()
{
for my $i(0..8)
{
    for my $j(0..8)
    {
	if ( $sudo[$i][$j] =~ /../) { return 0; }
    }
}
return 1;
}


printgame;
my $lx=0;
my $ly=0;
my @refs = (0,3,6);


print "\n\n";
while(!check())
{
for my $i(0..8)
{
    my $lx = $refs[$i / 3];
    for my $j(0..8)
    {
	my $ly = $refs[$j / 3];
	if ($sudo[$i][$j] =~ /^(.)$/) { sudo_delete($i,$j,$lx,$ly,$sudo[$i][$j]); }

	
	
	
    }
}
printgame;
#select(undef,undef,undef,0.5);



}

