1 package shoot_the_moon
;
4 import java
.io
.PrintStream
;
6 public class TextObserver
implements Observer
{
9 public TextObserver( PrintStream ps
) {
11 ps
.print( "**********************\n"
12 + "* SHOOT THE MOON *\n"
13 + "**********************\n\n" );
16 public void observeEvent( Event e
) {
17 if ( e
instanceof GameStartEvent
) {
18 GameStartEvent gse
= (GameStartEvent
) e
;
19 ps
.print( "The game is now starting. The settings are:\n" +
20 "\t" + gse
.getSettings().getNumPlayersPerTeam() + "\t players per team.\n" +
21 "\t" + gse
.getSettings().getNumDuplicateCards() + "\t copies of each card in the deck" +
23 } else if ( e
instanceof RoundStartEvent
) {
24 RoundStartEvent rse
= (RoundStartEvent
) e
;
25 ps
.print( "\t" + getPositionString( rse
.getDealer() ) + " deals the cards.\n" );
26 } else if ( e
instanceof BidMadeEvent
) {
27 BidMadeEvent bme
= (BidMadeEvent
) e
;
28 ps
.print( "\t\t" + getPositionString( bme
.getPosition() ) + " bids " + bme
.getBid().toString() + "\n");
29 } else if ( e
instanceof ContractMadeEvent
) {
30 ContractMadeEvent cme
= (ContractMadeEvent
) e
;
31 ps
.print( "\t" + getPositionString( cme
.getWinningPosition() ) + " wins the contract at " + cme
.getWinningBid().toString() +"\n\n");
32 } else if ( e
instanceof CardPlayedEvent
) {
33 CardPlayedEvent cpe
= (CardPlayedEvent
) e
;
34 ps
.print( "\t\t\t" + getPositionString( cpe
.getPosition() ) + " plays the " + cpe
.getCard().getShortString() +"\n" );
35 } else if ( e
instanceof TrickWonEvent
) {
36 TrickWonEvent twe
= (TrickWonEvent
) e
;
37 ps
.print( "\t\t" + getPositionString( twe
.getWinningPlayer() ) + " won the trick.\n\n" );
38 } else if ( e
instanceof RoundOverEvent
) {
39 RoundOverEvent roe
= (RoundOverEvent
) e
;
40 ps
.print( "The tricks taken this round were: \n");
41 for ( Team t
: Team
.allTeams
) {
42 ps
.print( "\t" + t
.getName() + "\t: " + roe
.getTricksTaken( t
) + " tricks\n" );
44 ps
.print( "The contract of " + roe
.getContract() + " made by " + getPositionString( roe
.getContractMaker() ) + " was " +(roe
.isContractMade() ?
"" : "not " ) + "made. The score is now...\n" );
45 for ( Team t
: Team
.allTeams
) {
46 ps
.print( t
.getName() + "\t: " + roe
.getScore( t
) + " points\n" );
49 } else if ( e
instanceof GameOverEvent
) {
50 GameOverEvent goe
= (GameOverEvent
) e
;
51 ps
.print( "The game is over. ");
53 ps
.print( "The game ended in a tie." );
55 ps
.print( goe
.getWinner().getName() + " won the game." );
60 private String
getPositionString( Position p
) {
61 return p
.getName() + " (" + p
.getTeam().getName() + ")";