//board_functions.cpp
//by John Ahlschwede
//defines member functions of board class

#include "board.h"

board::board()
{

	boardInit();	

	return;
}




int board::formatMove(int cup)
{
	//appropriate input is 1 - 6
	//the following section converts the input to such an integer
	if(cup < 0)
		cup = 14+(cup % 14);
	if(cup > 13)
		cup = cup % 14;
	if(cup > 7)
		cup = cup - 7;

	if(firstPlayersTurn)
	{
	
		while((cup==0)||(cup==7)||(cups[cup]<1))
		{
			cup++;

			if(cup > 6)
				cup = 1;
		}
	
		return cup;
	}

	else
	{
	
		while((cup==0)||(cup==7)||(cups[cup+7]<1))
		{
			cup++;

			if(cup > 6)
				cup = 1;
		}
	
		return cup;
	}


}

bool board::move(int cup)
{	

	
	//invalid choices include the mancalas (0 & 7), numbers not between 1 and 13, and empty cups 
	if((cup <1)||(cup==7)||(cup>13)||(cups[cup]==0))
	{
		cout << endl << endl << "INVALID MOVE!!!" << endl << endl << flush;
		firstPlayersTurn=!firstPlayersTurn; //forefit turn (?)
		return false;
	}
	
	int hand = cups[cup];
	cups[cup]=0;
	while(hand>0)
	{

		cup=(cup+1)%14;//move hand forward one cup
		if(cup == 0)
			if(!firstPlayersTurn) //if it's the second player's turn
			{
				hand--;
				cups[cup]++;
				scoreSecondPlayer++;
				if(hand==0)
					return true; //exactly ran out of beads at mancala: get exta turn
			}
		if(cup == 7)
			if(firstPlayersTurn) //if it's the first player's turn
			{
				hand--;
				cups[cup]++;
				scoreFirstPlayer++;
				if(hand==0)
					return true; //exactly ran out of beads at mancala: get exta turn
			}
		if((cup != 0)&&(cup != 7))//if not mancala
		{
			cups[cup]++;
			hand--;
			if(hand==0)//if hand empty
			{

				//first player ends turn on own side in empty cup
				if(firstPlayersTurn && (cup < 7) && (cups[cup]==1))
				{
					cups[cup]=0;//pick up stone played
					int net=1+(cups[14-cup]);//total netted from move
					scoreFirstPlayer+=net;//add net to score
					cups[7]+=net;//add net to mancala
					cups[14-cup]=0;//pick up stones opposite stone played
					firstPlayersTurn=!firstPlayersTurn;
					return false;
				}


				//second player ends turn on own side in empty cup
				if((!firstPlayersTurn) && (cup > 7) && (cups[cup]==1))
				{
					cups[cup]=0;//pick up stone played
					int net=1+(cups[14-cup]);//total netted from move
					scoreSecondPlayer+=net;//add net to score
					cups[0]+=net;//add net to mancala
					cups[14-cup]=0;//pick up stones opposite stone played
					firstPlayersTurn=!firstPlayersTurn;
					return false;
				}
			}//END EMPTY HAND
		}//END OF CODE FOR PLACING BEAD IN NON-MANCALA
	}
	firstPlayersTurn = (!firstPlayersTurn);
	return false; //go to next player's turn
}


bool board::gameOver()
{
	int firstSide=cups[1]+cups[2]+cups[3]+cups[4]+cups[5]+cups[6];
	int secondSide=cups[8]+cups[9]+cups[10]+cups[11]+cups[12]+cups[13];
	if((firstSide==0)||(secondSide==0))
	{
		cups[0]+=secondSide;
		scoreSecondPlayer+=secondSide;
		cups[7]+=firstSide;
		scoreFirstPlayer+=firstSide;
		for(int i=0; i<14;i++)
		{
			if((i!=0)&&(i!=7))
				cups[i]=0;
		}
		return true;
	}
	return false;
}





void board::randomizeBoard()
{
	for(int i=0; i<14; i++) //set all cups to zero
		cups[i]=0;

	srand(time(NULL));
	cups[(rand()%6)+1]=1;//places one bead on first side
	cups[(rand()%6)+8]=1;//places one bead on second side

	for(int j=0; j<46; j++) //place other 46 stones anywhere on board, randomly
	{
		cups[rand()%14]++;
	}

}



void board::boardInit()
{
	for(int i =0; i<14; i++)
		cups[i]=4;
	cups[0]=0;
	cups[7]=0;

	firstPlayer=NULL;
	secondPlayer=NULL;
	scoreFirstPlayer=0;
	scoreSecondPlayer=0;
	firstPlayersTurn=true;

}



bool board::playSetTurns(player *first, player *second, int turns)
{
	firstPlayer=first;
	secondPlayer=second;
	
	int reversedBoard[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	int temp=0;

	while((turns>0)&&(!gameOver()))
	{
		while(firstPlayersTurn&&!gameOver())
		{
			temp=first->exe(cups);
			temp=formatMove(temp);
			move(temp);
		}

		while((!firstPlayersTurn)&&(!gameOver()))
		{
			for(int i=0; i<7; i++) //sets up reversed board for opposite player
			{
				reversedBoard[i]=cups[i+7];
				reversedBoard[i+7]=cups[i];
			}


			temp=second->exe(reversedBoard);
			temp=formatMove(temp);
			move(temp+7);
		}
		
		
		turns--;
	}
	return true;
}


bool board::playGame(player *first, player *second)
{

	firstPlayer=first;
	secondPlayer=second;
	
	int reversedBoard[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	int temp = 0;

	
	while(!gameOver())
	{
		
		while((firstPlayersTurn)&&(!gameOver()))
		{
			temp=first->exe(cups);
			temp=formatMove(temp);
			move(temp);
		}
		while((!firstPlayersTurn)&&(!gameOver()))
		{
			for(int i=0; i<7; i++) //sets up reversed board for opposite player
			{
				reversedBoard[i]=cups[i+7];
				reversedBoard[i+7]=cups[i];
			}

			temp=second->exe(reversedBoard);
			temp=formatMove(temp);
			move(temp+7);

		}
	}
	return true;
}


void board::printResults()
{
	cout << endl << endl;
	cout << "First: " << scoreFirstPlayer << endl;
	cout << "Second: " << scoreSecondPlayer << endl;
/*	cout << endl << endl;
	for(int i=0; i<14; i++)
		cout << "cup " << i << "\t\t" << cups[i] << endl;*/
}


void board::setCups(int cupValues[14])
{
	for(int i=0; i<14; i++)
		cups[i]=cupValues[i];
}

int *board::getCups()
{
	return cups;
}

bool board::getTurn()
{
	return firstPlayersTurn;
}


int board::getRating() //returns cups[7] - cups[0]
{
	return cups[7] - cups[0];
}