package TTT;

// this is the class for the square that I'll need.  
//  To give it more functionality that a normal 
// GP.Graphics.FilledRectangle
// I will have it extend from the class and do more neat things

public class Square extends GP.Graphics.FilledRectangle implements
TicInterface{

// this square will need a color, container, a state (x or o or empty) ,
// and a size, and a reference to the board and a location in the array  -
// I'll do that in the constructor so they'll be around all the time
//  Those are all things that a regular GP.Graphic does not know about
//  That is why I am extending instead of instantiating a Rectangle


// so squares know where to be
private GP.Container _container;

//  the board needs to be around to tell it to do things
private Board _board;
//  the state is important for knowing who won
private String _state;
//  the array locations of this specific square
private int _xloc, _yloc;
//  is the game over - a boolean will know!!!
private boolean _over = false;

// CONSTRUCTOR - MAKE THE SQUARE!!!


public Square(GP.Container container, String state, Board board, int x,
int y) {

//  I need to make sure this is a Square so I'll need to call the
// superclass

super(container);

//  let's keep the container around for later - it is a good habit to have
//  even if you don't use it later - it will avoid null pointers

_container = container;

//  Same thing with the state - especially since it will be changing
// during the program - it is essential that it is an instance variable

_state = state;

//  Board too - we'll need this reference to get information from the
// board

_board = board;

//  OUR squares will be black and a little small so that we can see the
//  background color as lines

this.SetColor(new GP.Attributes.Colors.Black());
this.SetSize(new GP.Attributes.Size(SIZE-1, SIZE-1));

//  we'll want to keep track of where we are in the array so we can check
//  endgame

_xloc = x;
_yloc = y;

}

// we will need to be able to change the String that represents
// our squares state so a little method that does that would be great
// now - we can mutate the value of my reference to the state

public void setState(String aString) {
_state = aString;
}

//  Let's say someone wants to find out my state - they need to be able
// to get it - here is a method which does that

public String state() {
return _state;
}

// same thing with our positions in the array

public int getX() {
return _xloc;
}

public int getY() {
return _yloc;
}

//  we need to be able to set if the game is over

public void setGameOver(boolean b) {
_over = b;
}


//  ok - so someone is going to click on me to do something - what do I
// need to do?  first - whose turn is it?  I'll ask the board -

public void React() {

//  if state == null means is it an empty square? and if _over == flase
//  checks to see if someone has won  

if (_state == null && _over == false) {

int turn = _board.whoseTurn();

// now that I know whose turn it is - I can set my State to their shape -
// and I can set my visible representation to match that pick


if (turn == 1) {

//  if the number is odd - it is x's turn

this.setState("x");
this.makeX();
}

//  if it is even O's Turn

else {
this.setState("o");
this.makeO();
}

// Now we need to see if the game is over - we'll let the board do that

_board.checkGameOver(this, _state);
}
// the method is over

}

//  here is where we will make an X  It will be a small textual X

public void makeX() {

// a label is just a way of moving text around like a graphic

GP.Components.Label x = new GP.Components.Label(_container, "X");
x.SetSize(new GP.Attributes.Size(SIZE*2, SIZE*2));
x.SetPosition(this.GetPosition());

}

//  same as above


public void makeO() {
GP.Components.Label y = new GP.Components.Label(_container, "O");
y.SetSize(new GP.Attributes.Size(SIZE*2, SIZE*2));
y.SetPosition(this.GetPosition());

}

} // class 
