#!/usr/bin/perl
use strict;
use warnings;

# Grab the command line arguments.
my ($moves, $threat, $threatened, $protect, $protected) = @ARGV;

# Bail-out unless the number of moves (greater than or equal to 0).
die usage() unless $moves and $moves >= 0;

# Create a piece that is unprotected, unthreatened and unbounded.
my $piece = [ map { 1 / $_ } ($moves) x $moves ];

if ( $threat && $threatened ) {
    my @threats = split ',', $threatened;
    my $nonthreatened = $moves - @threats;
    my $nonthreat = $nonthreatened ? $threat * @threats / $nonthreatened : 0;

    my $i = 0;
    for my $p ( @$piece ) {
        $i++;
        if ( grep { $i == $_ } @threats ) {
            $p -= $threat;
        }
        elsif ( !grep { $i == $_ } @threats ) {
            $p += $nonthreat;
        }
    }

    output_state( $piece, 'threatened' );
}

if ( $protect && $protected ) {
    my @protects = split ',', $protected;
    my $nonprotected = $moves - @protects;
    my $nonprotect = $nonprotected ? $protect * @protects / $nonprotected : 0;

    my $i = 0;
    for my $p ( @$piece ) {
        $i++;
        if ( grep { $i == $_ } @protects ) {
            $p += $protect;
        }
        elsif ( !grep { $i == $_ } @protects ) {
            $p -= $nonprotect;
        }
    }

    output_state( $piece, ' protected' );
}

sub output_state {
    my ($piece, $state) = @_;
    my $i = 0;
    print "$state: ", join(' ', map { sprintf '%d:%.4f', ++$i, $_ } @$piece), "\n";
}

sub usage {
    return <<USAGE;

Compute probabilites of chess moves in a protective, threatening environment.

Usage: perl $0 [1-9...] [0-9...] [t1,t2...] [0-9...] [p1,p2...]

Arguments:
 'moves' is the number of moves of a piece.
    For example, a unobstructed knight can make eight moves.
 'threat' is the value or score of a single threat, like 0.2.
 'threatened moves' is a CSV list of threatened move numbers.
    This means that you can be captured by your enemy if you move there.
 'protect' is the value or score of a single protection, like 0.1.
 'protected moves' is a CSV list of protected move numbers.
    This means that you will be protected by an ally if you move there.

Examples:
 perl move-probability 8                # An unthreatened, unprotected knight
 perl move-probability 8 .2 1,8         # Threaten the 1st & 8th moves
 perl move-probability 8 0 0 .1 1,2     # Protect the 1st & 2nd moves
 perl move-probability 8 .2 1,8 .1 1,2  # Threaten & protect
USAGE
}
