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: Rules.java.html,v 1.1 2005/06/14 06:50:55 gsmith Exp $
025
026 package garrettsmith.blackjack;
027
028 import java.util.*;
029
030 /**
031 * Represents the rules that dictate how the game is played.
032 *
033 * @author Garrett Smith, gsmith at northwestern dot edu
034 * @version Blackjack v1.0
035 * @since Blackjack v1.0
036 */
037 public class Rules {
038
039 private boolean _dealerStandOnSoft17 = true;
040 private boolean _canSurrenderEarly = false;
041 private boolean _canDoubleAfterSplit = true;
042 private boolean _isResplittingAcesAllowed = false;
043 private boolean _isDoubleDownRestricted = false;
044 private boolean _canSurrenderLate = true;
045 private int _maxSplits = 3;
046 private int _numDecks = 6;
047 private double _blackjackPayoff = 1.5;
048 private double _shuffleLimit = 0.2;
049
050 /**
051 * Property key that sets what the blackjack multiple is; equal to
052 * <code>"blackJackPayoff"</code>. The value should be a double.
053 */
054 static final String BLACKJACK_PAYOFF = "blackJackPayoff";
055
056 /**
057 * Property key whose value is whether early surrender is allowed; equal to
058 * <code>"isEarlySurrenderAllowed"</code>. The value should be a boolean.
059 */
060 static final String EARLY_SURRENDER = "isEarlySurrenderAllowed";
061
062 /**
063 * Property key whose value is whether doubling after split is allowed; equal to
064 * <code>"isDoubleAfterSplitAllowed"</code>. The value should be a boolean.
065 */
066 static final String DOUBLE_AFTER_SPLIT= "isDoubleAfterSplitAllowed";
067
068 /**
069 * Property key whose value is whether late surrender is allowed; equal to
070 * <code>"isLateSurrenderAllowed"</code>. The value should be a boolean.
071 */
072 static final String LATE_SURRENDER = "isLateSurrenderAllowed";
073
074 /**
075 * Property key whose value is the maximum number of splits allowed; equal to
076 * <code>"maxNumRegularSplits"</code>. The value should be a int.
077 */
078 static final String MAX_SPLITS = "maxNumRegularSplits";
079
080 /**
081 * Property key whose value is the number of decks in the shoe; equal to
082 * <code>"numberOfDecks"</code>. The value should be an int.
083 */
084 static final String NUM_DECKS = "numberOfDecks";
085
086 /**
087 * Property key whose value is whether splitting already-split aces is allowed; equal to
088 * <code>"isResplittingAcesAllowed"</code>. The value should be a boolean.
089 */
090 static final String RESPLIT_ACES = "isResplittingAcesAllowed";
091
092 /**
093 * Property key whose value is whether double down is allowed on only 9s, 10s, and 11s; equal to
094 * <code>"isDoubleOnlyAllowedOn91011"</code>. The value should be a boolean.
095 */
096 static final String RESTRICTED_DOUBLE = "isDoubleOnlyAllowedOn91011";
097
098 /**
099 * Property key whose value is whether dealer stands on a soft 17; equal to
100 * <code>"doesDealerStandOnSoft17"</code>. The value should be a boolean.
101 */
102 static final String STAND_ON_SOFT_17 = "doesDealerStandOnSoft17";
103
104 /**
105 * Property key whose value is a what percent the deck is shuffled; equal to
106 * <code>"shufflePercentLimit"</code>. The value should be a double 0 to 1 inclusive.
107 */
108 static final String SHUFFLE_LIMIT = "shufflePercentLimit";
109
110 /**
111 * Creates an object with the default rules.
112 */
113 public Rules() {}
114
115 /**
116 * Creates an object with the rules specified by the properties passed in.
117 */
118 public Rules( Properties properties ) {
119 this.load( properties );
120 }
121
122 /**
123 * Returns whether the dealer stands on a soft 17.
124 */
125 public boolean doesDealerStandOnSoft17() {
126 return _dealerStandOnSoft17;
127 }
128
129 /**
130 * Returns the multiple of the original wager that is paid when the player
131 * has blackjack.
132 */
133 public double getBlackjackPayoff() {
134 return _blackjackPayoff;
135 }
136
137 /**
138 * Returns whether the player can double down after splitting.
139 */
140 public boolean canDoubleAfterSplit() {
141 return _canDoubleAfterSplit;
142 }
143
144 /**
145 * Returns whether the player can surrender early.
146 */
147 public boolean canSurrenderEarly() {
148 return _canSurrenderEarly;
149 }
150
151 /**
152 * Returns whether the player can surrender late.
153 */
154 public boolean canSurrenderLate() {
155 return _canSurrenderLate;
156 }
157
158 /**
159 * Returns the maximum number of splits allowed in one hand.
160 */
161 public int getMaxSplits() {
162 return _maxSplits;
163 }
164
165 /**
166 * Returns the number of decks used in the shoe for this game.
167 */
168 public int getNumberOfDecks() {
169 return _numDecks;
170 }
171
172 /**
173 * Returns whether splitting already-split aces is allowed.
174 */
175 public boolean isResplittingAcesAllowed() {
176 return _isResplittingAcesAllowed;
177 }
178
179 /**
180 * Returns whether double down is only allowed on 9s, 10s, and 11s.
181 */
182 public boolean isDoubleDownRestricted() {
183 return _isDoubleDownRestricted;
184 }
185
186 /**
187 * Returns the percent use, as a double between 0 and 1, at which the shoe is reshuffled.
188 */
189 public double getShuffleLimit() {
190 return _shuffleLimit;
191 }
192
193 void setMaxSplits(Properties props) {
194 String value = props.getProperty( MAX_SPLITS );
195 if ( value != null ) {
196 try {
197 setMaxSplits( Integer.parseInt( value ) );
198 }
199 catch ( NumberFormatException nfe ) {}
200 }
201 }
202
203 void setMaxSplits(int i) {
204 _maxSplits = i;
205 }
206
207 private void setBlackjackPayoff(Properties props) {
208 String value = props.getProperty( BLACKJACK_PAYOFF );
209 if ( value != null ) {
210 try {
211 setBlackjackPayoff( java.lang.Double.parseDouble( value ) );
212 }
213 catch ( NumberFormatException nfe ) {}
214 }
215 }
216
217 void setBlackjackPayoff( double payoff ) {
218 _blackjackPayoff = payoff;
219 }
220
221 void setNumberOfDecks(Properties props) {
222 String value = props.getProperty( NUM_DECKS );
223 if ( value != null ) {
224 try {
225 _numDecks = Integer.parseInt( value );
226 }
227 catch ( NumberFormatException e ) {}
228 }
229 }
230
231 void setShuffleLimit(Properties props) {
232 String value = props.getProperty( SHUFFLE_LIMIT );
233 if ( value != null ) {
234 try {
235 _shuffleLimit = java.lang.Double.parseDouble( value );
236 }
237 catch ( NumberFormatException nfe ) {}
238 }
239 }
240
241 void setDealerStandOnSoft17( boolean value ) {
242 _dealerStandOnSoft17 = value;
243 }
244
245 private void load(Properties props) {
246 setBlackjackPayoff( props );
247 setMaxSplits( props );
248 setNumberOfDecks( props );
249 setShuffleLimit( props );
250 setDealerStandOnSoft17(
251 Boolean.valueOf( props.getProperty( STAND_ON_SOFT_17 ) ).booleanValue() );
252 _canSurrenderEarly =
253 Boolean.valueOf( props.getProperty( EARLY_SURRENDER ) ).booleanValue();
254 _canSurrenderLate =
255 Boolean.valueOf( props.getProperty( LATE_SURRENDER ) ).booleanValue();
256 setCanDoubleAfterSplit(
257 Boolean.valueOf( props.getProperty( DOUBLE_AFTER_SPLIT ) ).booleanValue() );
258 setIsResplittingAcesAllowed(
259 Boolean.valueOf( props.getProperty( RESPLIT_ACES ) ).booleanValue() );
260 _isDoubleDownRestricted =
261 Boolean.valueOf( props.getProperty( RESTRICTED_DOUBLE ) ).booleanValue();
262 }
263
264 void setIsResplittingAcesAllowed(boolean b) {
265 _isResplittingAcesAllowed = b;
266 }
267
268 void setCanDoubleAfterSplit(boolean b) {
269 _canDoubleAfterSplit = b;
270 }
271
272 void setCanSurrenderLate( boolean canSurrenderLate ) {
273 _canSurrenderLate = canSurrenderLate;
274 }
275
276 void setCanSurrenderEarly(boolean value) {
277 _canSurrenderEarly = value;
278 }
279 }
|