#include <iostream.h>
#include <apmatrix.h>

void displayrules();
void fillmatrix(apmatrix<char> &othello);
void getmove(apmatrix<char> &othello, char turn, int &row, int &column);
void changeboard(apmatrix<char> &othello, int row, int column, char turn);
void showboard(const apmatrix<char> &othello);
void showscore(const apmatrix<char> &othello);
void win(const apmatrix<char> &othello);
void quit();
bool islegal(apmatrix<char> &othello, int row, int column);

const int SIDE=8;

int main()
{
	int movecount=0;
	char turn;
	int row=0;
	int column=0;
	displayrules();
	apmatrix<char> othello(SIDE, SIDE);
	fillmatrix(othello);
	showboard(othello);
	showscore(othello);
	while (movecount<SIDE*SIDE-4)
	{
		if (movecount%2==0)
			turn='W';
		else
			turn='B';
		getmove(othello, turn, row, column);
		changeboard(othello, row, column, turn);
		showboard(othello);
		showscore(othello);
		movecount++;
	}
	win(othello);
	quit();

	return 0;
}//end main

void displayrules()
{
	cout<<"The game begins with two white markers diagonally opposite each other"<<endl
		<<"in the middle of the board and two black marders on the other diagonal."<<endl
		<<"White goes first by placing a white marker adjacent to any other marker"<<endl
		<<"on the board then flipping any black markers that are between the marker"<<endl
		<<"white just placed and any other white marker in a straight line either"<<endl
		<<"up, down, left, right, or diagonal.  Then black moves the same.  The"<<endl
		<<"objective is to have the most markers in your color at the end of the game"<<endl
		<<"when the board is full of markers"<<endl;
	cout << "Enter you moves, row number first, then a space, then the column, then Enter." << endl;
	cout << "Press enter to continue." << endl;
	cin.get();
}

void fillmatrix(apmatrix<char> &othello)
{
	for(int rows=0; rows<SIDE; ++rows)
	{
		for (int col=0; col<SIDE; ++col)
		{
			othello[rows][col]='.';
		}
	}
	othello[SIDE/2-1][SIDE/2-1]='W';
	othello[SIDE/2][SIDE/2]='W';
	othello[SIDE/2-1][SIDE/2]='B';
	othello[SIDE/2][SIDE/2-1]='B';
}

void getmove(apmatrix<char> &othello, char turn, int &row, int &column)
{
	bool goodmove=false;
	do
	{
		cout << endl << "              Row Column" << endl;
		if (turn=='B')
			cout << "Black's move: "; 
		else 
			cout << "White's move: ";
		cin >> row >> column;
		if (((1<=row) && (row<=SIDE)) && ((1<=column) && (column<=SIDE) && islegal(othello, row-1, column-1)))
		{
			row--;
			column--;
			goodmove=true;
		}
		else
			cout << "That is an illegal move." << endl;	
	}
	while(!goodmove);
}

