#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

#include "gameBoard.h"
#include "ai.h"
#include <windows.h>
#include <time.h>

void playGame(bool=false);
void getInput(int &, int &, gameBoard &);

int main() {
	srand( static_cast<unsigned int>(time(NULL)) );

	bool demoPlay=false;

	char choice;

	do {
		playGame(demoPlay);
		cout << "\nWould you like to play again?";

		bool validChoice=false;
		while ( !validChoice ) {
			if ( demoPlay ) {
				validChoice=true;
				choice='y';
				Sleep(1000);
			} else {
				choice=_getch();
			}
			if ( choice == '0' ) {
				validChoice=true;
				demoPlay=true;
				choice='y';
			}
			if ( choice == 'y' || choice == 'Y' || 
				 choice == 'n' || choice == 'N' ) validChoice=true;
		} // end while validChoice

	} while ( choice=='y' || choice=='Y' );

	return 0;
}

void playGame(bool demoPlay) {
	
	ai aiPlayer[PLAYERS];

	for ( int i=0; i < PLAYERS; i++ ) {
		char input;
		cout << "\nPlayer '" << PLAYER_CELL[i] << "' is (H)uman or (C)omputer?: ";
		if ( demoPlay ) {
			input='c';
			Sleep(1000);
		} else {
			input=_getch();
		}
		cout << input << endl;
		if ( input == 'c' || input == 'C' ) 
			aiPlayer[i].activate(i);
	}

	gameBoard myGameBoard;
	
	int inputRow=0,inputCol=0;
	int gameState=0;
	int currentPlayer=0;
	coord aiMove;

	// MAIN GAME LOOP //
	do {
		myGameBoard.display();
		if ( aiPlayer[currentPlayer].isActive() ) {
			// AI player's turn
			aiMove = aiPlayer[currentPlayer].makeMove(myGameBoard);
			myGameBoard.captureCell(aiMove.row, aiMove.col, currentPlayer);
		} else {
			// Human player's turn
			do {
				cout << "Turn: Player # " << ( currentPlayer+1 ) << " ('" << PLAYER_CELL[currentPlayer] << "')" << endl;
				getInput(inputRow,inputCol,myGameBoard);
				inputRow--; inputCol--;
			} while  ( !(myGameBoard.captureCell(inputRow,inputCol,currentPlayer)) );
		}
		currentPlayer++;
		if ( currentPlayer >= PLAYERS ) currentPlayer=0;

		gameState=myGameBoard.getGameState();
	} while (gameState == GAME_IN_PROGRESS);

	myGameBoard.display();

	cout << "\nGame Over!\n";
	if ( gameState == GAME_TIE ) 
		cout << "Game ends in a tie\n";
	else if ( gameState >= 0 ) 
		cout << "Player " << (gameState+1) << " (" << PLAYER_CELL[gameState] << ") Wins!\n";

}

void getInput(int & row, int & col, gameBoard & board) {

	char key=NULL;
	bool colSet=false,rowSet=false;

	cout << "\nEnter Row/Column.\nPress (R) to redraw the game board.\n";

	do {

		key=_getch();

		if ( key == 'R' || key == 'r' ) { 
			board.display(); 
			cout << "\nEnter Row/Column.\nPress (R) to redraw the game board.\n";
		}

		if ( key == 'A' || key == 'a' ) 
		{
			col=1;
			colSet=true;
			cout << "[Column A] ";
		}
		else if ( key == 'B' || key == 'b' )
		{
			col=2;
			colSet=true;
			cout << "[Column B] ";
		}
		else if ( key == 'C' || key == 'c' )
		{
			col=3;
			colSet=true;
			cout << "[Column C] ";
		}

		if ( key >= '1' && key <= '3' ) 
		{
			row=key-48;
			rowSet=true;
			cout << "[Row " << row << "] ";
		}

	} while ( !(rowSet && colSet) );

}