Tic Tac Toe help

I have to write a program for a tic tac toe game. I have the game part done, but I also have to have at the end of the game for it to say whether player 1 won, player 2 won, or it is a tie. I can't seem to figure out how to get it to say who won. Can someone please help? Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace std;

void locations(int &, int &);
void tables(char [][3], int);

int main()
{
	const int ROW = 3;
	const int COLUMN = 3;
	char table[ROW][COLUMN] = { '*', '*', '*',
								'*', '*', '*',
								'*', '*', '*'};
	int rows, columns;
	
	cout << " *** TIC TAC TOE *** " << endl;
	cout << "_____________________\n\n";

	tables(table, ROW);

	cout << "\n Player 1 is X\n Player 2 is O" << endl;
	
	
	for(int count = 0; count < 5; count++)
	{
		if (count < 5)
		{
			cout << "\n Player 1" << endl;
			locations(rows, columns);
			table[rows][columns] = 'X';
			tables(table, ROW);
		}
		if (count < 4)
		{		
			cout << "\n Player 2" << endl;
			locations(rows, columns);
			table[rows][columns] = 'O';
			tables(table, ROW);
		}
     }		
	
	return 0;
}

void locations(int &row, int &column)
{
	cout << " Enter a row (between 0 and 2): ";
	cin >> row;
	while(row < 0 || row > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the row: ";
		cin >> row;
	}
	cout << " Enter a column (between 0 and 2): ";
	cin >> column;
	while(column < 0 || column > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the column: ";
		cin >> column;
	}
}

void tables(char table[][3], int rows)
{
	for(int x = 0; x < rows; x++)
	{
		for(int y = 0; y < 3; y++)
		{
			cout << " " << table[x][y] << " | ";
		}
		cout << endl;
		cout << " ------------- " << endl;
	}
}
There are lots of tic tac toe games posted on this site. I would recommend looking some of them up, to see how other people did it.
Last edited on
I figured out how to get it to check for the winner, but I cannot figure out how to get the game to end after there is a winner. And also, I still cannot figure out how to get it to say "Tied Game" if no one wins.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
using namespace std;

void locations(int &, int &);
void tables(char [][3], int);

int main()
{
	const int ROW = 3;
	const int COLUMN = 3;
	char table[ROW][COLUMN] = { '*', '*', '*',
								'*', '*', '*',
								'*', '*', '*'};
	int rows, columns;
	
	cout << " *** TIC TAC TOE *** " << endl;
	cout << "_____________________\n\n";

	tables(table, ROW);

	cout << "\n Player 1 is X\n Player 2 is O" << endl;
	
	
	for(int count = 0; count < 5; count++)
	{
		if (count < 5)
		{
			cout << "\n Player 1" << endl;
			locations(rows, columns);
			table[rows][columns] = 'X';
			tables(table, ROW);

			if (table[0][0]=='X' && table[0][0]==table[1][1] && table[1][1]==table[2][2]) {cout<<"Player 1 wins!"<<endl;}
			if (table[2][0]=='X' && table[2][0]==table[1][1] && table[1][1]==table[0][2]) {cout<<"Player 1 wins!"<<endl;}
			if (table[0][0]=='X' && table[0][0]==table[1][0] && table[1][0]==table[2][0]) {cout<<"Player 1 wins!"<<endl;}
			if (table[0][1]=='X' && table[0][1]==table[1][1] && table[1][1]==table[2][1]) {cout<<"Player 1 wins!"<<endl;}
			if (table[0][2]=='X' && table[0][2]==table[1][2] && table[1][2]==table[2][2]) {cout<<"Player 1 wins!"<<endl;}
			if (table[0][0]=='X' && table[0][0]==table[0][1] && table[0][1]==table[0][2]) {cout<<"Player 1 wins!"<<endl;}
			if (table[1][0]=='X' && table[1][0]==table[1][1] && table[1][1]==table[1][2]) {cout<<"Player 1 wins!"<<endl;}
			if (table[2][0]=='X' && table[2][0]==table[2][1] && table[2][1]==table[2][2]) {cout<<"Player 1 wins!"<<endl;}
		}

		if (count < 4)
		{		
			cout << "\n Player 2" << endl;
			locations(rows, columns);
			table[rows][columns] = 'O';
			tables(table, ROW);

			if (table[0][0]=='O' && table[0][0]==table[1][1] && table[1][1]==table[2][2]) {cout<<"Player 2 wins!"<<endl;}
			if (table[2][0]=='O' && table[2][0]==table[1][1] && table[1][1]==table[0][2]) {cout<<"Player 2 wins!"<<endl;}
			if (table[0][0]=='O' && table[0][0]==table[1][0] && table[1][0]==table[2][0]) {cout<<"Player 2 wins!"<<endl;}
			if (table[0][1]=='O' && table[0][1]==table[1][1] && table[1][1]==table[2][1]) {cout<<"Player 2 wins!"<<endl;}
			if (table[0][2]=='O' && table[0][2]==table[1][2] && table[1][2]==table[2][2]) {cout<<"Player 2 wins!"<<endl;}
			if (table[0][0]=='O' && table[0][0]==table[0][1] && table[0][1]==table[0][2]) {cout<<"Player 2 wins!"<<endl;}
			if (table[1][0]=='O' && table[1][0]==table[1][1] && table[1][1]==table[1][2]) {cout<<"Player 2 wins!"<<endl;}
			if (table[2][0]=='O' && table[2][0]==table[2][1] && table[2][1]==table[2][2]) {cout<<"Player 2 wins!"<<endl;}
		}
     }	
	
	return 0;
}

void locations(int &row, int &column)
{
	cout << " Enter a row (between 0 and 2): ";
	cin >> row;
	while(row < 0 || row > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the row: ";
		cin >> row;
	}
	cout << " Enter a column (between 0 and 2): ";
	cin >> column;
	while(column < 0 || column > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the column: ";
		cin >> column;
	}
}

void tables(char table[][3], int rows)
{
	for(int x = 0; x < rows; x++)
	{
		for(int y = 0; y < 3; y++)
		{
			cout << " " << table[x][y] << " | ";
		}
		cout << endl;
		cout << " ------------- " << endl;
	}
}
I'd like to point out that your current set-up does not check if someone already placed an x or o on a spot, so you can "override" someone else.
Crap! How do I fix that?
When asking for input, check whether that spot currently contins '*'. If not, require them to input again. While and for statements work well here.
Topic archived. No new replies allowed.