Skip to content

Instantly share code, notes, and snippets.

@RogerDodger
Created July 17, 2012 10:58
Show Gist options
  • Save RogerDodger/3128719 to your computer and use it in GitHub Desktop.
Save RogerDodger/3128719 to your computer and use it in GitHub Desktop.
Randomly assigns players into a mafia setup
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dump;
use feature qw/say/;
use List::Util qw/shuffle/;
my %setups = (
Scumcity => [
qw/Godfather Cop Doctor/, qw/Goon/ x 1, qw/Townie Miller/ x 2,
],
C9 => [
[qw/Cop Doctor/, qw/Goon/ x 2, qw/Townie/ x 3],
[qw/Cop/, qw/Goon/ x 2, qw/Townie/ x 4],
[qw/Doctor/, qw/Goon/ x 2, qw/Townie/ x 4],
[qw//, qw/Goon/ x 2, qw/Townie/ x 5],
],
F11 => [
[qw/Roleblocker Cop Doctor/, qw/Goon/ x 1, qw/Townie/ x 5],
[qw/Cop/, qw/Goon/ x 2, qw/Townie/ x 6],
[qw/Doctor/, qw/Goon/ x 2, qw/Townie/ x 6],
[qw/Roleblocker/, qw/Goon/ x 1, qw/Townie/ x 7],
],
);
my $arg = shift // "help";
if($arg eq 'setups') {
for(keys %setups) {
printf " %-15s", $_;
if(ref $setups{$_}->[0]) {
say "Semi-open ", scalar(@{ $setups{$_}->[0] }), " players";
} else {
say "Open ", scalar(@{ $setups{$_} }), " players";
}
}
exit;
}
if(!grep {$arg eq $_} keys %setups) {
# $arg is not a valid setup; print usage info and exit
print "
usage: perl $0 \$setup [info]
Creates a game with the setup '\$setup'.
Reads player names from STDIN.
Outputs the assigned roles to STDOUT.
Get a list of all available setups with:
perl $0 setups
Get info on a particular setup with:
perl $0 \$setup info
";
exit;
}
my $arg2 = shift // "create";
if($arg2 eq "info") {
print "$arg: ";
if(ref $setups{$arg}->[0]) {
say "Semi-open setup. ", scalar(@{ $setups{$arg}->[0] }), " players.";
} else {
say "Open setup. ", scalar(@{ $setups{$arg} }), " players.";
}
dd($setups{$arg});
exit;
}
die "Input error" unless $arg2 eq "create";
# A valid setup is chosen. Retrieve player names and output assigned roles.
my $open = !ref $setups{$arg}->[0];
my $player_count = $open? @{$setups{$arg}} : @{$setups{$arg}->[0]};
my @players;
while (@players < $player_count) {
(my $in = <STDIN>) =~ s/^\s+|\s$//g;
push @players, $in if $in ne '';
}
my @roles;
if($open) {
@roles = @{$setups{$arg}};
} else {
my @setups = @{$setups{$arg}};
@roles = @{$setups[int rand(@setups)]};
}
for(1..10) {
@roles = shuffle(@roles);
@players = shuffle(@players);
}
for(my $i = 0; $i < $player_count; $i++) {
say sprintf "%-15s %s", $players[$i], $roles[$i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment