001    /*
002     * Created on Feb 15, 2005
003     *
004     */
005    package aima.games;
006    
007    import java.util.Hashtable;
008    import java.util.Iterator;
009    import java.util.Set;
010    
011    /**
012     * @author Ravi Mohan
013     * 
014     */
015    
016    public class GameState {
017            private Hashtable<String, Object> state;
018    
019            public GameState() {
020                    state = new Hashtable<String, Object>();
021            }
022    
023            @Override
024            public boolean equals(Object anotherState) {
025    
026                    if (this == anotherState) {
027                            return true;
028                    }
029                    if ((anotherState == null)
030                                    || (this.getClass() != anotherState.getClass())) {
031                            return false;
032                    }
033                    GameState another = (GameState) anotherState;
034                    Set keySet1 = state.keySet();
035                    Iterator i = keySet1.iterator();
036                    Iterator j = another.state.keySet().iterator();
037                    while (i.hasNext()) {
038                            String key = (String) i.next();
039                            boolean keymatched = false;
040                            boolean valueMatched = false;
041                            while (j.hasNext()) {
042                                    String key2 = (String) j.next();
043                                    if (key.equals(key2)) {
044                                            keymatched = true;
045                                            if (state.get(key).equals(another.state.get(key2))) {
046                                                    valueMatched = true;
047                                            }
048                                            break;
049                                    }
050                            }
051                            if (!((keymatched) && valueMatched)) {
052                                    return false;
053                            }
054                    }
055                    return true;
056            }
057    
058            @Override
059            public int hashCode() {
060                    int result = 17;
061                    for (String s : state.keySet()) {
062                            result = 37 * result + s.hashCode();
063                            result = 37 * result + state.get(s).hashCode();
064                    }
065    
066                    return result;
067            }
068    
069            public Object get(String key) {
070                    return state.get(key);
071            }
072    
073            public void put(String key, Object value) {
074                    state.put(key, value);
075    
076            }
077    }