scg/ch11/Player

From FANG

Jump to: navigation, search

001 package scg.ch11;
002 
003 import java.util.Random;
004 
005 /**
006  * Represents a Pig player. A player has a name and a score. When it is
007  * the player's turn, takeTurn is called. takeTurn uses console I/O
008  * defined in the Pig class (static methods answersYes and getLine) for
009  * communication with the player.<br />
010  * Invariant: The score will never be negative. The name is immutable
011  * after construction.
012  */
013 public class Player {
014   private static final Random random = new Random();
015 
016   /** this player's name */
017   private final String name;
018 
019   /** this player;s current score */
020   private int score;
021 
022   /**
023    * Construct a new {@link Player}. Players start with a score of 0 and
024    * the given name
025    *
026    @param  name  the player's name
027    */
028   public Player(String name{
029     this.name = name;
030     setScore(0);
031   }
032 
033   /**
034    * Get the player's name.
035    *
036    @return  the player's name
037    */
038   public String getName() {
039     return name;
040   }
041 
042   /**
043    * Get the player's score.
044    *
045    @return  the player's current score.
046    */
047   public int getScore() {
048     return score;
049   }
050 
051   /**
052    * Take this player's turn. The video game loop is here, a little
053    * tilted on its side. The state update happens at the top of the
054    * loop. Since the loop keeps going around and around, this is fine.
055    */
056   public void takeTurn() {
057     int turnTotal = 0;
058     boolean rolledPig = false;
059     boolean heldPoints = false;
060     while (!rolledPig && !heldPoints{
061       // ----- update state of turn -----
062       int roll = rollOneDie();
063       rolledPig = (roll == 1);
064       // ----- show state of turn -----
065       if (rolledPig{
066         System.out.println(name + ": Rolled PIG!");
067       } else {
068         System.out.println(name + ": Rolled " + roll);
069       }
070       // ----- get user input for turn -----
071       if (!rolledPig{
072         turnTotal += roll;
073         heldPoints = Pig.answersYes(name + " with " + turnTotal +
074             " points this turn, would you like to hold your points?");
075       }
076     }
077 
078     // Could have gotten here for two different reasons; only update
079     // score if player held the points.
080     if (heldPoints{
081       incrementScore(turnTotal);
082     }
083 
084     System.out.println(name + " ends turn with " + score + " points.");
085   }
086 
087   /**
088    * Get a string representation for this player. The string
089    * representation is the name, a colon, and the current score.
090    *
091    @return  string representation of this {@link Player}
092    */
093   @Override
094   public String toString() {
095     return name + ": " + score;
096   }
097 
098   /**
099    * Increment the player's score by the given amount. score silently
100    * left unchanged if it would be negative.
101    *
102    @param  delta  amount to add to the player's score.
103    */
104   private void incrementScore(int delta{
105     setScore(score + delta);
106   }
107 
108   /**
109    * Roll one 6-sided die. A random number on the rannge [1-n] can be
110    * generated with a random number on the range [0-n) + 1 (there are n
111    * different values and the smallest value is 1).
112    *
113    @return  random int on [1-6]
114    */
115   private int rollOneDie() {
116     return random.nextInt(6+ 1;
117   }
118 
119   /**
120    * Update the player's score.
121    *
122    @param  score  the new score value; silently ignored (score
123    *                unchanged) if negative value passed in
124    */
125   private void setScore(int score{
126     if (score >= 0{
127       this.score = score;
128     }
129   }
130 }
131 
132 //Uploaded on Mon Mar 29 21:40:10 EDT 2010


Download/View scg/ch11/Player.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