From: Erik V. <ev...@us...> - 2010-06-15 20:17:02
|
Update of /cvsroot/rails/18xx/rails/game/specific/_1835 In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv30779/rails/game/specific/_1835 Modified Files: PrussianFormationRound.java Log Message: 1835: fixed train discarding by PR at end of any PR formation round. Index: PrussianFormationRound.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/specific/_1835/PrussianFormationRound.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** PrussianFormationRound.java 7 May 2010 20:03:48 -0000 1.12 --- PrussianFormationRound.java 15 Jun 2010 20:16:54 -0000 1.13 *************** *** 7,10 **** --- 7,11 ---- import rails.common.GuiDef; import rails.game.*; + import rails.game.action.DiscardTrain; import rails.game.action.PossibleAction; import rails.game.move.CashMove; *************** *** 23,30 **** private boolean mergePr; private boolean forcedMerge; private enum Step { START, ! MERGE }; --- 24,34 ---- private boolean mergePr; private boolean forcedMerge; + + private List<CompanyI> foldablePrePrussians; private enum Step { START, ! MERGE, ! DISCARD_TRAINS }; *************** *** 71,77 **** --- 75,83 ---- startingPlayer = ((GameManager_1835)gameManager).getPrussianFormationStartingPlayer(); + log.debug("Original Prussian starting player was "+startingPlayer.getName()); setCurrentPlayer(startingPlayer); if (forcedMerge) { List<SpecialPropertyI> sps; + setFoldablePrePrussians(); List<CompanyI> foldables = new ArrayList<CompanyI> (); for (PrivateCompanyI company : gameManager.getAllPrivateCompanies()) { *************** *** 90,93 **** --- 96,101 ---- executeExchange (foldables, false, true); finishRound(); + } else { + findNextMergingPlayer(false); } } *************** *** 108,144 **** } else if (step == Step.MERGE) { ! while (true) { ! List<CompanyI> foldables = new ArrayList<CompanyI> (); ! SpecialPropertyI sp; ! for (PrivateCompanyI company : currentPlayer.getPortfolio().getPrivateCompanies()) { ! sp = company.getSpecialProperties().get(0); ! if (sp instanceof ExchangeForShare) { ! foldables.add(company); ! } ! } ! PublicCompanyI company; ! List<SpecialPropertyI> sps; ! for (PublicCertificateI cert : currentPlayer.getPortfolio().getCertificates()) { ! if (!cert.isPresidentShare()) continue; ! company = cert.getCompany(); ! sps = company.getSpecialProperties(); ! if (sps != null && !sps.isEmpty() && sps.get(0) instanceof ExchangeForShare) { ! foldables.add(company); ! } ! } ! if (foldables.isEmpty()) { ! // No merge options for the current player, try the next one ! setNextPlayer(); ! if (getCurrentPlayer() == startingPlayer) { ! finishRound(); ! break; ! } else { ! continue; ! } ! } else { ! possibleActions.add(new FoldIntoPrussian(foldables)); ! break; ! } ! } } return true; --- 116,127 ---- } else if (step == Step.MERGE) { ! possibleActions.add(new FoldIntoPrussian(foldablePrePrussians)); ! ! } else if (step == Step.DISCARD_TRAINS) { ! ! if (prussian.getNumberOfTrains() > prussian.getTrainLimit(getCurrentPhase().getIndex())) { ! possibleActions.add(new DiscardTrain(prussian, ! prussian.getPortfolio().getUniqueTrains(), true)); ! } } return true; *************** *** 146,149 **** --- 129,154 ---- } + private void setFoldablePrePrussians () { + + foldablePrePrussians = new ArrayList<CompanyI> (); + SpecialPropertyI sp; + for (PrivateCompanyI company : currentPlayer.getPortfolio().getPrivateCompanies()) { + sp = company.getSpecialProperties().get(0); + if (sp instanceof ExchangeForShare) { + foldablePrePrussians.add(company); + } + } + PublicCompanyI company; + List<SpecialPropertyI> sps; + for (PublicCertificateI cert : currentPlayer.getPortfolio().getCertificates()) { + if (!cert.isPresidentShare()) continue; + company = cert.getCompany(); + sps = company.getSpecialProperties(); + if (sps != null && !sps.isEmpty() && sps.get(0) instanceof ExchangeForShare) { + foldablePrePrussians.add(company); + } + } + } + @Override protected boolean processGameSpecificAction(PossibleAction action) { *************** *** 158,174 **** } else { step = Step.MERGE; } } else if (step == Step.MERGE) { mergeIntoPrussian (a); ! ! // No merge options for the current player, try the next one ! setNextPlayer(); ! if (getCurrentPlayer() == startingPlayer) { ! finishRound(); ! } ! } return true; } else { return false; --- 163,182 ---- } else { step = Step.MERGE; + findNextMergingPlayer(false); } + } else if (step == Step.MERGE) { mergeIntoPrussian (a); ! } + return true; + + } else if (action instanceof DiscardTrain) { + + discardTrain ((DiscardTrain) action); + return true; + } else { return false; *************** *** 176,179 **** --- 184,209 ---- } + protected boolean findNextMergingPlayer(boolean skipCurrentPlayer) { + + while (true) { + + if (skipCurrentPlayer) { + setNextPlayer(); + if (getCurrentPlayer() == startingPlayer) { + if (prussian.getNumberOfTrains() > prussian.getTrainLimit(getCurrentPhase().getIndex())) { + step = Step.DISCARD_TRAINS; + } else { + finishRound(); + } + return false; + } + } + + setFoldablePrePrussians(); + if (!foldablePrePrussians.isEmpty()) return true; + skipCurrentPlayer = true; + } + } + private boolean startPrussian (FoldIntoPrussian action) { *************** *** 248,251 **** --- 278,283 ---- // Execute if (folding) executeExchange (folded, false, false); + + findNextMergingPlayer(true); return folding; *************** *** 326,348 **** } ! // Check the trains, autodiscard any excess non-permanent trains ! int trainLimit = prussian.getTrainLimit(gameManager.getCurrentPlayerIndex()); ! List<TrainI> trains = prussian.getPortfolio().getTrainList(); ! if (prussian.getNumberOfTrains() > trainLimit) { ! ReportBuffer.add(""); ! int numberToDiscard = prussian.getNumberOfTrains() - trainLimit; ! List<TrainI> trainsToDiscard = new ArrayList<TrainI>(4); ! for (TrainI train : trains) { ! if (!train.getType().isPermanent()) { ! trainsToDiscard.add(train); ! if (--numberToDiscard == 0) break; ! } } ! for (TrainI train : trainsToDiscard) { ! train.moveTo(pool); ! ReportBuffer.add(LocalText.getText("CompanyDiscardsTrain", ! PR_ID, train.getName())); } } } --- 358,417 ---- } ! } ! ! public boolean discardTrain(DiscardTrain action) { ! ! TrainI train = action.getDiscardedTrain(); ! PublicCompanyI company = action.getCompany(); ! ! String errMsg = null; ! ! // Dummy loop to enable a quick jump out. ! while (true) { ! // Checks ! // Must be correct step ! if (company != prussian) { ! errMsg = LocalText.getText("WrongCompany", company.getName(), prussian.getName()); ! break; } ! ! if (train == null && action.isForced()) { ! errMsg = LocalText.getText("NoTrainSpecified"); ! break; } + + // Does the company own such a train? + if (!company.getPortfolio().getTrainList().contains(train)) { + errMsg = + LocalText.getText("CompanyDoesNotOwnTrain", + company.getName(), + train.getName() ); + break; + } + + break; } + if (errMsg != null) { + DisplayBuffer.add(LocalText.getText("CannotDiscardTrain", + company.getName(), + (train != null ?train.getName() : "?"), + errMsg )); + return false; + } + + /* End of validation, start of execution */ + moveStack.start(true); + // + if (action.isForced()) moveStack.linkToPreviousMoveSet(); + + pool.buyTrain(train, 0); + ReportBuffer.add(LocalText.getText("CompanyDiscardsTrain", + company.getName(), + train.getName() )); + + // This always finished this type of round + finishRound(); + + return true; } |