I have this code for a tic tac toe game, it`s not done yet, and what I have done isn`t working like I want it to.
The screan prints and promps player 1 and 2 for inputs one at a time, but my while loop in main is probably wrong, the loop continues even after the grid if fool of X and O, I want the program to end when it`s done, and then I`ll finish my checkWinner function.
somebody told me to change my functions to a bool, but I`m not sure how to do that.

Can anybody help me?

#include <iostream>
#include <iomanip>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

using std::setw;

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

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

// program execution begins
int main()
{
	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
	ticTacToe( ticTac ); // function call
	while(ticTac[row][column] != BLANK) 	
	{		
		player1(ticTac);		
		player2(ticTac);	
	}			
		cout << endl; 
	

	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;
		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;
		ticTacToe( ticTac );
}

/*void checkWinner(const string ticTac[][column])
{
	for (int i = 0; i > row; i++)
	{
		for (int j = 0; j > column; j++)
		{
			//check rows for winner
		   if (ticTac[0][0] == ticTac[1][0] && ticTac[1][0] == ticTac[2][0])
		   {
			   cout << "YOU ARE THE WINNER!";
		   }
		   else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1])
		   {
			   cout << "YOU ARE THE WINNER!";
		   }
		   else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][1] == ticTac[2][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   //check for winner diagnally
		   else if (ticTac[2][0] == ticTac[1][1] && ticTac[1][1] == ticTac[0][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   else if ([0][0] == ticTac[1][1] && ticTac[1][2] == ticTac[2][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
			//check columns for winner
		   else if ([0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   else if ([1][0] == ticTac[1][1] && ticTac[1][1] == ticTac[1][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   else if ([2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		}
	}
}*/

I commented this last function out because I want to get just what`s in main working properly first before finishing it

Recommended Answers

All 3 Replies

Add a for statement at the top to end when X>8 (with the original X being 0), you can force x to be greater than 8 in either one of the winner stages by simply adding 9 to X, and as the program goes along, every time a player takes a turn, the computer could add one onto X every time a player makes a move, that way, either someone will win, or the board will be full and the for will detect this and go to the finish

I didn`t get it, this is what I did, I don`t think this is what you meant

for (int i = 0; i = row; i++)
	{
		for (int j = 0; j = column; j++)
		{
		   while(ticTac[row][column] != BLANK) 	
			{		
				player1(ticTac);		
				player2(ticTac);	
			}			
		cout << endl;
		 break;
		}
	}

it`s doing the same thing

I think the problem is you need to convert your checkWinner function to return a bool (true if there's a win or a tie and false if not). As I was attempting to do that I was thinking that you really should try to pare down player1() and player2() into one method (like VernonDozier suggested in the other post). That way, your while loop in main would simply be:

int counter=0;
string marker;
bool winner = false;
while(!winner) 	
	{		
	     if(counter % 2 == 0)
                     marker = "X";
             else
                      marker = "O";
                         
               player(ticTac,marker);
		
              winner = checkWinner(ticTac));
	      counter++;
	}

You could turn it into a do-while and pull that checkWinner function right into the loop condition. Anyway, just suggestions but as it stands your loop just sits there and does nothing. Try to implement the modified checkWinner function, it's not much more beyond what you have and returns true at points after you've checked for a win (it's got a flaw in it currently as you are checking all the "BLANK" ones versus each other and getting that there is a win with 3 blanks. You'll need to add a const to your function prototype to match what you have in the definition.

EDIT: I suppose you could use the counter to keep track of the turns too and use that in your loop condition for a tie rather than building it into checkWinner

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.