package TTT;

//  This is the board class.  The Board will be the place where all the
// pieces need to exist and where all the gameplay will go on.  It will be
//made of a 3x3 array of Square's that will be able to be clicked on.  I
//chose an array because there are many things that need to happen between
//different squares on the board (like seeing if the game is over) and an
//array seemed like the simplest way to "hold" all the squares together so
//that they could know about each other.

public class Board extends GP.Containers.Frame implements TicInterface {

//  The board needs a 3x3 array for the rows of x's and o's
private Square[][] _array;

// if we want to pass our container to anyone else - we need an INSTANCE
// VARIABLE of it

private GP.Container _container;

//  Here is how we monitor whose turn it is

private int _turn = 0;

//  Here is the constructor - this is where we "make" a board

public Board(GP.Container container) {

//  call the superclass constructor and tell it where this class belongs
//graphically

super(container);

//  we need to set the old container equal to our container

_container = container;

// give me a color for the board and a size

this.SetColor(new GP.Attributes.Colors.Red());
this.SetSize(new GP.Attributes.Size(500,500));

// let's instantiate the array - note:  it's elements are still null

_array = new Square[3][3];

//  Now - let's put squares in each of the array spots with values of 
// blank spots.  each square will need three states - "x" "o" or "empty";

for (int i=0; i < 3; i++) {
	for (int j=0; j < 3 ; j++) {
	
//  this will loop through the array and fill in the squares with new
// Squares that have a null or "empty" value for their value in the game -
// they'll also need a reference to the Board to do stuff with it:  also,
//not only should the array know about the squares, but the squares should
//know where in the array they exist and that is why we pass on the
//integers i and j.

	_array[i][j] = new Square(this, null, this, i, j);
	_array[i][j].SetPosition(new
	GP.Attributes.Position(i*SIZE+SIZE*2, j*SIZE+SIZE*2));

} // end j
} // end i

// now our board is set up and we are all set for the constructor;
}

// There are other methods that this class  will need but I need to go 
// and fill in the other classes that I'll need like squares and players
// before I get to the functionality of the methods here.

//Now I'm back - I need end game checking!!!

//  Here is the most difficult part of the program - checking to see if
// you have won.  It is not that tricky but it is a little bit complicated

public void checkGameOver(Square square, String s) {

// everytime we call this it means it is the next guys turn

_turn++;

// this is so we know if someone won

boolean won = false;

// loop through vertically checking all wins in the array

for (int i=0; i<3; i++) {
	int count = 0;
	for (int j=0; j<3; j++) {

//  This will check the first vertical column of the array, then the
//  second, then the third

	if (_array[i][j].state() == s) {
	count++;
	if (count == 3) {
		won = true;
	}
	}
}
}
//  loop through horizontally to check all parts of the array - same as
//  above

for (int i=0; i<3; i++) {
        int count = 0;
        for (int j=0; j<3; j++) {
        if (_array[j][i].state() == s) {
        count++;
        if (count == 3) {
                won = true;
        }
        }

}
}

//  Special Case for diagonals
// check and see - if middle is not yours no diagonals can exist

if (_array[1][1].state() == s) {

// left to right diagonal - easier case - (0,0)(1,1)(2,2)

	int count = 0;
	for (int x=0; x<3; x++) {
		if (_array[x][x].state() == s) {
		count++;
		}
	}
	if (count == 3) {
	won = true;
	}

//  THE UGLY STEP - RIGHT TO LEFT DIAGONAL!!! (2,0)(1,1)(0,2)

	if (_array[2][0].state() == s && _array[1][1].state() == s
&& _array[0][2].state() == s) {
	won = true;
	}
}

if (won == true) {

// game over syndrome - end game!

for (int i=0; i < 3; i++) {
        for (int j=0; j < 3 ; j++) {

// Fill in the array with new squares that say who won in a random color
 
        _array[i][j] = new Square(this, null, null, 0,0);
	_array[i][j].SetSize(new GP.Attributes.Size(SIZE-1, SIZE-1));
        _array[i][j].SetPosition(new
GP.Attributes.Position(i*SIZE+SIZE*2,
j*SIZE+SIZE*2));
	_array[i][j].SetColor(new GP.Attributes.Colors.Random()); 
	GP.Components.Label l = new GP.Components.Label(this, s + " won");
	l.SetPosition(_array[i][j].GetPosition());
	_array[i][j].setGameOver(true);
}
}       
	
}	

//  let's say it is a tie - same as above but the graphic must say tie!!!

if (_turn == 9 && won == false) {

for (int i=0; i < 3; i++) {
        for (int j=0; j < 3 ; j++) {
                
// Fill in the array with new squares that say who won in a random color
         
        _array[i][j] = new Square(this, null, null, 0,0);
        _array[i][j].SetSize(new GP.Attributes.Size(SIZE-1, SIZE-1));
        _array[i][j].SetPosition(new
GP.Attributes.Position(i*SIZE+SIZE*2,
j*SIZE+SIZE*2));
        _array[i][j].SetColor(new GP.Attributes.Colors.Random());
        GP.Components.Label l = new GP.Components.Label(this,"TIE");
        l.SetPosition(_array[i][j].GetPosition());
        _array[i][j].setGameOver(true);
}
} 

}

			
}

//  This is a way to see if it is x's turn or not 
//  This could have been done more oorientedly but the case was too simple
// not to do with an incremented instance variable

public int whoseTurn() {
return _turn%2;
}



} //  end class

