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 package garrettsmith.blackjack;
025
026 import garrettsmith.playingcards.CardList;
027
028 /**
029 * Represents a player's move, such as hitting or standing.
030 *
031 * @author Garrett Smith, gsmith at northwestern dot edu
032 * @version Blackjack v1.0
033 * @since Blackjack v1.0
034 */
035 public interface Move {
036
037 /**
038 * The numeric value of the move used only internally for equality comparison.
039 */
040 public int value();
041
042 /**
043 * Executes the move; should only be invoked internally by the framework.
044 */
045 public Hand execute( Hand hand, EventHandler handler, CardList dealerCards );
046
047 /**
048 * Single instance of the move that represents hitting.
049 */
050 public static final Move HIT = new Hit();
051
052 /**
053 * Single instance of the move that represents standing.
054 */
055 public static final Move STAND = new Stand();
056
057 /**
058 * Single instance of the move that represents surrendering.
059 */
060 public static final Move SURRENDER = new Surrender();
061
062 /**
063 * Single instance of the move that represents doubling down.
064 */
065 public static final Move DOUBLE = new Double();
066
067 /**
068 * Single instance of the move that represents splitting the hand.
069 */
070 public static final Move SPLIT = new Split();
071 }
072
073 abstract class BaseMove implements Move {
074
075 public abstract int value();
076
077 public int hashCode() {
078 return this.getClass().getName().hashCode();
079 }
080
081 public boolean equals( Object o ) {
082 return ( o != null
083 && this.getClass().isInstance( o )
084 && ( (Move) o).value() == this.value() );
085 }
086
087 public Hand execute( Hand hand, EventHandler handler, CardList dealerCards ) {
088 if ( ! hand.isMoveAllowed( this ) ) {
089 throw new NotAllowedException( this.toString() );
090 }
091 return doMove( hand, handler, dealerCards );
092 }
093
094 protected abstract Hand doMove( Hand hand, EventHandler handler, CardList dealerCards );
095 }
096
097 final class Hit extends BaseMove {
098
099 Hit() {}
100
101 public int value() {
102 return 0;
103 }
104
105 public Hand doMove(
106 final Hand hand,
107 final EventHandler handler,
108 final CardList dealerCards ) {
109 hand.hit();
110 return null;
111 }
112 }
113
114 final class Stand extends BaseMove {
115
116 Stand() {}
117
118 public int value() {
119 return 1;
120 }
121
122 public Hand doMove(
123 final Hand hand,
124 final EventHandler handler,
125 final CardList dealerCards ) {
126 hand.stand();
127 return null;
128 }
129 }
130
131 final class Surrender extends BaseMove {
132
133 Surrender() {}
134
135 public int value() {
136 return 2;
137 }
138
139 public Hand doMove(
140 final Hand hand,
141 final EventHandler handler,
142 final CardList dealerCards ) {
143 hand.surrender();
144 handler.handFinished(
145 hand,
146 -0.5 * hand.getWager(),
147 Result.LATE_SURRENDER,
148 dealerCards );
149 return null;
150 }
151 }
152
153 final class Double extends BaseMove {
154
155 Double() {}
156
157 public int value() {
158 return 3;
159 }
160
161 public Hand doMove(
162 final Hand hand,
163 final EventHandler handler,
164 final CardList dealerCards ) {
165 hand.doubleDown();
166 return null;
167 }
168 }
169
170 final class Split extends BaseMove {
171
172 Split() {}
173
174 public int value() {
175 return 4;
176 }
177
178 public Hand doMove(
179 final Hand hand,
180 final EventHandler handler,
181 final CardList dealerCards ) {
182 return hand.split();
183 }
184 }
|