Skip to content

Instantly share code, notes, and snippets.

Created August 23, 2014 00:28
Show Gist options
  • Save anonymous/425b07001cfea1d89ade to your computer and use it in GitHub Desktop.
Save anonymous/425b07001cfea1d89ade to your computer and use it in GitHub Desktop.
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Controller {
private static final File workingDir = new File("resources/players").getAbsoluteFile();
public static final char BUY = 'B';
public static final char SELL = 'S';
public static void main(String[] args){
try {
BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream("resources/config")));
int numRounds = Integer.parseInt(br1.readLine());
int turnsPerRound = Integer.parseInt(br1.readLine());
List<Player> players = new ArrayList<Player>();
Map<Player, BigInteger> totals = new HashMap<Player, BigInteger>();
String line1 = null;
while ((line1 = br1.readLine()) != null) {
Player p = new Player(line1);
players.add(p);
totals.put(p, BigInteger.ZERO);
}
br1.close();
//Begin processing
for(int round = 0; round < numRounds; round++){
//Create players' shares and currency array
Map<Player, Possessions> vals = new HashMap<Player, Possessions>();
for (Player player : players) vals.put(player, new Possessions());
BigInteger marketValue = BigInteger.valueOf(getRandInt(10,150));
for (int turn = 0; turn < turnsPerRound; turn++){
BigInteger newValue = marketValue;
for (Player player : players) {
Possessions position = vals.get(player);
String line = "Err";
try {
Process proc = player.run(marketValue, position);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
line = br.readLine();
br.close();
proc.destroy();
switch (line == null ? '\u0000' : line.charAt(0)) {
case BUY: {
BigInteger bought = new BigInteger(line.substring(1).trim());
if (bought.compareTo(BigInteger.ZERO) < 0) throw new IllegalArgumentException("Can't buy negative shares");
BigInteger cost = bought.multiply(marketValue);
if (cost.compareTo(position.cash) <= 0) {
position.cash = position.cash.subtract(cost);
position.shares = position.shares.add(bought);
newValue = newValue.add(bought.multiply(BigInteger.valueOf(getRandInt(0,2))));
if (newValue.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) newValue = BigInteger.valueOf(Integer.MAX_VALUE - 1);
}
break;
}
case SELL: {
BigInteger sold = new BigInteger(line.substring(1).trim());
if (sold.compareTo(BigInteger.ZERO) < 0) throw new IllegalArgumentException("Can't sell negative shares");
if (sold.compareTo(position.shares) <= 0) {
position.cash = position.cash.add(sold.multiply(marketValue));
position.shares = position.shares.subtract(sold);
newValue = newValue.subtract(sold.multiply(BigInteger.valueOf(getRandInt(5,10))));
if (newValue.compareTo(BigInteger.ONE) < 0) newValue = BigInteger.ONE;
}
break;
}
default:
break;
}
} catch (Exception e) {
System.err.println("[" + player + "] threw error:");
e.printStackTrace();
}
}
marketValue = newValue;
System.out.println("Turn " + turn + " over: " + marketValue);
}
System.out.println("End of round market value is: " + marketValue);
for (Player player : players) {
Possessions position = vals.get(player);
totals.put(player, totals.get(player).add(position.cash).add(position.shares.multiply(marketValue)));
}
}
for (Player player : players) {
System.out.println(player + ":\t$" + totals.get(player).divide(BigInteger.valueOf(numRounds)));
}
} catch (Exception e) {
System.err.println("An exception occured while running the controller.");
e.printStackTrace();
}
}
public static Random r = new Random(new Date().getTime());
public static int getRandInt(int min, int max){
return r.nextInt(max - min) + min;
}
private static class Possessions {
public BigInteger cash;
public BigInteger shares;
public Possessions() {
cash = BigInteger.valueOf(5000);
shares = BigInteger.valueOf(getRandInt(20, 30)); // TODO Fix this random bias
}
}
private static class Player {
private List<String> cmd;
public Player(String configLine) {
String[] parts = configLine.split("&");
cmd = new ArrayList<String>();
for (int i = 0; i < parts.length; i++) cmd.add(parts[i]);
}
public Process run(BigInteger stockPrice, Possessions position) throws IOException {
List<String> fullCmd = new ArrayList<String>(cmd);
fullCmd.add(stockPrice.toString());
fullCmd.add(position.cash.toString());
fullCmd.add(position.shares.toString());
return new ProcessBuilder(fullCmd).directory(workingDir).start();
}
@Override
public String toString() {
return cmd.get(cmd.size() - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment