Tic Tac Toe tie function

I'm almost done with this tic tac toe program other than making a function to state that it's a tie. I've tried a couple different methods but they haven't worked properly. What's the best solution for my prog set up?


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <iomanip>
using namespace std;

const gRow = 3;
const gCol = 3;
int numPlayer;
char sPlayer;
bool el;

void showGrid(char grid[][gCol], int);
int changeValue(int, int);
int checkTurn(int);
int checkPlayer(int);
bool checkGrid(char array[][3]);
void runGame(int, int, char array[][3]);


int main()
{ char grid[gRow][gCol] =  {{'*', '*', '*'},
							{'*', '*', '*'}, 
							{'*', '*', '*'}};
	
	
	int turn = 1;
	int nPlayer;
	char player;
	
	
cout << "         TIC TAC TOE" << endl;
cout << "Player One: X    Player Two: O\n" << endl;

do {
el = true;
showGrid(grid, gRow);

checkTurn(turn);
player = sPlayer;

checkPlayer(turn);
nPlayer = numPlayer; 

runGame(nPlayer, player, grid);


checkGrid(grid);

if (el == false)
{	cout << "Player " << nPlayer << " wins!!!\n";
el = false;
}

turn++;

}
while(el != false);

return 0;
}


checkTurn(int pTurn)
{ 
	if (pTurn % 2)
	{sPlayer = 'X';}
	else {sPlayer = 'O';}
	return sPlayer;
}
checkPlayer(int pTurn)
{ 
	if (pTurn % 2)
{ numPlayer = 1;}
else
{numPlayer = 2;}
return numPlayer;
}

void showGrid(char array[][gCol], int numRows)
{
    for (int row = 0; row < numRows; row++)
    {   for (int col = 0; col < gCol; col++)
        {
            cout << setw(2) << array[row][col] << " ";
        }
        cout << endl;
    }
}


void runGame(int num1, int num2, char array[][3])
{ int rows, cols;
cout << "Player " << num1 << ", Enter the row and column:";
cin >> rows >> cols;
while (array[rows-1][cols-1] != '*')
{cout << "This space is taken... Enter the row and column:\n";
cin >> rows >> cols; }
	
array[rows - 1][cols - 1] = num2;
}


bool checkGrid(char array[][3])
{ 

for(int i=0; i<3; i++) /* check rows */
if(array[i][0]==array[i][1] &&
array[i][0]==array[i][2]  && array[i][0] != '*') 
{ el = false; }

for(i=0; i<3; i++) /* check columns */
if(array[0][i]==array[1][i] &&
array[0][i]==array[2][i] && array[0][i] != '*')
{ el = false; }

if(array[0][0]==array[1][1] &&
array[1][1]==array[2][2] && array[0][0] != '*')
{ el = false; }

if(array[0][2]==array[1][1] &&
array[1][1]==array[2][0] && array[0][2] != '*')
{ el = false; }
	return el;
}


Last edited on
Here's some pseudocode:

1
2
3
4
5
6
7
bool checkGrid(char array[][3]) {
   if (someone won) {
      do win
   } else {
      it's a tie
   }
} 
Topic archived. No new replies allowed.