package TicTacToe;

/**
 * Matthew Amdur
 * MBA
 *
 * This class models a behavior that we add to our squares, which in turn contain
 * the representations of our x's and o's for the game. This class extends off of
 * the GP.Behaviors.Perpetual, so it inherits all the functionality that it needs in
 * order to keep calling its Apply method.
 *
 * What is a perpetual behavior? It is something you add to an object that does 
 * something over and over again. It has an Apply(double time) method, where time 
 * is the delay in milliseconds between when Apply gets called. As a result, you
 * place the code you want to have called over and over again in this method, and
 * GP takes care of calling the method over and over again.
 *
 * What does this behavior do? This behavior takes an object and constantly, or
 * as fast as it can, changes the object's color.
 *
 * How does a behavior know to start? Good question. Since we are subclassing,
 * the super class defines a Start(double time) method, that in turn calls Apply.
 * So, to use a behavior we add it to the object we want it to act on, and then
 * tell the Behavior to start. There is also a stop method that can be used
 * to stop the behavior from calling Apply.
 **/

public class ColorBehavior extends GP.Behaviors.Perpetual {

  /** The behavior takes any GP.Graphics.Shape, because any shape knows how to set
    * its color. It takes this as opposed to a Piece because this way if you ever
    * needed a color changing behavior, you could just instantiate this class. This
    * is a good example of code reuse, as this behavior can be added to any 
    * GP.Graphics.Shape to make in its color change randomly. **/

  private GP.Graphics.Shape _piece;

  /**
   * This is the constructor for the ColorBehavior class. We need a reference to
   * the object whose color we are going to change, so we set the behaviors 
   * reference here.
   */

  public ColorBehavior(Piece piece) {
    _piece = piece;
  }

  /**
   * This method has been redefined to change the shape's color. This method is
   * called every time miliseconds by GP, and everytime it is called it comes up
   * with a new random color to set the object's color as.
   */

  public void Apply(double time) {
    /**One constructor of a GP.Attributes.Color takes three integers, the red, 
      *green, and blue values of the color you want. We use a random number
      *factory provided by the support code to get a random number. I don't use
      *the 0 to 3 nor the 240 to 256 colors, since they end up making it too
      *dark.**/
    
    int red = cs015.SP.RandomNumberFactory.RandomNumber(3, 240);
    int green = cs015.SP.RandomNumberFactory.RandomNumber(3, 240);
    int blue = cs015.SP.RandomNumberFactory.RandomNumber(3, 240);
    
    /**Now that we have three random numbers, we can change the shape's color. **/

    _piece.SetColor(new GP.Attributes.Color(red, green, blue));
  }
}
