package TicTacToe;

/**
 * Matthew Amdur
 * MBA
 * 
 * This class will represent the X's for our wonderful game. All the x's
 * in this game are actually triangles, but don't tell. This class also
 * subclasses off Piece and GP.Graphics.Filledrectangle, so it inherits much
 * of its functionality. This piece differs from BlankPiece in that it also has
 * an instance of a GP.Graphics.FramedTriangle. We have a method that tells the
 * square to show its shape, in this case, it shows its triangle.
 **/

public class XPiece extends Piece implements BoardSize {
  /**We need a reference to a triangle.**/

  private GP.Graphics.FramedTriangle _triangle;

  /**
   * This is the constructor of the XPiece class. It uses the abstract Piece classes
   * constructor to set its size, and then it makes a new triangle and sets its 
   * position so that the triangle appears inside it. It then hides the triangle
   * so that if we wanted to, we could instantiate an XPiece without having the X
   * appear, so that we could hide the entire thing until it was ready to be used 
   * in the game. Also, the reason we hide the triangle is to show how you can
   * use the Show() and Hide() methods on any GP.Graphics to make them appear
   * and disappear! (This is a learning tool you know...)
   */

  public XPiece(GP.Container cont, int xIndex, int yIndex) {
    /**We want to set the size by using the super class's constructor.**/

    super(cont);

    /**We need a reference to the shape that the square is going to show on the 
      *board, so the user knows where the X's and O's are. We first instantiate the
      *triangle, then set its color and its position, and then hide it. So all the
      *XPiece has to do to draw its X is have the ShowShape() method called on it.
      * We set the position the same way we set the squares position, see the 
      * comment below for more info.**/

    _triangle = new GP.Graphics.FramedTriangle(cont);
    _triangle.Hide();
    _triangle.SetColor(new GP.Attributes.Colors.Yellow());
    _triangle.SetPosition(new GP.Attributes.Position(Positioner.GetPosition(xIndex), Positioner.GetPosition(yIndex)));
    
    /**We need to set the position of the squares to make it look like a TicTacToe
      *Board. Rather than copying all the math for every place we need to position,
      *or instantiating a poistioner class, we have a class Positioner that has a
      *static method. To learn more about Static methods, look at the Positioner
      *class. Quickly, it is a method that can be called without having to 
      *instaniate the class. This method takes the array index and computes the
      *on screen position. **/

    this.SetPosition(new GP.Attributes.Position(Positioner.GetPosition(xIndex), Positioner.GetPosition(yIndex)));
  }

  /**
   * This method is redefined to show the triangle that the XPiece square contains.
   * This method must be redefined here since it is in the absract super class, and
   * once it is called the visual representation of the X appears on the Board. 
   */

  public void ShowShape() {
    _triangle.Show();
  }

  /**
   * This method is used to check to see if there is a TicTacToe, since we add
   * up the values of each row and column. Since an XPiece  helps
   * you get a TicTacToe, it returns -1. This way, if the number we get when we
   * add all the values in a row is equal to the negative size of the Board, we know
   * we have a TicTacToe. It is a polymorphic solution to the problem of seeing if
   * we have the correct number of pieces in a row.
   */

  public int Counter() {
    return -1;
  }
}
