001 /*
002 Copyright (c) 2005 Garrett Smith
003 The MIT License
004 
005 Permission is hereby granted, free of charge, to any person obtaining a copy 
006 of this software and associated documentation files (the "Software"), to deal
007 in the Software without restriction, including without limitation the rights
008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
009 copies of the Software, and to permit persons to whom the Software is 
010 furnished to do so, subject to the following conditions:
011 
012 The above copyright notice and this permission notice shall be included in all 
013 copies or substantial portions of the Software.
014 
015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
021 THE SOFTWARE.
022 */
023 
024 // $Id: Result.java.html,v 1.1 2005/06/14 06:50:55 gsmith Exp $
025 
026 package garrettsmith.blackjack;
027 
028 /**
029  <p>
030  * Represents the result of one hand that is complete.
031  </p>
032  @author Garrett Smith, gsmith at northwestern dot edu
033  @version Blackjack v1.0
034  @since Blackjack v1.0
035  */
036 public interface Result {
037 
038     /**
039      * Returns the value of the result used only internally for equality comparison.
040      */
041     public int value();
042 
043     /**
044      * Represents when both the player and dealer have the same value 
045      * hand that is equal to or less than 21. 
046      */
047     public static final Result PUSH = new PushResult();
048 
049     /**
050      * Represents when the player has a hand whose value is greater than the dealer's
051      * and both hands are equal to or less than 21.
052      */
053     public static final Result WIN = new WinResult();
054 
055     /**
056      * Represents when the player has a hand whose value is less than the dealer's
057      * and both hands are equal to or less than 21.
058      */
059     public static final Result LOSE = new LoseResult();
060 
061     /**
062      * Represents when the player surrenders his hand.
063      */
064     public static final Result LATE_SURRENDER = new LateSurrenderResult();
065 
066     /**
067      * Represents when the player surrenders his hand.
068      */
069     public static final Result EARLY_SURRENDER = new EarlySurrenderResult();
070 
071     /**
072      * Represents when the dealer has a blackjack.
073      */
074     public static final Result DEALER_BLACKJACK = new DealerBlackjackResult();
075 
076     /**
077      * Represents when the player's hand exceeded 21.
078      */
079     public static final Result BUSTED = new BustedResult();
080 
081     /**
082      * Represents when the dealer's hand exceeded 21 and the player's hand
083      * is equal to or less than 21.
084      */
085     public static final Result DEALER_BUSTED = new DealerBustedResult();
086 
087     /**
088      * Represents when the player has a blackjack.
089      */
090     public static final Result BLACKJACK = new BlackjackResult();
091     
092     /**
093      * Represents when both the player and the dealer have a blackjack.
094      */
095     public static final Result BLACKJACK_PUSH = new BlackjackPushResult();
096 
097 }
098 
099 abstract class BaseResult implements Result {
100     
101     protected String stringValue;
102     
103     public abstract int value();
104     
105     public int hashCode() {
106         return this.getClass().getName().hashCode();
107     }
108     
109     public boolean equalsObject o ) {
110         return o != null
111                 && this.getClass().isInstance)
112                 && ( (Resulto).value() == this.value() );
113     }
114     
115     public String toString() {
116         return stringValue;
117     }
118 }
119 
120 final class PushResult extends BaseResult {
121     PushResult() {
122         stringValue = "push";
123     }
124     public int value() {
125         return 0;
126     }
127 }
128 
129 final class WinResult extends BaseResult {
130     WinResult() {
131         stringValue = "win";
132     }
133     public int value() {
134         return 1;
135     }
136 }
137 
138 final class LoseResult extends BaseResult {
139     LoseResult() {
140         stringValue = "lose";
141     }
142     public int value() {
143         return 2;
144     }
145 }
146 
147 final class LateSurrenderResult extends BaseResult {
148     LateSurrenderResult() {
149         stringValue = "late surrender";
150     }
151     public int value() {
152         return 3;
153     }
154 }
155 
156 final class DealerBlackjackResult extends BaseResult {
157     DealerBlackjackResult() {
158         stringValue = "dealer blackjack";
159     }
160     public int value() {
161         return 4;
162     }
163 }
164 
165 final class BustedResult extends BaseResult {
166     BustedResult() {
167         stringValue = "busted";
168     }
169     public int value() {
170         return 5;
171     }
172 }
173 
174 final class DealerBustedResult extends BaseResult {
175     DealerBustedResult() {
176         stringValue = "dealer busted";
177     }
178     public int value() {
179         return 6;
180     }
181 }
182 
183 final class BlackjackResult extends BaseResult {
184     BlackjackResult() {
185         stringValue = "blackjack";
186     }
187     public int value() {
188         return 7;
189     }
190 }
191 
192 final class BlackjackPushResult extends BaseResult {
193     BlackjackPushResult() {
194         stringValue = "blackjack push";
195     }
196     public int value() {
197         return 8;
198     }
199 }
200 
201 final class EarlySurrenderResult extends BaseResult {
202     EarlySurrenderResult() {
203         stringValue = "early surrender";
204     }
205     public int value() {
206         return 9;
207     }
208 }