Program: 173719
Project: 173719
Buggy source:
/** 
 * Get a list of buyable trains for the currently operating company. Omit trains that the company has no money for. If there is no cash to buy any train from the Bank, prepare for emergency train buying.
 */
public void setBuyableTrains(){
  if (operatingCompany == null)   return;
  TrainManager trainMgr=gameManager.getTrainManager();
  int cash=operatingCompany.getCash();
  int cost;
  List<TrainI> trains;
  boolean hasTrains=operatingCompany.getPortfolio().getNumberOfTrains() > 0;
  boolean canBuyTrainNow=canBuyTrainNow();
  boolean presidentMayHelp=!hasTrains && operatingCompany.mustOwnATrain();
  TrainI cheapestTrain=null;
  int costOfCheapestTrain=0;
  if (getCurrentPhase().canBuyMoreTrainsPerTurn() || trainsBoughtThisTurn.isEmpty()) {
    boolean mayBuyMoreOfEachType=getCurrentPhase().canBuyMoreTrainsPerTypePerTurn();
    trains=trainMgr.getAvailableNewTrains();
    for (    TrainI train : trains) {
      if (!operatingCompany.mayBuyTrainType(train))       continue;
      if (!mayBuyMoreOfEachType && trainsBoughtThisTurn.contains(train.getType())) {
        continue;
      }
      cost=train.getCost();
      if (cost <= cash) {
        if (canBuyTrainNow) {
          BuyTrain action=new BuyTrain(train,ipo,cost);
          action.setForcedBuyIfNoRoute(presidentMayHelp);
          possibleActions.add(action);
        }
      }
 else       if (costOfCheapestTrain == 0 || cost < costOfCheapestTrain) {
        cheapestTrain=train;
        costOfCheapestTrain=cost;
      }
      if (train.canBeExchanged() && hasTrains) {
        cost=train.getType().getExchangeCost();
        if (cost <= cash) {
          List<TrainI> exchangeableTrains=operatingCompany.getPortfolio().getUniqueTrains();
          BuyTrain action=new BuyTrain(train,ipo,cost);
          action.setTrainsForExchange(exchangeableTrains);
          possibleActions.add(action);
          canBuyTrainNow=true;
        }
      }
      if (!canBuyTrainNow)       continue;
      for (      SpecialTrainBuy stb : getSpecialProperties(SpecialTrainBuy.class)) {
        int reducedPrice=stb.getPrice(cost);
        if (reducedPrice > cash)         continue;
        BuyTrain bt=new BuyTrain(train,ipo,reducedPrice);
        bt.setSpecialProperty(stb);
        bt.setForcedBuyIfNoRoute(presidentMayHelp);
        possibleActions.add(bt);
      }
    }
    if (!canBuyTrainNow)     return;
    trains=pool.getUniqueTrains();
    for (    TrainI train : trains) {
      if (!mayBuyMoreOfEachType && trainsBoughtThisTurn.contains(train.getType())) {
        continue;
      }
      cost=train.getCost();
      if (cost <= cash) {
        BuyTrain bt=new BuyTrain(train,pool,cost);
        bt.setForcedBuyIfNoRoute(presidentMayHelp);
        possibleActions.add(bt);
      }
 else       if (costOfCheapestTrain == 0 || cost < costOfCheapestTrain) {
        cheapestTrain=train;
        costOfCheapestTrain=cost;
      }
    }
    if (!hasTrains && possibleActions.getType(BuyTrain.class).isEmpty() && cheapestTrain != null && presidentMayHelp) {
      BuyTrain bt=new BuyTrain(cheapestTrain,cheapestTrain.getHolder(),costOfCheapestTrain);
      bt.setPresidentMustAddCash(costOfCheapestTrain - cash);
      bt.setForcedBuyIfNoRoute(presidentMayHelp);
      possibleActions.add(bt);
    }
  }
  if (!canBuyTrainNow)   return;
  if (getCurrentPhase().isTrainTradingAllowed()) {
    PublicCompanyI c;
    BuyTrain bt;
    Player p;
    Portfolio pf;
    int index;
    int numberOfPlayers=getNumberOfPlayers();
    List<List<PublicCompanyI>> companiesPerPlayer=new ArrayList<List<PublicCompanyI>>(numberOfPlayers);
    for (int i=0; i < numberOfPlayers; i++)     companiesPerPlayer.add(new ArrayList<PublicCompanyI>(4));
    List<PublicCompanyI> companies;
    for (int j=0; j < operatingCompanyArray.length; j++) {
      c=operatingCompanyArray[j];
      if (c.isClosed() || c == operatingCompany)       continue;
      p=c.getPresident();
      index=p.getIndex();
      companiesPerPlayer.get(index).add(c);
    }
    int currentPlayerIndex=operatingCompany.getPresident().getIndex();
    for (int i=currentPlayerIndex; i < currentPlayerIndex + numberOfPlayers; i++) {
      companies=companiesPerPlayer.get(i % numberOfPlayers);
      for (      PublicCompanyI company : companies) {
        pf=company.getPortfolio();
        trains=pf.getUniqueTrains();
        for (        TrainI train : trains) {
          if (train.isObsolete())           continue;
          if (i != currentPlayerIndex && getGameParameterAsBoolean(GameDef.Parm.FIXED_PRICE_TRAINS_BETWEEN_PRESIDENTS) || operatingCompany.mustTradeTrainsAtFixedPrice() || company.mustTradeTrainsAtFixedPrice()) {
            if (cash >= train.getCost()) {
              bt=new BuyTrain(train,pf,train.getCost());
            }
 else {
              continue;
            }
          }
 else {
            bt=new BuyTrain(train,pf,0);
          }
          if (presidentMayHelp && cash < train.getCost()) {
            bt.setPresidentMayAddCash(train.getCost() - cash);
          }
          possibleActions.add(bt);
        }
      }
    }
  }
  if (!operatingCompany.mustOwnATrain() || operatingCompany.getPortfolio().getNumberOfTrains() > 0) {
    doneAllowed=true;
  }
}

