From: <ste...@us...> - 2010-08-22 15:32:33
|
Revision: 1400 http://rails.svn.sourceforge.net/rails/?rev=1400&view=rev Author: stefanfrey Date: 2010-08-22 15:32:27 +0000 (Sun, 22 Aug 2010) Log Message: ----------- Added HashMapState Used that to fix problems with TileLaysPerColour And fixed wrong valuation of bankrupt player Modified Paths: -------------- trunk/18xx/rails/game/GameManager.java trunk/18xx/rails/game/OperatingRound.java trunk/18xx/rails/game/Player.java trunk/18xx/rails/game/ShareSellingRound.java trunk/18xx/rails/game/move/MapChange.java trunk/18xx/rails/game/state/ArrayListState.java Added Paths: ----------- trunk/18xx/rails/game/move/RemoveFromMap.java trunk/18xx/rails/game/state/HashMapState.java Modified: trunk/18xx/rails/game/GameManager.java =================================================================== --- trunk/18xx/rails/game/GameManager.java 2010-08-22 15:30:41 UTC (rev 1399) +++ trunk/18xx/rails/game/GameManager.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -995,12 +995,12 @@ File recoveryFile = null; boolean result; try { - log.debug("Created temporary recovery file, path = " + tempFile.getAbsolutePath()); + log.debug("Created temporary recovery file, path = " + tempFile.getPath()); // check if previous save file exists recoveryFile = new File(filePath); - log.debug("Potential recovery filePath = " + recoveryFile.getAbsolutePath()); + log.debug("Potential recovery filePath = " + recoveryFile.getPath()); if (recoveryFile.exists()) { - log.debug("Potential recovery filePath = " + recoveryFile.getAbsolutePath()); + log.debug("Potential recovery filePath = " + recoveryFile.getPath()); File backupFile = new File(filePath + ".bak"); if (recoveryFile.renameTo(backupFile)) { result = tempFile.renameTo(recoveryFile); @@ -1018,7 +1018,7 @@ } if (result) { - log.debug("Renamed to recovery file, path = " + recoveryFile.getAbsolutePath()); + log.debug("Renamed to recovery file, path = " + recoveryFile.getPath()); if (!recoverySaveWarning) { DisplayBuffer.add(LocalText.getText("RecoverySaveSuccessAgain")); recoverySaveWarning = true; Modified: trunk/18xx/rails/game/OperatingRound.java =================================================================== --- trunk/18xx/rails/game/OperatingRound.java 2010-08-22 15:30:41 UTC (rev 1399) +++ trunk/18xx/rails/game/OperatingRound.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -13,6 +13,7 @@ import rails.game.state.ArrayListState; import rails.game.state.EnumState; import rails.game.state.GenericState; +import rails.game.state.HashMapState; import rails.util.LocalText; import rails.util.SequenceUtil; @@ -49,8 +50,8 @@ protected List<LayTile> currentNormalTileLays = new ArrayList<LayTile>(); - protected Map<String, Integer> tileLaysPerColour = - new HashMap<String, Integer>(); + protected HashMapState<String, Integer> tileLaysPerColour = + new HashMapState<String, Integer>("tileLaysPerColour"); protected List<LayBaseToken> currentNormalTokenLays = new ArrayList<LayBaseToken>(); @@ -450,10 +451,6 @@ protected boolean checkNormalTileLay(TileI tile, boolean update) { - // Map<String,Integer> tileLaysPerColour = tileLaysPerColourState.getObject(); - - // if (tileLaysPerColour.isEmpty()) return false; - String colour = tile.getColourName(); Integer oldAllowedNumberObject = tileLaysPerColour.get(colour); @@ -471,28 +468,23 @@ * different colours may be laid. THIS MAY NOT BE TRUE FOR ALL GAMES! */ - // Map<String,Integer> tileLaysPerColourUpdated = new HashMap<String, Integer>(); // new (empty) map - if (oldAllowedNumber <= 1) { - for (String key:tileLaysPerColour.keySet()) - new MapChange<String,Integer>(tileLaysPerColour, key, new Integer(0)); + for (String key:tileLaysPerColour.viewKeySet()) { + tileLaysPerColour.put(key, new Integer(0)); + } log.debug("No more normal tile lays allowed"); currentNormalTileLays.clear(); } else { - // tileLaysPerColourUpdated.put(colour, new Integer(oldAllowedNumber - 1)); - for (String key:tileLaysPerColour.keySet()) - if (colour.equals(key)) - new MapChange<String,Integer> - (tileLaysPerColour, colour, new Integer(oldAllowedNumber-1)); - else - new MapChange<String,Integer>(tileLaysPerColour, key, new Integer(0)); - + for (String key:tileLaysPerColour.viewKeySet()) { + if (colour.equals(key)) { + tileLaysPerColour.put(colour, new Integer(oldAllowedNumber-1)); + } else { + tileLaysPerColour.put(key, new Integer(0)); + } + } log.debug((oldAllowedNumber - 1) + " more " + colour + " tile lays allowed"); } - - // tileLaysPerColourState.set(tileLaysPerColourUpdated); - return true; } @@ -1306,20 +1298,16 @@ * of the tile laying step. */ protected void getNormalTileLays() { - - // Map<String,Integer> - tileLaysPerColour = - new HashMap<String, Integer>(getCurrentPhase().getTileColours()); // Clone - - int allowedNumber; - for (String colour : tileLaysPerColour.keySet()) { - allowedNumber = operatingCompany.get().getNumberOfTileLays(colour); + + // duplicate the phase colours + Map<String, Integer> newTileColours = new HashMap<String, Integer>(getCurrentPhase().getTileColours()); + for (String colour : newTileColours.keySet()) { + int allowedNumber = operatingCompany.get().getNumberOfTileLays(colour); // Replace the null map value with the allowed number of lays - new MapChange<String, Integer>(tileLaysPerColour, colour, new Integer(allowedNumber)); + newTileColours.put(colour, new Integer(allowedNumber)); } - - // store state - // tileLaysPerColourState = new GenericState<Map<String,Integer>>("tileLaysPerColour", tileLaysPerColour); + // store to state + tileLaysPerColour.initFromMap(newTileColours); } protected void setNormalTileLays() { @@ -1330,11 +1318,11 @@ // Map<String,Integer> tileLaysPerColour = (Map<String,Integer>)(tileLaysPerColourState.getObject()); int sumLays = 0; - for (Integer i: tileLaysPerColour.values()) + for (Integer i: tileLaysPerColour.viewValues()) sumLays = sumLays + i; if (sumLays != 0) { // if (!tileLaysPerColour.isEmpty()) { - currentNormalTileLays.add(new LayTile(tileLaysPerColour)); + currentNormalTileLays.add(new LayTile(tileLaysPerColour.viewMap())); } } Modified: trunk/18xx/rails/game/Player.java =================================================================== --- trunk/18xx/rails/game/Player.java 2010-08-22 15:30:41 UTC (rev 1399) +++ trunk/18xx/rails/game/Player.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -83,8 +83,15 @@ * @return Total worth */ public int getWorth() { - int worth = wallet.getCash(); - + // if player is bankrupt cash is not counted + // as this was generated during forced selling + int worth; + if (bankrupt.booleanValue()) { + worth = 0; + } else { + worth = wallet.getCash(); + } + for (PublicCertificateI cert : portfolio.getCertificates()) { worth += cert.getCompany().getGameEndPrice() * cert.getShares(); } Modified: trunk/18xx/rails/game/ShareSellingRound.java =================================================================== --- trunk/18xx/rails/game/ShareSellingRound.java 2010-08-22 15:30:41 UTC (rev 1399) +++ trunk/18xx/rails/game/ShareSellingRound.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -423,7 +423,7 @@ } else if (getSellableShares().isEmpty()) { DisplayBuffer.add(LocalText.getText("YouMustRaiseCashButCannot", Bank.format(cashToRaise.intValue()))); - + currentPlayer.setBankrupt(); gameManager.registerBankruptcy(); } Modified: trunk/18xx/rails/game/move/MapChange.java =================================================================== --- trunk/18xx/rails/game/move/MapChange.java 2010-08-22 15:30:41 UTC (rev 1399) +++ trunk/18xx/rails/game/move/MapChange.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -20,6 +20,10 @@ protected V oldValue; protected boolean keyExisted; + /** + * Creates a move that changes a map <key,value> pair + */ + public MapChange (Map<K, V> map, K key, V newValue) { this.map = map; @@ -30,29 +34,22 @@ MoveSet.add(this); } - + @Override public boolean execute() { - map.put(key, newValue); - return true; } @Override public boolean undo() { - if (keyExisted) { map.put (key, oldValue); - } else { - map.remove(key); } - return true; } public String toString() { return "MapChange: key="+key+" from "+oldValue+" to "+newValue; } - } Added: trunk/18xx/rails/game/move/RemoveFromMap.java =================================================================== --- trunk/18xx/rails/game/move/RemoveFromMap.java (rev 0) +++ trunk/18xx/rails/game/move/RemoveFromMap.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -0,0 +1,55 @@ +/* $Header: /Users/blentz/rails_rcs/cvs/18xx/rails/game/move/MapChange.java,v 1.4 2010/05/05 21:36:59 evos Exp $ + * + * Created on 19-Jul-2006 + * Change Log: + */ +package rails.game.move; + +import java.util.Map; + +/** + * This Move class handles removable from a stateful map (collection) + * + * @author Erik Vos + */ +public class RemoveFromMap<K, V> extends Move { + + protected Map<K, V> map; + protected K key; + protected V oldValue; + protected boolean keyExisted; + + /** + * Creates a move that removes key from map + */ + + public RemoveFromMap (Map<K, V> map, K key) { + + this.map = map; + this.key = key; + this.keyExisted = map.containsKey(key); + + MoveSet.add(this); + } + + @Override + public boolean execute() { + if (keyExisted) { + map.remove(key); + } + return true; + } + + @Override + public boolean undo() { + if (keyExisted) { + map.put (key, oldValue); + } + return true; + } + + public String toString() { + return "RemoveFromMap: remove key="+key+" from map"; + } + +} Property changes on: trunk/18xx/rails/game/move/RemoveFromMap.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/18xx/rails/game/state/ArrayListState.java =================================================================== --- trunk/18xx/rails/game/state/ArrayListState.java 2010-08-22 15:30:41 UTC (rev 1399) +++ trunk/18xx/rails/game/state/ArrayListState.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.List; import rails.game.move.AddToList; @@ -12,50 +11,57 @@ /** * State class that wraps an ArrayList * Generates according list moves + * + * Remark: Does not extend State or implements StateI do avoid additional overhead + * All state/move mechanisms already contained in Move objects + * For the future a simpler unified StateI would make things clearer * - * @author freystef - * + * TODO: Replace all stateful lists by this class and simplify according move objects + * */ -public class ArrayListState<E> extends State { +public class ArrayListState<E> { private final ArrayList<E> list = new ArrayList<E>(); + private String listName; + /** * constructor for an empty list * @param name */ - public ArrayListState(String name) { - super(name, ArrayList.class); + public ArrayListState(String listName) { + this.listName = listName; } /** * constructor for a prefilled list * @param element */ - public ArrayListState(String name, Collection<E> collection) { - super(name, ArrayList.class); - for (E element:collection) { - add(element); - } + public ArrayListState(String listName, Collection<E> collection) { + this(listName); + list.addAll(collection); } public void add(E element) { - new AddToList<E>(list, element, name); + new AddToList<E>(list, element, listName); } public void add(int index, E element) { - new AddToList<E>(list, element, name).atIndex(index); + new AddToList<E>(list, element, listName).atIndex(index); } public void remove(E element) { - new RemoveFromList<E>(list, element, name); + new RemoveFromList<E>(list, element, listName); } public void clear() { for (E element:list) { - new RemoveFromList<E>(list, element, name); + remove(element); } } + /** + * returns unmodifiable view of list + */ public List<E> viewList() { return Collections.unmodifiableList(list); } Added: trunk/18xx/rails/game/state/HashMapState.java =================================================================== --- trunk/18xx/rails/game/state/HashMapState.java (rev 0) +++ trunk/18xx/rails/game/state/HashMapState.java 2010-08-22 15:32:27 UTC (rev 1400) @@ -0,0 +1,103 @@ +package rails.game.state; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import rails.game.move.MapChange; +import rails.game.move.RemoveFromMap; + +/** + * State class that wraps a HashMap + * Generates according map moves + * + * Remark: Does not extend State or implements StateI do avoid additional overhead + * All state/move mechanisms already contained in Move objects + * For the future a simpler unified StateI would make things clearer + * + * TODO: Replace all stateful HashMaps by this class and simplify according move objects + * + */ + +public class HashMapState<K,V>{ + + private final HashMap<K,V> map = new HashMap<K,V>(); + private String mapName; + + /** + * constructor for an empty map + */ + public HashMapState(String listName) { + this.mapName = listName; + } + /** + * constructor for a prefilled map + */ + public HashMapState(String listName, Map<K,V> map) { + this(listName); + } + + public void put(K key, V value) { + new MapChange<K,V>(map, key, value); + } + + public void putAll(Map<K,V> map) { + for (K key:map.keySet()) { + new MapChange<K,V>(map, key, map.get(key)); + } + } + + public V get(K key) { + return map.get(key); + } + + public void remove(K key) { + new RemoveFromMap<K,V>(map, key); + } + + public void clear() { + for (K key:map.keySet()) { + remove(key); + } + } + + /** + * (re)intializes the state map from another map + * efficiently generates the required moves + */ + public void initFromMap(Map<K,V> initMap) { + for (K key:map.keySet()) { + // union elements + if (initMap.containsKey(key)) { + new MapChange<K,V>(map, key, initMap.get(key)); + } else { // only in the old map + new RemoveFromMap<K,V>(map, key); + } + } + for (K key:initMap.keySet()) { + // new elements + if (!map.containsKey(key)) { + new MapChange<K,V>(map, key, initMap.get(key)); + } + } + } + + /** + * @return unmodifiable view of map + */ + public Map<K,V> viewMap() { + return Collections.unmodifiableMap(map); + } + /** + * @return unmodifiable view of keyset + */ + public Set<K> viewKeySet() { + return Collections.unmodifiableSet(map.keySet()); + } + + public Collection<V> viewValues() { + return Collections.unmodifiableCollection(map.values()); + } +} Property changes on: trunk/18xx/rails/game/state/HashMapState.java ___________________________________________________________________ Added: svn:mime-type + text/plain This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |