#!/usr/bin/perl use warnings; use strict; use lib qw(blib/lib); use Games::Chomp; #my $c = new Games::Chomp; #print $c->resolve([4,4,4]), "\n"; #print $c->resolve([3,3,3]), "\n"; #$c->show_all_winning_pos; #$c->run; my $p = new Games::Chomp::Position; $p->limit_in_row_length(3,2); #$p->row_length(1); #while ($p->next) { # print join ",", $p->row_length; # print "\n"; #} $p->conjugate(7, 2, 3); print join ",", $p->row_length; print "\n"; __END__ #my $rows = 4; #my $columns = 4; #my @max; #@max[0..$columns-1]=($rows); #print "@max"; #my $pos = [10,10,10]; #@winning_positions = ( [1], [2, 1]); # [3, 2] #@loosing_positions = ( [2], [3], [1, 1], [3,1], [1, 1, 1]); #foreach my $p (all_moves_from_here([3, 2])) { # display_position($p); #} #foreach my $pos (all_positions(3,3)) { # display_position($pos); #} # returns a list of arrayrefs # in each array there is a position that is a list of # numbers while non of the numbers is bigger than $cols # and there are at most $rows numbers. sub all_positions { my ($rows, $cols) = @_; my @theset=(); my @pos = (1); # the length of each row while (@pos <= $rows) { push @theset, [@pos]; my $i; for ($i=0; defined $pos[$i] and $pos[$i]==$cols; $i++) {} $pos[$i]++; for(my $j=0; $j<$i; $j++) { $pos[$j] = $pos[$i]; } } return @theset; } =pod my @base_value_test = ( 2, 10, 5, 23, 7, 99, 21, 9827, ); while (my $base = shift @base_value_test) { my $value = shift @base_value_test; unless ($value == base_to_decimal($base, (decimal_to_base($base, $value)))) { print "NOT OK: $base $value\n"; #my @x = decimal_to_base($base, $value); #print @x, "\n"; #print base_to_decimal($base, @x), "\n"; } } =cut # Converts a decimal number to any base # the return value is an array or an array ref # (depending on context) contain sub decimal_to_base { my $base = shift; # the base (like 2 or 8 or 16) my $value = shift; # in decimal format # sanity check ? # $base is positive integer # $value my @inbase = (); while ($value > 0) { my $remand = $value % $base; unshift @inbase, $remand; $value -= $remand; $value /= $base; } if (wantarray) { return @inbase; } else { return \@inbase; } } sub base_to_decimal { my $base = shift; # the base (like 2 or 8 or 16) my @value = @_; my $dec = 0; foreach my $v (@value) { $dec = ($dec * $base) + $v; } return $dec; } # In the chocolate (or covering or chomp (!)) game the ultimate winning position is # [1] when only one cube remains in one row. # Algorithm: # Select all the possible positions which are not in eihter group and from where # there is at least one move that leads to a position in the winning group. # put all these positions into the @loosing_positions group as if X moves to such # a (loosing) position, his opponent Y can(!) move to a winning position. # # Select all the possible positions that .... and have at least one move that # can lead to a position in the loosing positions group. #preceeding_positions(row, col, pos) #for all pos1 in wp # get all poss that can preceede pos1 and put them in lp #foreach my $wp (@winning_positions) { # foreach my $move (possible_earlier_positions($wp, $rows, $columns)) { # if $move # } #} #sub possible_earlier_positions { #} # for all positions within a frame (a table) # if there is a move from it that lead to winning_postition then put it to # the loosing positions # if all ways lead to loosing_positions then put it in the winning_positions # otherwise (there is one or more moves that are not clarified yet) put it at the # end of the line; (or at some position behind the first one but not necessarily # the ast one) #my $dec = ; #my $base = ; #my @x = decimal_to_base($dec, $base); #print "@x\n"; #my $table = 2**$rows * 3**$cols; #x ami osztoja T-nek es nincs olyan y osztoja T-nek aminek x osztoja =pod # all positions that can be reached from here sub all_pos_from_here { my @curpos; for (my $i=0; $i<@curpos; $i++) { for (my $j=0; $j<$curpos[$i]; $j++) { my @next_pos =(); } } } for ($i=1; $i<=$rows; $i++) { solve($i, $cols); } sub solve { my $rows= shift; my $cols= shift; if ($rows ==1 and $cols==1) { push @winning_positions, [1]; return; } # later assume that there are more columns than rows and transpose the rest my @pos=(); for my $i (1..$rows) { push @pos, 1; } while ($pos[-1] < $cols) { } } sub is_legal_move_a_to_b { my ($a, $b) = @_; my $eq; return 0 if (@$a < @$b); # original is shorter foreach my $i (0..@$a) { next if ($$a[$i] == $$b[$i]); return 0 if ($$a[$i] < $$b[$i]); # original has shorter row if (defined $eq) { return 0 if ($$b[$i] != $eq); # 2 different differences } } =cut