scg/ch05/OneDie

From FANG

Jump to: navigation, search

01 package scg.ch05;
02 
03 import fang2.core.Game;
04 import fang2.sprites.StringSprite;
05 
06 /**
07  * This class, extending StringSprite, represents one six-sided die.
08  * That is, a cube used for generating random numbers. The faces are
09  * marked from 1 to 6.
10  */
11 public class OneDie
12   extends StringSprite {
13   /** the value of the die; range is 1-6 */
14   private int face;
15 
16   /**
17    * Initialize a OneDieStringSprite object with default values
18    */
19   public OneDie() {
20     setFace(1);
21   }
22 
23   /**
24    * Get the current value of face.
25    *
26    @return  [1..6], the current value of the die
27    */
28   public int getFace() {
29     return face;
30   }
31 
32   /**
33    * Roll this die: get currentGame(), use randomInt and setFace.
34    */
35   public void roll() {
36     setFace(Game.getCurrentGame().randomInt(16));
37   }
38 
39   /**
40    Set value of face to newFace and update displayed value if newFace
41    * is legal; otherwise leave face unchanged.
42    *
43    @param  newFace  the new value for face
44    */
45   public void setFace(int newFace{
46     if ((<= newFace&& (newFace <= 6)) {
47       face = newFace;
48       setText(Integer.toString(face));
49     }
50   }
51 }
52 
53 //Uploaded on Mon Mar 29 21:42:31 EDT 2010


Download/View scg/ch05/OneDie.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