1 package shoot_the_moon
;
4 import java
.util
.Collections
;
8 public abstract class Player
implements Observer
{
10 private List
<Card
> hand
;
12 private List
<Card
> readOnlyHand
;
14 private Trump currentTrump
;
16 private Bid lastHighestBid
;
18 private Card leadCard
;
20 private Position position
;
22 private GameSettings settings
;
25 * Called when the player is dealt a card
26 * @param c the card dealt
28 public final void acceptCard( Card c
) {
29 //if our hand hasn't been initialized, first create it
31 hand
= ListFactory
.makeList();
32 readOnlyHand
= Collections
.unmodifiableList( hand
);
40 * Called to tell a Player what his position is at the table. Should never
41 * be called by a subclass of Player.
44 * the player's assigned position
46 public void acceptPosition( Position position
) {
47 this.position
= position
;
51 * Called to make a Player make his/her bid. Should never be called by a
54 * @return the Bid that this player is making
56 public final Bid
makeBid() {
57 Bid bid
= decideBid( lastHighestBid
);
63 * Called by Game event to send events to the Player. Should never be called
64 * by a subclass of Player.
67 * An Event sent from the Game
69 public final void observeEvent( Event e
) {
70 if ( e
instanceof GameStartEvent
) {
71 GameStartEvent ge
= (GameStartEvent
) e
;
72 settings
= ge
.getSettings();
74 if ( e
instanceof ContractMadeEvent
) {
75 ContractMadeEvent cme
= (ContractMadeEvent
) e
;
76 currentTrump
= cme
.getWinningBid().getTrump();
77 } else if ( e
instanceof BidMadeEvent
) {
78 BidMadeEvent bne
= (BidMadeEvent
) e
;
79 if ( lastHighestBid
!= null ) {
80 if ( bne
.getBid().isBetterThan( lastHighestBid
) ) {
81 lastHighestBid
= bne
.getBid();
85 lastHighestBid
= bne
.getBid();
87 } else if ( e
instanceof RoundStartEvent
) {
88 lastHighestBid
= null;
92 } else if ( e
instanceof TrickWonEvent
) {
94 } else if ( e
instanceof CardPlayedEvent
) {
95 if ( leadCard
== null ) {
96 CardPlayedEvent cpe
= (CardPlayedEvent
) e
;
97 leadCard
= cpe
.getCard();
104 * Called by a Game to make this Player play a card This function is not to
105 * be called by a subclass.
107 * @return the Card this player is playing
109 public final Card
playCard() {
110 List
<Card
> legalCards
= Rules
.getLegalCards( hand
, leadCard
,
112 Card card
= decideCard( legalCards
);
113 if ( hand
.contains( card
) ) {
117 throw new RuntimeException( "Player tried to play illegal card." );
122 * Called to make a Player decide a bid
124 * @param lastHighestBid :
125 * the lastHighestBid observed by the Player this round (may be
127 * @returns Bid b where b.getNumber() == Bid.PASS or b.isGreaterThan(
130 protected abstract Bid
decideBid( Bid lastHighestBid
);
133 * Called to make a Player decide on a Card to play
135 * @param legalCards :
136 * a list of the legal cards to play based on the current
137 * observed state of the round
138 * @returns Card c such that legalCards.contains( c ) == true;
140 protected abstract Card
decideCard( List
<Card
> legalCards
);
143 * Called whenever a Player observes an event happening in a Game
148 protected abstract void processEvent( Event e
);
151 * Gets the player's name
152 * @return the player's name
154 public abstract String
getName();
157 * Returns what the trump is currently, according to the messages sent to
158 * this Player. Returns null if no trump has been named.
160 protected final Trump
getCurrentTrump() {
165 * Function to get the read-only view of a player's hand. Returns null if no
166 * hand has been assigned yet.
168 * @returns read-only view of this player's hand (List<Card>)
170 protected final List
<Card
> getHand() {
175 * Based on Events passed to the Player, returns the last highest bid made
178 * @return null if no bids have been made yet this round, the highest bid
179 * observed this round otherwise
181 protected final Bid
getLastHighestBid() {
182 return lastHighestBid
;
186 * Based on events passed to the Player, returns the lead card in a trick.
187 * Returns null if no lead card has been played in the current trick.
189 protected final Card
getLeadCard() {
194 * @return the PlayerPosition assigned to this player
196 protected final Position
getPosition() {
201 * @return the settings passed to this player by the game
203 protected final GameSettings
getSettings() {