laelzio.mosca 0 Light Poster

Please help!

// Laelzio Mosca
// 12/09/09
// Tic Tac Toe game

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

#include <string>
using std::string;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

//global variables
const int row = 3; //size of array
const int column = 3; // size of array
string BLANK = " ";
string ticTac[row][column];

static int nspots = 9; // number of possible spots

void ticTacToe(const string ticTacToe[][column]); //Function prototypes
void player1(string ticTacToe[][column]);
void player2(string ticTac[][column]);
void generateNumber(string ticTac[][column]);
void checkWinner(string ticTac[][column]);
void checkToWin(string ticTac[][column];

// program execution begins
int main()
{
	srand( time(0));
	int numberOfPlayers;

	string ticTac[row][column] = {{BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}}; //number display for rows and columns 
	cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
	cout << "Enter the number of players with a max of 2 and press enter: ";
	cin >> numberOfPlayers;
	if (numberOfPlayers == 2)
	{
		ticTacToe( ticTac ); // function call

			   while (nspots > 0) 	
				{		
					player1(ticTac);	
					checkWinner(ticTac);

					if (nspots < 0) break;
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
					player2(ticTac);

					checkWinner(ticTac);
					// the following should never happen
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
				//	cout << "nspots: " << nspots << endl;
				}
			   cout << endl;
			
			   fflush(stdin); // clear the buffer, and wait to exit.
			   cout << "\nPress enter to exit " << endl;
			   getc(stdin);
	}
	else
	{
		ticTacToe( ticTac ); // function call

			   while (nspots > 0) 	
				{		
					player1(ticTac);	
					checkWinner(ticTac);

					if (nspots < 0) break;
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
					generateNumber(ticTac);	

					checkWinner(ticTac);
					// the following should never happen
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
				}
			   cout << endl;
			
			   fflush(stdin); // clear the buffer, and wait to exit.
			   cout << "\nPress enter to exit " << endl;
			   getc(stdin);
	}

	return 0;
}

//Function ticTacToe
void ticTacToe(const string ticTacToe[][column])
{
	cout << setw(5)<<"|"<<setw(5)<< "|" <<endl;

	//for loop-continuation condition and increment
	for (int i = 0; i < row; i ++)
	{
		for (int j = 0; j < column; j++)
			//check to output '|' only in between the numbers, not at the end
			if ( j < column - 1)
			{
				cout << setw(3)<< ticTacToe[i][j] << setw(2)<<"|";
			}
			else					
				cout << setw(3)<<ticTacToe[i][j];
							
			cout << endl;

			// check to make sure the horizontal line in only printed in between
			// the 3 rows, not on the bottom fo the picture
			if ( i < row -1)
			{
				cout << "____|____|____"<<endl;
				cout << setw(5) << "|" << setw(5) << "|" << endl;
			}
			else
				cout << setw(5) << "|" << setw(5) << "|" << endl;		
			
	}
}

void player1(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p1 = "X";

	cout << "Player one can enter your coordinates for X." << endl;
		cin >> x >> y ;
		x--, y--;
		while (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'X'." << endl;
			player1(ticTac);
		}
		else {
			ticTac[x][y] = p1;
			nspots--;
		}
		ticTacToe( ticTac );
		
}

void player2(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p2 = "O";

	cout << "Player two can enter you coordinates for 'O'. ";
		cin >> x >> y;
	x--,y--;
		while  (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'O'." << endl;
			player2(ticTac);
		}
		else {
			ticTac[x][y] = p2;
			nspots--;
		}
		ticTacToe( ticTac );
}

void generateNumber(string ticTac[][column])
{

	int a; // number 1
	int b; // number 2
	string gn = "O";
	
	
	do {
		a = (rand() % 3); // generate number 1
	} while (a > 2);
	do {
		b = (rand() % 3); // generate number 2
	} while (b > 2);


		if (ticTac[a][b] != BLANK)
		{
			generateNumber(ticTac);
		}
		else
		{
			ticTac[a][b] = gn;	
			nspots--;
		}
	ticTacToe( ticTac );
}
void checkWinner(string ticTac[][column])
{
			//check columns for winner // column 1
		   if ((ticTac[0][0] == ticTac[1][0]) && (ticTac[1][0] == ticTac[2][0]) && (ticTac[0][0] != BLANK))
		   {
			   cout << "YOU ARE THE WINNER!";
			   nspots = -1;
		   } // column 2
		   else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1] && (ticTac[0][1] != BLANK))
		   {
			   cout << "YOU ARE THE WINNER!";
			   nspots = -1;
		   } // column 3
		   else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][2] == ticTac[2][2] && (ticTac[0][2] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
		   //check for winner diagnally
		   else if (ticTac[2][0] == ticTac[1][1] && ticTac[1][1] == ticTac[0][2] && (ticTac[2][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
		   else if (ticTac[0][0] == ticTac[1][1] && ticTac[1][1] == ticTac[2][2] && (ticTac[0][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
			//check rows for winner  // row 1
		   else if (ticTac[0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2] && (ticTac[0][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   } // row 2 
		   else if (ticTac[1][0] == ticTac[1][1] && ticTac[1][1] == ticTac[1][2] && (ticTac[1][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   } // row 3
		   else if (ticTac[2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2] && (ticTac[2][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
}

void checkToWin(string ticTac[][column]
{
	int nx = 0;
	for (int i = 0; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
			if (ticTac[][])= "X";
			nx++;
			
		}

	}
}

I want to make the computer play harder in this program, so I`m trying to create this last function to check to see if computer can win, if yes, mark the square, if not, then check to see if player can win, if so, mark the box to block, if neither of these possiblilities are true, then generate a random number.
I`m not sure how to check every row, column, diagonals, and make the computer move to win or block.
in the checkToWin() function, nx means number of X`s
row and column = 3

thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.