void changeboard(apmatrix<char> &othello, int row, int column, char turn)
{
	char other;
	if (turn=='W')
		other='B';
	else
		other='W';

	int col2=column;
	int row2=row;
	while ((col2>0) && (othello[row][col2-1]==other))
	{
		col2--;
	}
	if (col2>0)
		if (othello[row][col2-1]==turn)
			for (int x=col2; x<column; x++)
				othello[row][x]=turn;
	col2=column;
	while ((col2<SIDE-1) && (othello[row][col2+1]==other))
	{
		col2++;
	}
	if (col2<SIDE-1)
		if (othello[row][col2+1]==turn)
			for (int x=column; x<col2+1; x++)
				othello[row][x]=turn;

	while ((row2>0) && (othello[row2-1][column]==other))
	{
		row2--;
	}
	if (row2>0)
		if (othello[row2-1][column]==turn)
			for (int x=row2; x<row; x++)
				othello[x][column]=turn;

	while ((row2<SIDE-1) && (othello[row2+1][column]==other))
	{
		row2++;
	}
	if (row2<SIDE-1)
		if (othello[row2+1][column]==turn)
			for (int x=row; x<row2+1; x++)
				othello[x][column]=turn;	

	row2=row;
	col2=column;
	while((row2<SIDE-1) && (col2<SIDE-1) && (othello[row2+1][col2+1]==other))
	{
		row2++;
		col2++;
	}
	if ((row2<SIDE-1) && (col2<SIDE-1))
		if (othello[row2+1][col2+1]==turn)
		{
			col2=column;
			for (int x=row; x<row2+1; x++)
			{
				othello[x][col2]=turn;
				col2++;
			}
		}
	
	row2=row;
	col2=column;
	while((row2<SIDE-1) && (col2>0) && (othello[row2+1][col2-1]==other))
	{
		row2++;
		col2--;
	}
	if ((row2<SIDE-1) && (col2>0))
		if (othello[row2+1][col2-1]==turn)
		{
			col2=column;
			for (int x=row; x<row2+1; x++)
			{	
				othello[x][col2]=turn;
				col2--;
			}
		}
	
	row2=row;
	col2=column;
	while((row2>0) && (col2<SIDE-1) && (othello[row2-1][col2+1]==other))
	{
		row2--;
		col2++;
	}
	if ((row2>0) && (col2<SIDE-1))
		if (othello[row2-1][col2+1]==turn)
		{
			row2=row;
			for (int x=column; x<col2+1; x++)
			{
				othello[row2][x]=turn;
				row2--;
			}
		}

	row2=row;
	col2=column;
	while((row2>0) && (col2>0) && (othello[row2-1][col2-1]==other))
	{
		row2--;
		col2--;
	}
	if ((row2>0) && (col2>0))
		if (othello[row2-1][col2-1]==turn)
		{
			for (int x=col2; x<column; x++)
			{
				othello[row2][x]=turn;
				row2++;
			}
		}
	
	othello[row][column]=turn;
}

void showboard(const apmatrix<char> &othello)
{
	cout << "   ";
	for (int count=1; count<=SIDE; count++)
		cout << count << "  ";
	cout << endl;
	for (int row=0; row<SIDE; row++)
	{
		cout << row+1 << ' ';
		for (int column=0; column<SIDE; column++)
		{
			cout << "[" << othello[row][column] << "]";
		}
		cout << endl;
	}
}

void showscore(const apmatrix<char> &othello)
{
	int black=0;
	int white=0;
	for (int row=0; row<SIDE; row++)
		for (int column=0; column<SIDE; column++)
		{
			if (othello[row][column]=='B')
				black++;
			if (othello[row][column]=='W')
				white++;
		}
	cout << "White: " << white << '\t' << "Black: " << black << endl;
}

void quit()
{
}

bool islegal(apmatrix<char> &othello, int row, int column)
{
	if (othello[row][column]!='.')
		return false;

	if (row>0)
	{
		if (column>0)
			if (othello[row-1][column-1]!='.')
				return true;
		if (othello[row-1][column]!='.')
			return true;
		if (column<SIDE-1)
			if (othello[row-1][column+1]!='.')
				return true;
	}

	if (column>0)
		if (othello[row][column-1]!='.')
			return true;
	
	if (column<SIDE-1)
		if (othello[row][column+1]!='.')
			return true;

	if (row<SIDE-1)
	{
		if (column>0)
			if (othello[row+1][column-1]!='.')
				return true;

		if (othello[row+1][column]!='.')
			return true;

		if (column<SIDE-1)
			if (othello[row+1][column+1]!='.')
				return true;
	}
	return false;
}

void win(const apmatrix<char> &othello)
{
	int black=0;
	int white=0;
	for (int row=0; row<SIDE; row++)
		for (int column=0; column<SIDE; column++)
		{
			if (othello[row][column]=='B')
				black++;
			if (othello[row][column]=='W')
				white++;
		}
	if (black==white)
		cout << "It's a tie!" << endl;
	if (black>white)
		cout << "Black wins!" << endl;
	if (white>black)
		cout << "White wins!" << endl;
}