scg/ch10/ScoreSprite

From FANG

Jump to: navigation, search

01 package scg.ch10;
02 
03 import fang2.sprites.StringSprite;
04 
05 /**
06  * Implements a score keeper. Score is kept (and displayed) as a ratio
07  * between wins and games played. Initially the score is set to 0/0
08  * (zero games won of zero games played). The win method records a
09  * winning game; the lose method records a loss.
10  */
11 public class ScoreSprite
12   extends StringSprite {
13   /** number of games played */
14   private int games;
15 
16   /** number of games won */
17   private int wins;
18 
19   /**
20    * Default constructor: 0/0 is initial score.
21    */
22   public ScoreSprite() {
23     this(00);
24   }
25 
26   /**
27    * Create new ScoreSprite with the given score. Games must be greater
28    * than or equal to wins (or else wins will be reset to games).
29    *
30    @param  wins   games won
31    @param  games  games played
32    */
33   public ScoreSprite(int wins, int games{
34     this.wins = wins;
35     this.games = games;
36     validateScore();
37     fixTextDisplay();
38   }
39 
40   /**
41    * Record a losing game.
42    */
43   public void lose() {
44     ++games;
45     fixTextDisplay();
46   }
47 
48   /**
49    * Record a winning game.
50    */
51   public void win() {
52     ++games;
53     ++wins;
54     fixTextDisplay();
55   }
56 
57   /**
58    * Implementation method: display the currently recorded score.
59    */
60   private void fixTextDisplay() {
61     setText(wins + "/" + games);
62   }
63 
64   /**
65    * Make sure that the score makes sense; if there are more wins than
66    * games, reset wins to match games.
67    */
68   private void validateScore() {
69     if (games < wins{
70       wins = games;
71     }
72   }
73 }
74 
75 //Uploaded on Mon Mar 29 21:42:15 EDT 2010


Download/View scg/ch10/ScoreSprite.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games