Four-Man Standoff

Grenadier

Guns are overrated. A true Scotsman's standoff goes like this:

  • Prepare
  • Throw at enemy with most health
  • Repeat (if by some miracle you're still alive)

While this seems trivial, it's probably not a terrible strategy. Since guns and grenades both have a two-turn cycle, this is by far the more efficient1 way to deal damage.

Of course, if all three opponents shoot me on the first round it's no good. But not much else would be, either.

public class Grenadier {
    public static void main(String[] args) {
        if(args.length < 2)
            return;
        String[] list = args[1].split(" ");
        if(list.length < 4)
            return;
        
        if(list[0].charAt(list[0].length()-1) != 'P'){
            System.out.print("P");
            return;
        }
        
        int target = 1;
        for(int i=2;i<4;i++)
            if(list[i].charAt(0)>list[target].charAt(0))
                target = i;

        System.out.print("T"+target);
    }
}

Compile/run in the standard Java way:

> javac Grenadier.java
> java Grenadier arg0 arg1

1 Pointless footnote


Asimov's Rule Number 0 Bot - Python

A robot may not harm humanity, or, by inaction, allow humanity to come to harm.

Pretty straight forward, he'll attack the first player he sees holding a grenade in order to protect the majority of humans. If no one is a threat to the majority of humans, he'll do nothing.

import sys

def total_humans_alive(humans):
  return sum([is_alive(human) for human in humans])

def is_alive(x):
  return int(x.split(",")[0]) > 0  

def is_threat_to_humanity(lastAction):
  return lastAction == "P"

action = "N"
threat_id = 1
humans = sys.argv[2].split()[1:];

if total_humans_alive(humans) == 3:
  for human in humans:
    if is_threat_to_humanity(human[-1]):
      action = "S" + str(threat_id)
      break
    threat_id= threat_id+ 1

print action

Run it like:

python rule0bot.py

Han Solo - Python

Han shot first. In this case, he'll shoot first by picking the closest target alive.

import sys

def is_alive(player):
  return int(player.split(",")[0]) > 0

closest_living_target = 1;

for player in sys.argv[2].split()[1:]:
  if is_alive(player):
    action = "S" + str(closest_living_target)
    break

  closest_living_target = closest_living_target + 1

print action

Run it like:

python hansolo.py

Note: This is the first thing I ever wrote in Python, so if you see any python-specific bad practices, please let me know.