From: Erik V. <ev...@us...> - 2010-05-15 16:36:17
|
Update of /cvsroot/rails/18xx/rails/game In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv5643/rails/game Modified Files: Round.java OperatingRound.java GameManager.java Log Message: Rewritten code to assign next operating company to avoid the OperatingCompanyIndex state object. This was continually causing problems. Also partial implementation of 18EU bankruptcy; game currently hangs if this occurs. Index: OperatingRound.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/OperatingRound.java,v retrieving revision 1.129 retrieving revision 1.130 diff -C2 -d -r1.129 -r1.130 *** OperatingRound.java 13 May 2010 09:51:32 -0000 1.129 --- OperatingRound.java 15 May 2010 16:36:09 -0000 1.130 *************** *** 11,14 **** --- 11,15 ---- import rails.game.special.*; import rails.game.state.EnumState; + import rails.game.state.GenericState; import rails.game.state.IntegerState; import rails.util.LocalText; *************** *** 33,41 **** = new ArrayList<PublicCompanyI> (); ! protected PublicCompanyI[] operatingCompanyArray; ! protected IntegerState operatingCompanyIndexObject; ! protected PublicCompanyI operatingCompany; // Non-persistent lists (are recreated after each user action) --- 34,43 ---- = new ArrayList<PublicCompanyI> (); ! protected List<PublicCompanyI> operatingCompanies; ! //protected IntegerState operatingCompanyIndexObject; ! protected GenericState<PublicCompanyI> operatingCompanyObject; ! protected PublicCompanyI operatingCompany = null; // Non-persistent lists (are recreated after each user action) *************** *** 94,98 **** super (gameManager); ! operatingCompanyArray = super.getOperatingCompanies(); // sfy NoMapMode --- 96,100 ---- super (gameManager); ! operatingCompanies = setOperatingCompanies(); // sfy NoMapMode *************** *** 112,119 **** privatesPayOut(); ! if (operatingCompanyArray.length > 0) { StringBuilder msg = new StringBuilder(); ! for (PublicCompanyI company : operatingCompanyArray) { msg.append(",").append(company.getName()); } --- 114,121 ---- privatesPayOut(); ! if (operatingCompanies.size() > 0) { StringBuilder msg = new StringBuilder(); ! for (PublicCompanyI company : operatingCompanies) { msg.append(",").append(company.getName()); } *************** *** 1491,1540 **** protected boolean setNextOperatingCompany(boolean initial) { - - if (operatingCompanyIndexObject == null) { - operatingCompanyIndexObject = - new IntegerState("OperatingCompanyIndex"); - } - /* - if (initial) { - operatingCompanyIndexObject.set(0); - } else { - operatingCompanyIndexObject.add(1); - } - - int operatingCompanyIndex = operatingCompanyIndexObject.intValue(); - - if (operatingCompanyIndex >= operatingCompanyArray.length) { - operatingCompany = null; - return false; - } else { - operatingCompany = operatingCompanyArray[operatingCompanyIndex]; - log.debug("Operating company is "+operatingCompany.getName()+" in "+getRoundName()); - return true; - } - */ - // The above logic doesn't work if companies have closed in the meantime. - // The new logic below originates from OperatingRound_1856 while (true) { ! if (initial) { ! operatingCompanyIndexObject.set(0); initial = false; } else { ! operatingCompanyIndexObject.add(1); } ! int operatingCompanyIndex = operatingCompanyIndexObject.intValue(); ! ! if (operatingCompanyIndex >= operatingCompanyArray.length) { ! return false; ! } else { ! operatingCompany = operatingCompanyArray[operatingCompanyIndex]; ! ! if (operatingCompany.isClosed()) continue; ! return true; ! } } ! } --- 1493,1522 ---- protected boolean setNextOperatingCompany(boolean initial) { while (true) { ! if (initial || operatingCompany == null || operatingCompanyObject == null) { ! setOperatingCompany(operatingCompanies.get(0)); initial = false; } else { ! int index = operatingCompanies.indexOf(operatingCompany); ! if (++index >= operatingCompanies.size()) { ! return false; ! } ! setOperatingCompany(operatingCompanies.get(index)); } ! if (operatingCompany.isClosed()) continue; ! return true; } ! } ! ! protected void setOperatingCompany (PublicCompanyI company) { ! if (operatingCompanyObject == null) { ! operatingCompanyObject = ! new GenericState<PublicCompanyI>("OperatingCompanyIndex", company); ! } else { ! operatingCompanyObject.set(company); ! } ! operatingCompany = company; } *************** *** 1754,1758 **** excessTrainCompanies = new HashMap<Player, List<PublicCompanyI>>(); Player player; ! for (PublicCompanyI comp : operatingCompanyArray) { if (comp.getPortfolio().getNumberOfTrains() > comp.getTrainLimit(getCurrentPhase().getIndex())) { player = comp.getPresident(); --- 1736,1740 ---- excessTrainCompanies = new HashMap<Player, List<PublicCompanyI>>(); Player player; ! for (PublicCompanyI comp : operatingCompanies) { if (comp.getPortfolio().getNumberOfTrains() > comp.getTrainLimit(getCurrentPhase().getIndex())) { player = comp.getPresident(); *************** *** 2186,2195 **** */ public PublicCompanyI getOperatingCompany() { ! return operatingCompany; } ! ! @Override ! public PublicCompanyI[] getOperatingCompanies() { ! return operatingCompanyArray; } --- 2168,2176 ---- */ public PublicCompanyI getOperatingCompany() { ! return operatingCompanyObject.getObject(); } ! ! public List<PublicCompanyI> getOperatingCompanies() { ! return operatingCompanies; } *************** *** 2217,2221 **** public int getOperatingCompanyIndex() { ! return operatingCompanyIndexObject.intValue(); } --- 2198,2202 ---- public int getOperatingCompanyIndex() { ! return operatingCompanies.indexOf(getOperatingCompany()); } *************** *** 2229,2235 **** public boolean setPossibleActions() { ! ! int operatingCompanyIndex = operatingCompanyIndexObject.intValue(); ! operatingCompany = operatingCompanyArray[operatingCompanyIndex]; /* Create a new list of possible actions for the UI */ --- 2210,2214 ---- public boolean setPossibleActions() { ! operatingCompany = getOperatingCompany(); /* Create a new list of possible actions for the UI */ *************** *** 2585,2589 **** /* Other company trains, sorted by president (current player first) */ if (getCurrentPhase().isTrainTradingAllowed()) { - PublicCompanyI c; BuyTrain bt; Player p; --- 2564,2567 ---- *************** *** 2599,2604 **** List<PublicCompanyI> companies; // Sort out which players preside over which companies. ! for (int j = 0; j < operatingCompanyArray.length; j++) { ! c = operatingCompanyArray[j]; if (c.isClosed() || c == operatingCompany) continue; p = c.getPresident(); --- 2577,2581 ---- List<PublicCompanyI> companies; // Sort out which players preside over which companies. ! for (PublicCompanyI c : getOperatingCompanies()) { if (c.isClosed() || c == operatingCompany) continue; p = c.getPresident(); Index: Round.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/Round.java,v retrieving revision 1.40 retrieving revision 1.41 diff -C2 -d -r1.40 -r1.41 *** Round.java 8 May 2010 13:57:31 -0000 1.40 --- Round.java 15 May 2010 16:36:09 -0000 1.41 *************** *** 250,258 **** } ! /** Get the operating companies in their current acting order */ ! public PublicCompanyI[] getOperatingCompanies() { - List<PublicCompanyI> companies = - companyManager.getAllPublicCompanies(); Map<Integer, PublicCompanyI> operatingCompanies = new TreeMap<Integer, PublicCompanyI>(); --- 250,256 ---- } ! /** Set the operating companies in their current acting order */ ! public List<PublicCompanyI> setOperatingCompanies() { Map<Integer, PublicCompanyI> operatingCompanies = new TreeMap<Integer, PublicCompanyI>(); *************** *** 260,264 **** int key; int minorNo = 0; ! for (PublicCompanyI company : companies) { if (!canCompanyOperateThisRound(company)) continue; --- 258,262 ---- int key; int minorNo = 0; ! for (PublicCompanyI company : companyManager.getAllPublicCompanies()) { if (!canCompanyOperateThisRound(company)) continue; *************** *** 278,282 **** } ! return operatingCompanies.values().toArray(new PublicCompanyI[0]); } --- 276,280 ---- } ! return new ArrayList<PublicCompanyI>(operatingCompanies.values()); } Index: GameManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManager.java,v retrieving revision 1.100 retrieving revision 1.101 diff -C2 -d -r1.100 -r1.101 *** GameManager.java 8 May 2010 13:56:30 -0000 1.100 --- GameManager.java 15 May 2010 16:36:09 -0000 1.101 *************** *** 1006,1009 **** --- 1006,1047 ---- if (gameEndsWithBankruptcy) { finishGame(); + } else { + Player player, newPresident; + int numberOfPlayers = getNumberOfPlayers(); + int maxShare; + int share; + + // Assume default case as in 18EU: all assets to Bank/Pool + Player bankrupter = getCurrentPlayer(); + new CashMove (bankrupter, bank, bankrupter.getCash()); + Portfolio bpf = bankrupter.getPortfolio(); + List<PublicCompanyI> presidencies = new ArrayList<PublicCompanyI>(); + for (PublicCertificateI cert : bpf.getCertificates()) { + if (cert.isPresidentShare()) presidencies.add(cert.getCompany()); + } + for (PublicCompanyI company : presidencies) { + // Check if the presidency is dumped on someone + newPresident = null; + maxShare = 0; + for (int index=getCurrentPlayerIndex()+1; + index<getCurrentPlayerIndex()+numberOfPlayers; index++) { + player = getPlayerByIndex(index%numberOfPlayers); + share = player.getPortfolio().getShare(company); + if (share >= company.getPresidentsShare().getShare() + && (share > maxShare)) { + maxShare = share; + newPresident = player; + } + } + if (newPresident != null) { + bankrupter.getPortfolio().swapPresidentCertificate(company, + newPresident.getPortfolio()); + } else { + company.setClosed(); + // TODO: can be restarted (in 18EU) + } + } + // Dump all shares + Util.moveObjects(bankrupter.getPortfolio().getCertificates(), bank.getPool()); } } |