//Vladimir Tenev
//PIC 10A: Winter 2009
//Tic-Tac-Toe with Classes and Switch Statement
//February 20, 2009

#include "ccc_x11.h"
#include <string>

class TTT_Game{
public:
TTT_Game(); //draws the board
bool isdone() const; //checks to see if there is a winner
bool move_is_valid(int xcoord, int ycoord) const; //checks to see if the space the user has clicked in is available
void drawx(int xcoord, int ycoord); //draws an X in the box with lower left corner (xcoord,ycoord)
void drawo(int xcoord, int ycoord); //draws an O in the box with lower left corner (xcoord,ycoord)
private:
	int turn; //keeps track of which turn we're on
	int move_history[9]; //keeps track of the moves played thus far
	int translate(int xcoord, int ycoord) const; //translates user's click into a "magic square number" that facilitates checking for the winner
};

TTT_Game::TTT_Game()
{
cwin.coord(0,3.1,3,-0.1);
cwin.clear();
turn=0;
for(int x=0; x<4; x++)
	{
	cwin << Line(Point(x,0),Point(x,3)) << Line(Point(0,x),Point(3,x));
	}
}

int TTT_Game::translate(int xcoord, int ycoord) const
{
switch(xcoord*10+ycoord)
	{
	case(0): return 4; break;
	case(1): return 9; break;
	case(2): return 2; break;
	case(10): return 3; break;
	case(11): return 5; break;
	case(12): return 7; break;
	case(20): return 8; break;
	case(21): return 1; break;
	case(22): return 6; break;
	default: return 0;
	}
}

bool TTT_Game::move_is_valid(int xcoord, int ycoord) const
{
int magic_num = translate(xcoord,ycoord);
for (int x=0; x<turn; x++)
	if(move_history[x]==magic_num) return false;
return true;
}

void TTT_Game::drawx(int xcoord, int ycoord)
{
cwin << Line(Point(xcoord,ycoord),Point(xcoord+1,ycoord+1)) << Line(Point(xcoord,ycoord+1),Point(xcoord+1,ycoord));
move_history[turn]=translate(xcoord,ycoord);
turn++;
}

void TTT_Game::drawo(int xcoord, int ycoord)
{
cwin << Circle(Point(xcoord+0.5,ycoord+0.5),0.4);
move_history[turn]=translate(xcoord,ycoord);
turn++;
}

bool TTT_Game::isdone() const //checks for a winner by looking for three numbers (corresponding to the current player's previous moves) that add up to 15
{
int index = turn - 1;//this is the index of move_history which stores the previous move
if ((index%2==0 && index>3)||(index%2==1 && index>4))
	for(int x=index%2; x<=(index-4); x+=2)
		for(int y=x+2; y<=(index-2); y+=2)
			if(move_history[x]+move_history[y]+move_history[index]==15) return true;
return false;
}

int ccc_win_main()
{
string response;
do{
	int turn=1; //note that there is a private variable named turn in the TTT_Game class, and it has been implemented slightly differently. Be sure you can tell the difference between the two!
	TTT_Game newgame;
	Point userclick;
	int xcoord=0;//stores x-coordinate of user's click (intentionally cast to an int so that we get the lower-left corner)
	int ycoord=0;//stores y-coordinate
	while(!newgame.isdone() && turn < 10){ //keeps going until we get a winner or until we've exhausted the board
        	userclick = cwin.get_mouse("Click to move.");
		xcoord = (int) userclick.get_x();
		ycoord = (int) userclick.get_y();
		if(newgame.move_is_valid(xcoord,ycoord))
		{
			if(turn%2==1) newgame.drawx(xcoord,ycoord);
			else newgame.drawo(xcoord, ycoord);
			turn++;
		}	
	}
	if(newgame.isdone() && turn%2==1) cwin << Message(Point(0,0), "O wins.");
	else if(newgame.isdone()) cwin << Message(Point(0,0), "X wins.");
	else cwin << Message(Point(0,0),"Cat's game");
	response = cwin.get_string("Play again?");
}while(response =="Y" || response =="y");
return 0;
}