Fixed version (ground truth):
/** 
 * Get a list of buyable trains for the currently operating company. Omit trains that the company has no money for. If there is no cash to buy any train from the Bank, prepare for emergency train buying.
 */
public void setBuyableTrains(){
  if (operatingCompany == null)   return;
  TrainManager trainMgr=gameManager.getTrainManager();
  int cash=operatingCompany.getCash();
  int cost;
  List<TrainI> trains;
  boolean hasTrains=operatingCompany.getPortfolio().getNumberOfTrains() > 0;
  boolean canBuyTrainNow=canBuyTrainNow();
  boolean presidentMayHelp=!hasTrains && operatingCompany.mustOwnATrain();
  TrainI cheapestTrain=null;
  int costOfCheapestTrain=0;
  if (getCurrentPhase().canBuyMoreTrainsPerTurn() || trainsBoughtThisTurn.isEmpty()) {
    boolean mayBuyMoreOfEachType=getCurrentPhase().canBuyMoreTrainsPerTypePerTurn();
    trains=trainMgr.getAvailableNewTrains();
    for (    TrainI train : trains) {
      if (!operatingCompany.mayBuyTrainType(train))       continue;
      if (!mayBuyMoreOfEachType && trainsBoughtThisTurn.contains(train.getType())) {
        continue;
      }
      cost=train.getCost();
      if (cost <= cash) {
        if (canBuyTrainNow) {
          BuyTrain action=new BuyTrain(train,ipo,cost);
          action.setForcedBuyIfNoRoute(presidentMayHelp);
          possibleActions.add(action);
        }
      }
 else       if (costOfCheapestTrain == 0 || cost < costOfCheapestTrain) {
        cheapestTrain=train;
        costOfCheapestTrain=cost;
      }
      if (train.canBeExchanged() && hasTrains) {
        cost=train.getType().getExchangeCost();
        if (cost <= cash) {
          List<TrainI> exchangeableTrains=operatingCompany.getPortfolio().getUniqueTrains();
          BuyTrain action=new BuyTrain(train,ipo,cost);
          action.setTrainsForExchange(exchangeableTrains);
          possibleActions.add(action);
          canBuyTrainNow=true;
        }
      }
      if (!canBuyTrainNow)       continue;
      for (      SpecialTrainBuy stb : getSpecialProperties(SpecialTrainBuy.class)) {
        int reducedPrice=stb.getPrice(cost);
        if (reducedPrice > cash)         continue;
        BuyTrain bt=new BuyTrain(train,ipo,reducedPrice);
        bt.setSpecialProperty(stb);
        bt.setForcedBuyIfNoRoute(presidentMayHelp);
        possibleActions.add(bt);
      }
    }
    if (!canBuyTrainNow)     return;
    trains=pool.getUniqueTrains();
    for (    TrainI train : trains) {
      if (!mayBuyMoreOfEachType && trainsBoughtThisTurn.contains(train.getType())) {
        continue;
      }
      cost=train.getCost();
      if (cost <= cash) {
        BuyTrain bt=new BuyTrain(train,pool,cost);
        bt.setForcedBuyIfNoRoute(presidentMayHelp);
        possibleActions.add(bt);
      }
 else       if (costOfCheapestTrain == 0 || cost < costOfCheapestTrain) {
        cheapestTrain=train;
        costOfCheapestTrain=cost;
      }
    }
    if (!hasTrains && possibleActions.getType(BuyTrain.class).isEmpty() && cheapestTrain != null && presidentMayHelp) {
      BuyTrain bt=new BuyTrain(cheapestTrain,cheapestTrain.getHolder(),costOfCheapestTrain);
      bt.setPresidentMustAddCash(costOfCheapestTrain - cash);
      bt.setForcedBuyIfNoRoute(presidentMayHelp);
      possibleActions.add(bt);
    }
  }
  if (!canBuyTrainNow)   return;
  if (getCurrentPhase().isTrainTradingAllowed()) {
    PublicCompanyI c;
    BuyTrain bt;
    Player p;
    Portfolio pf;
    int index;
    int numberOfPlayers=getNumberOfPlayers();
    List<List<PublicCompanyI>> companiesPerPlayer=new ArrayList<List<PublicCompanyI>>(numberOfPlayers);
    for (int i=0; i < numberOfPlayers; i++)     companiesPerPlayer.add(new ArrayList<PublicCompanyI>(4));
    List<PublicCompanyI> companies;
    for (int j=0; j < operatingCompanyArray.length; j++) {
      c=operatingCompanyArray[j];
      if (c.isClosed() || c == operatingCompany)       continue;
      p=c.getPresident();
      index=p.getIndex();
      companiesPerPlayer.get(index).add(c);
    }
    int currentPlayerIndex=getCurrentPlayer().getIndex();
    for (int i=currentPlayerIndex; i < currentPlayerIndex + numberOfPlayers; i++) {
      companies=companiesPerPlayer.get(i % numberOfPlayers);
      for (      PublicCompanyI company : companies) {
        pf=company.getPortfolio();
        trains=pf.getUniqueTrains();
        for (        TrainI train : trains) {
          if (train.isObsolete())           continue;
          if (i != currentPlayerIndex && getGameParameterAsBoolean(GameDef.Parm.FIXED_PRICE_TRAINS_BETWEEN_PRESIDENTS) || operatingCompany.mustTradeTrainsAtFixedPrice() || company.mustTradeTrainsAtFixedPrice()) {
            if (cash >= train.getCost()) {
              bt=new BuyTrain(train,pf,train.getCost());
            }
 else {
              continue;
            }
          }
 else {
            bt=new BuyTrain(train,pf,0);
          }
          if (presidentMayHelp && cash < train.getCost()) {
            bt.setPresidentMayAddCash(train.getCost() - cash);
          }
          possibleActions.add(bt);
        }
      }
    }
  }
  if (!operatingCompany.mustOwnATrain() || operatingCompany.getPortfolio().getNumberOfTrains() > 0) {
    doneAllowed=true;
  }
}