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

#include <iomanip>	
using std :: setw;

const int rows = 3;
const int column = 3;

void PrintTicTacToe( char [][3] );	//The function prototpyes for this program
void p1( char [][3]);
void p2( char [][3]);
bool WinVert( char [][3]);
bool WinHori( char [][3]);
bool WinDiag( char [][3]);

void main()	//The start of the main function block.
{
	char TicTacToe[rows][column];	//Declaring an array.

	for (int i=0; i <rows; i++)	//Initializing the array to contain the element '*'.
		for (int j=0; j < column; j++)
			TicTacToe[i][j] = '*';

	PrintTicTacToe(TicTacToe);	//Displaying the array elements

	p1(TicTacToe);	//Excutes the player1 and player2 prompts to key in coordinates for their marks.
	p2(TicTacToe);	//A preset 9 rounds of prompt.
	p1(TicTacToe);
	p2(TicTacToe);
	p1(TicTacToe);
	p2(TicTacToe);
	p1(TicTacToe);
	p2(TicTacToe);
	p1(TicTacToe);
		
	if(WinVert(TicTacToe))	//Checks the array for matching horizontal, vertical or diagonal win.
		cout << "vert";

	else if(WinHori(TicTacToe))
		cout << "Hori";

	else if(WinDiag(TicTacToe))
		cout << "diag";
	else
		cout << "Its a Draw.";
}	//End of main function block.

void PrintTicTacToe( char tttoe[][3])	//Function parameters for all the prototypes.
{
	
	for(int i=0; i<rows; i++)
		for(int j=0; j<column; j++)
		{
			cout << tttoe[i][j] << (j%3==2 ? '\n' : '|');
			
			if (j == 2 && i <= 1)
				cout << "-----\n";
		}
}

void p1( char a[][3])	//player1 function parameter.
{
	int P1row;
	int P1column;

	cout << "\nPlayer 1, please key in the row you want place your 'X'(0-2): ";
	cin >> P1row;			
	cout << "Player 1, please key in the column you wish to place your 'X'(0-2): ";
	cin	>> P1column;
	cout << endl;
					
	if((P1row >=0 || P1row < 3) && (P1column >= 0 || P1column <3 ))
	{
		a[P1row][P1column] = 'X';
		PrintTicTacToe(a);
	}
	else
		cout << "Wrong input. Goodbye!";
	
}

void p2( char a[][3])	//player 2 function prototype.
{
	int P2row;
	int P2column;

	cout << "\nPlayer 2, please key in the row you want place your 'X'(0-2): ";
	cin >> P2row;			
	cout << "Player 2, please key in the column you wish to place your 'X'(0-2): ";
	cin	>> P2column;
	cout << endl;
					
	if( (P2row <= 2 || P2row >= 0) && (P2column <=2 || P2column >=0) )
	{
		a[P2row][P2column] = 'O';
		PrintTicTacToe(a);
	}
	else
		cout << "Wrong input. Goodbye!";
}

bool WinVert( char atoe[][3]) //Boolean data type to check for winning rows/column.
{
	return (atoe[0][0] == atoe[0][1] == atoe[0][2] || 
			atoe[1][0] == atoe[1][1] == atoe[1][2] || 
			atoe[2][0] == atoe[2][1] == atoe[2][2]);
}

bool WinHori( char btoe[][3])
{
	return (btoe[0][0] == btoe[1][0] == btoe[2][0] || 
			btoe[0][1] == btoe[1][1] == btoe[2][1] || 
			btoe[0][2] == btoe[1][2] == btoe[2][2]);
}

bool WinDiag( char ctoe[][3])
{
	return (ctoe[0][0] == ctoe[1][1] == ctoe[2][2] || 
			ctoe[0][2] == ctoe[1][1] == ctoe[2][0]);
}

Recommended Answers

All 9 Replies

Don't just post code. Ask a question.

my program cant tell who is the winner, how to stop a player than hav put a mark on a mark place and is there anything to change..

Member Avatar for iamthwee

So have you tried debugging it, placing a few couts here and there to see where it's going wrong?

my program can work.. but i cant find out the winner instantly..

Um... assuming you have written that code (which looks very neat), it seems odd you can't do some simple debugging. Do what "iamthewee" said.

thanks... im new to C++.. i try every method but still cant find out..

Member Avatar for iamthwee

thanks... im new to C++.. i try every method but still cant find out..

Nope, go away, and put lots of couts in strategic places.

Do they show what you expect, if not why?

When you're done come back and we'll be happy to help.

Nope, go away, and put lots of couts in strategic places.

Do they show what you expect, if not why?

When you're done come back and we'll be happy to help.

i tried but still cant... anymore help?

> atoe[0][0] == atoe[0][1] == atoe[0][2]
This line doesn't do what you think it does. The evaluation of this expression is from left to right so the result of the first comparison(between first and second term) is then compared with the third term.

Create a function called 'threeEqual' with something like this:

bool threeEqual(char a, char b, char c)
{
    return((a == b) && (b == c));
}

// and call it in your program as

bool WinVert( char atoe[][3]) //Boolean data type to check for winning rows/column.
{
    return (threeEqual(atoe[0][0],atoe[0][1],atoe[0][2]) ||
            threeEqual(atoe[1][0],atoe[1][1],atoe[1][2]) ||
            threeEqual(atoe[2][0],atoe[2][1],atoe[2][2]));
}

Plus you have interchanged the names of the functions. The one named WinHori should be WinVert and vice a versa.

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.