Initial add (with Netbeans project and build script)
[shoot.git] / src / shoot_the_moon / Player.java
blob21c5e2c5fcaad3f73e2e495985169c26b5dd9820
1 package shoot_the_moon;
4 import java.util.Collections;
5 import java.util.List;
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;
24 /**
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
30 if ( hand == null ) {
31 hand = ListFactory.makeList();
32 readOnlyHand = Collections.unmodifiableList( hand );
35 //add the card
36 hand.add( c );
39 /**
40 * Called to tell a Player what his position is at the table. Should never
41 * be called by a subclass of Player.
43 * @param position :
44 * the player's assigned position
46 public void acceptPosition( Position position ) {
47 this.position = position;
50 /**
51 * Called to make a Player make his/her bid. Should never be called by a
52 * subclass of Player.
54 * @return the Bid that this player is making
56 public final Bid makeBid() {
57 Bid bid = decideBid( lastHighestBid );
59 return bid;
62 /**
63 * Called by Game event to send events to the Player. Should never be called
64 * by a subclass of Player.
66 * @param e :
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();
84 else {
85 lastHighestBid = bne.getBid();
87 } else if ( e instanceof RoundStartEvent ) {
88 lastHighestBid = null;
89 currentTrump = null;
90 leadCard = null;
91 hand = null;
92 } else if ( e instanceof TrickWonEvent ) {
93 leadCard = null;
94 } else if ( e instanceof CardPlayedEvent ) {
95 if ( leadCard == null ) {
96 CardPlayedEvent cpe = (CardPlayedEvent) e;
97 leadCard = cpe.getCard();
100 processEvent( e );
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,
111 currentTrump );
112 Card card = decideCard( legalCards );
113 if ( hand.contains( card ) ) {
114 hand.remove( card );
115 return card;
116 } else {
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
126 * null)
127 * @returns Bid b where b.getNumber() == Bid.PASS or b.isGreaterThan(
128 * lastHighestBid )
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
145 * @param e :
146 * the observed Event
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() {
161 return currentTrump;
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() {
171 return readOnlyHand;
175 * Based on Events passed to the Player, returns the last highest bid made
176 * this round.
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() {
190 return leadCard;
194 * @return the PlayerPosition assigned to this player
196 protected final Position getPosition() {
197 return position;
201 * @return the settings passed to this player by the game
203 protected final GameSettings getSettings() {
204 return settings;