#! /usr/bin/env perl


## options

# bid names (for verbose sequence output)
%bid_names = (
               0, 'p',
               1, '1T',
               2, '1K',
               3, '1C',
               4, '1P',
               5, '1NT',
               6, '2T',
               7, '2K',
               8, '2C',
               9, '2P',
              10, '2NT',
              11, '3T',
              12, '3K',
              13, '3C',
              14, '3P',
              15, '3NT',
              16, '4T',
              17, '4K',
              18, '4C',
              19, '4P',
              20, '4NT',
              21, '5T',
              22, '5K',
              23, '5C',
              24, '5P',
              25, '5NT',
              26, '6T',
              27, '6K',
              28, '6C',
              29, '6P',
              30, '6NT',
              31, '7T',
              32, '7K',
              33, '7C',
              34, '7P',
              35, '7NT',
             );


##############################################################################


# get options
use Getopt::Long;
GetOptions(
           'h|help',
           'v|verbose',
          )
  || die "$0: error getting options, exiting now.\n";

# print help if requested
if ($opt_h)
{
  print "usage: $0 [options]\n";
  print "  -h, --help        print this help screen and exit\n";
  print "  -v, --verbose     verbose output (print all sequences)\n";
  exit;
}


# check parameter
if ("$ARGV[0]" =~ /^(\d+)$/)
{
  $sequence_length = "$ARGV[0]";
}
else
{
  die "$0: give sequence lenght as parameter!\n";
}


# generate bidding sequences for every bidding round
while ($bidding_round < $sequence_length)
{
  # increase round count
  $bidding_round++;

  # check all bids
  foreach $bid (sort {$a <=> $b} keys(%bid_names))
  {
    # all possible bids for first bid
    if (@sequences == ())
    {
      push @new_sequences, $bid;
    }

    # continue previous sequences
    else
    {
      foreach $previous_sequence (@sequences)
      {
        # get highest bid so far
        $highest_bid = 0;
        foreach $previous_bid (split /\s+/, "$previous_sequence")
        {
          if ($previous_bid > $highest_bid)
          {
            $highest_bid = $previous_bid;
          }
        }

        # current bid must be higher than highest bid so far or pass
        if (($bid > $highest_bid) || ($bid == 0))
        {
          push @new_sequences, "$previous_sequence $bid";
        }
      }
    }
  }


  # re-init for next bidding round
  @sequences = @new_sequences;
  @new_sequences = ();
  $sequence_count = @sequences;


  # verbose output
  if ($opt_v)
  {
    printf "\nbidding round %2.2i:\n", $bidding_round;
    print "--------------------------------\n";
    foreach $sequence (@sequences)
    {
      foreach $bid (split /\s+/, "$sequence")
      {
        printf "%-3s ", $bid_names{$bid};
      }
      print "\n";
    }
    print "--------------------------------\n";
    print "total in this round: $sequence_count\n";
    print "================================\n";
  }

  # short output
  else
  {
    printf "bidding round %2.2i: %10i\n", $bidding_round, $sequence_count;
  }
}
