#!/usr/bin/perl -w
use Data::Dumper;
$|=1;
my @refs = (0,3,6);



#  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 = ([8   ,$all,$all,$all,$all,$all,$all,2   ,6   ],
         [7   ,$all,2   ,$all,$all,4   ,$all,$all,$all],
         [$all,1   ,5   ,$all,7   ,$all,$all,$all,$all ],	 
         [$all,$all,7   ,4   ,$all,$all,5   ,$all,8   ],	 
         [$all,$all,$all,9   ,$all,8   ,$all,$all,$all ],	 
         [5   ,$all,9   ,$all,$all,6   ,2   ,$all,$all],	 
         [$all,$all,$all,$all,4   ,$all,6   ,7   ,$all ],	 
         [$all,$all,$all,5   ,$all,$all,3   ,$all,2   ],	 
         [2   ,4    ,$all,$all,$all,$all,$all,$all,1]
	 );




sub printgame()
{
for my $i(0..8)
{
    for my $j(0..8)
    {
	if ($sudo[$i][$j] =~ /\d\d/) { if($sudo[$i][$j] =~ /^.$/) { print $sudo[$i][$j]; } else { print "x"; } } 
    }
    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;
}


sub sudo_delete(@)
{

    my ($i,$j) = @_;
    print "Position OK ($i,$j) \n";    
    
    my $square_x = $refs[$i / 3];
    my $square_y = $refs[$j / 3];
    for my $current_a($square_x ..$square_x+2) 
    { 
	for my $current_b($square_y..$square_y+2) 
	{ 
	    print "Del carre $current_a,$current_b ";
	    if ($current_a != $i || $current_b != $j) 
	    { 
	        if ($current_a != $i && $current_b != $j) 
		{
		    $sudo[$current_a][$current_b] =~ s/$sudo[$i][$j]//g; 
		}
	    }
	    print "\n";
	} 
    }
    for(0..8) {   
	if ($_ != $i)
	    { 
	    print "Del line $j $_ \n";	    		    
	    $sudo[$_][$j] =~ s/$sudo[$i][$j]//g; 
	    }
	if ($_ != $j)
	    {
	    print "Del col $_ $i \n";	    	
	    $sudo[$i][$_] =~ s/$sudo[$i][$j]//g; 
	    }
    }



    my @check = (0,0,0,0,0,0,0,0,0);
    my @nums= ();
    for my $current_a($square_x ..$square_x+2) 
    { 
	for my $current_b($square_y..$square_y+2) 
	{ 
	    foreach(split //,$sudo[$current_a][$current_b])
	    {
	        @nums[$_] = [$current_a,$current_b];
		$check[$_]++;
	    }
	}
    }

    for(0..8)
    {
	if ($check[$_] == 1)
	{
	    print "On deduit la position $nums[$_][0], $nums[$_][1]\n";
	    $sudo[$nums[$_][0]][$nums[$_][1]] = $_;
	}
    }








    for(0..8) {   
	for $j(0..8)
	{
	    print $sudo[$_][$j]."\t";
	}
	print"\n";
	
    }



    print"\n"; 
}



printgame;
my $lx=0;
my $ly=0;


print "\n\n";
while(!check())
{
for my $i(0..8)
{
    for my $j(0..8)
    {
	if ($sudo[$i][$j] =~ /^(.)$/) { sudo_delete($i,$j); }
	    print "\tfIN\n\n";
	    printgame;
    }
    
}    
    
printgame;
#select(undef,undef,undef,0.5);
}

