
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] > $#$self ) { return 0 ; }
	}
	return 1 ;
}


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

	for( my $coin = 0 ; $coin <= $#$self ; $coin++ ) {
		for( my $pos = 0 ; $pos < $self->[$coin] ; $pos++ ) {
			my $flag = 0 ;
			for( my $l = 0 ; $l <= $#$self ; $l++ ) {
				if ( $pos == $self->[$l] ) {
					$flag = 1 ;
				}
			}
			push( @allowed, [$coin,$pos] ) unless $flag ;
		}
	}

	return @allowed ;
}
			

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

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

	return undef unless( 0 <= $pos && $pos < $self->[$coin] ) ; 

	for( my $l = 0 ; $l <= $#$self ; $l++ ) {
		if ( $pos == $self->[$l] ) {
			return undef ;
		}
	}

	$self->[$coin] = $pos ;
}


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

	$max = 0 ;
	for( my $k = 0 ; $k <= $#$self ; $k++ ) {
		if ($self->[$k] > $max ) {
			$max = $self->[$k] ;
		}
	}

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

return 1 ;


