// 7/18/02
// Bryan's recitation implementation of 2 player tic tac toe.

#include <iostream.h>

char board[3][3];

// reset game function
void init();

// menu function
void menu();

// returns marker for a given player
char getMarker(int player);

// get player move function(player num, int pos_x, int pos_y)
int move(int num, int pos_x, int pos_y);

// draws out the board
void draw_board();

int game_won();

void main()
{
	int player=0, winner, pos_x, pos_y;
	char choice = 'y';

	init();

	cout << "Welcome to Tic-Tac-Toe!" << endl;
	while (choice=='y' || choice=='Y') {
		while ((winner = game_won()) == -1) {
			do {
				cout << "Player " << getMarker(player) << " where do you want to move?";
				cin >> pos_x >> pos_y;
			} while (!move(player, pos_x, pos_y));
			draw_board();
			player=(player+1)%2;
		}
		if (winner == -2)
			cout << "No one wins this game!" << endl;
		else
			cout << "Player " << getMarker(winner) << " won the game." << endl;

		cout << "Would you like to play again? (Hit Y for yes, N for no) ";
		cin >> choice;
		init();
	}
}

// Initialized Board
void init()
{
	char blank = ' ';
	for (char Row=0; Row < 3; Row++) {
		// Display a row
		for (char Col=0; Col < 3; Col++) {
			// Display a column
			board[Row][Col] = blank;
		}
	}
}

// Assigns O or X to Player 0 or 1, respectively
char getMarker(int player)
{
	if		(player == 0) return 'O';
	else if (player == 1) return 'X';
}

int move(int player, int pos_x, int pos_y)
{
	if ((pos_x<=1) &amp;&amp; (pos_x<=3) &amp;&amp; (pos_y<=1) &amp;&amp; (pos_y<=3)) {
		if (board[pos_x-1][pos_y-1]==' ') {
			board[pos_x-1][pos_y-1] = getMarker(player);
			return 1;
		} else {
			cout << endl << "The position is already taken" << endl;
			return 0;
		}
	} else {
		cout << endl << "That spot is not on the board." << endl;
		return 0;
	}
}

void draw_board()
{
	for (int index=0; index<3; index++) {
		cout << "+-+-+-+" << endl;
		cout << "|";
		for (int indices=0; indices<3; indices++) {
			cout << board[index][indices] << "|";
		}
		cout << endl;
	}
	cout << "+-+-+-+" << endl;
}

// returns player number that won, -1 if no win, -2 if draw
int game_won()
{
	int x,y,xnum=0,onum=0;
	
	// detect row wins
	for (x=0;x<3;x++) {
		for(y=0;y<3;y++) {
			if (board[x][y]=='X'){
				xnum++;
				if (xnum==3) return 1;
			} else if (board[x][y]=='O') {
				onum++;
				if (onum==3) return 0;
			}
		}
		xnum=onum=0;
	}
	
	xnum=onum=0;

	// detect column wins
	for (x=0;x<3;x++) {
		for(y=0;y<3;y++) {
			if (board[y][x]=='X'){
				xnum++;
				if (xnum==3) return 1;
			} else if (board[y][x]=='O') {
				onum++;
				if (onum==3) return 0;
			}
		}
		xnum=onum=0;
	}

	xnum=onum=0;

	// detect diagonal wins
	for (x=0;x<3;x++) {
		if (board[x][x]=='X') {
				xnum++;
				if (xnum==3) return 1;
		} else if (board[x][x]=='O') {
				onum++;
				if (onum==3) return 0;
		}
	}

	xnum=onum=0;

	for (x=0;x<3;x++) {
		if (board[x][2-x]=='X') {
				xnum++;
				if (xnum==3) return 1;
		} else if (board[x][2-x]=='O') {
				onum++;
				if (onum==3) return 0;
		}
	}
	
	// detect draws
	for (x=0; x<3; x++) {
		for (y=0; y<3; y++) {
			if (board[x][y] == ' ') return -1;
		}
	}
	return -2;
}


