scg/ch10/Player

From FANG

Jump to: navigation, search

001 package scg.ch10;
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   /**
053    * Increment the player's score by the given amount. score silently
054    * left unchanged if it would be negative.
055    *
056    @param  delta  amount to add to the player's score.
057    */
058   private void incrementScore(int delta{
059     setScore(score + delta);
060   }
061 
062   /**
063    * Update the player's score.
064    *
065    @param  score  the new score value; silently ignored (score
066    *                unchanged) if negative value passed in
067    */
068   private void setScore(int score{
069     if (score >= 0{
070       this.score = score;
071     }
072   }
073 
074   /**
075    * Take this player's turn. The video game loop is here, a little
076    * tilted on its side. The state update happens at the top of the
077    * loop. Since the loop keeps going around and around, this is fine.
078    */
079   public void takeTurn() {
080     int turnTotal = 0;
081     boolean rolledPig = false;
082     boolean heldPoints = false;
083     while (!rolledPig && !heldPoints{
084       // ----- update state of turn -----
085       int roll = rollOneDie();
086       rolledPig = (roll == 1);
087       // ----- show state of turn -----
088       if (rolledPig{
089         System.out.println(name + ": Rolled PIG!");
090       } else {
091         System.out.println(name + ": Rolled " + roll);
092       }
093       // ----- get user input for turn -----
094       if (!rolledPig{
095         turnTotal += roll;
096         heldPoints = Pig.answersYes(name + " with " + turnTotal +
097             " points this turn, would you like to hold your points?");
098       }
099     }
100 
101     // Could have gotten here for two different reasons; only update
102     // score if player held the points.
103     if (heldPoints{
104       incrementScore(turnTotal);
105     }
106 
107     System.out.println(name + " ends turn with " + score + " points.");
108   }
109 
110   /**
111    * Get a string representation for this player. The string
112    * representation is the name, a colon, and the current score.
113    *
114    @return  string representation of this {@link Player}
115    */
116   @Override
117   public String toString() {
118     return name + ": " + score;
119   }
120 
121   /**
122    * Roll one 6-sided die. A random number on the rannge [1-n] can be
123    * generated with a random number on the range [0-n) + 1 (there are n
124    * different values and the smallest value is 1).
125    *
126    @return  random int on [1-6]
127    */
128   private int rollOneDie() {
129     return random.nextInt(6+ 1;
130   }
131 }
132 
133 //Uploaded on Mon Mar 29 21:40:49 EDT 2010


Download/View scg/ch10/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