
package coins ;

sub new {
	my $size = int(rand(3)) + 3 ;
	my @mycoins=() ;
	my $previous = 0 ;
	for( my $i = 0 ; $i < $size ; $i++ ) {
		$position = $previous + int(rand(3)) + 1 ;
		push( @mycoins, $position ) ;
		$previous = $position ;
	}
	my $self = \@mycoins ;
	bless  $self ;
	return $self ;
}


sub clone {
	my ( $self ) = @_ ;
	my @mycoins=() ;
	for( my $i = 0 ; $i <= $#$self ; $i++ ) {
		push( @mycoins, $self->[$i] ) ;
	}
	my $myclone = \@mycoins ;
	bless  $myclone ;
	return $myclone ;
}


sub evaluate_state {
	my ( $self ) = @_ ;
	for( my $i = 0 ; $i <= $#$self ; $i++ ) {
		if ( $self->[$i] != $i ) { return 0 ; }
	}
	return 1 ;
}


sub allowed_moves {
	my ( $self ) = @_ ;
	@allowed = () ;

	for( my $k = 0 ; $k < $self->[0] ; $k++ ) {
		push( @allowed, [0,$k] ) ;
	}

	for( my $i = 1 ; $i <= $#$self ; $i++ ) {
		for( my $j = $self->[$i-1]+1 ; $j < $self->[$i] ; $j++ ) {
			push( @allowed, [$i,$j] ) ;
		}
	}

	return @allowed ;
}
			

sub move {
	my ( $self, $coin, $pos ) = @_ ;

	return undef unless ( 0 <= $coin && $coin <= $#$self ) ;

	if( $coin == 0 ) {
		if( 0 <= $pos && $pos < $self->[0] ) {
			$self->[0] = $pos ;
		} else {
			return undef ;
		}
	} else {
		if( $self->[$coin-1] < $pos && $pos < $self->[$coin] ) {
			$self->[$coin] = $pos ;
		} else {
			return undef ;
		}
	}
}


sub display {
	my ( $self ) = @_ ;

	for( my $k = 0 ; $k <= $self->[$#$self] ; $k++ ) {
		if( $k < 10 ) { print " " ; }
		print " ", $k ;
	}
	print "\n" ;
	my $l = 0 ;
	for( my $i = 0 ; $i <= $self->[$#$self] ; $i++ ) {
		my $flag = 0 ;
		for( my $j = 0 ; $j <= $#$self ; $j++ ) {
			if( $i == $self->[$j] ) { $flag = 1 ; }
		}
		if( $flag ) { print "  ", $l++ ; } else { print "   " ; }
	}
	print "\n" ;
}

return 1 ;


