#include <iostream>
usingnamespace std;
char matrix [3][3] = { '1', '2', '3', '4', '5', '6', '7', '8','9'};
int player;
void Draw()
{
cout << "Quick TIC TAC TOE!" << endl;
for (int i= 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matrix [i][j] << " ";
}
cout << endl;
}
}
void Input()
{
int a;
cout << "Press any # on the chart: ";
cin >> a;
if (a== 1)
matrix[0][0] = player;
elseif (a == 2)
matrix[0][1] = player;
elseif (a == 3)
matrix[0][2] = player;
elseif (a == 4)
matrix[1][0] = player;
elseif (a == 5)
matrix[1][1] = player;
elseif (a == 6)
matrix[1][2] = player;
elseif (a == 7)
matrix[2][0] = player;
elseif (a == 8)
matrix[2][1] = player;
elseif (a == 9)
matrix[2][2] = player;
}
void Toggleplayer()
{
if (player == 'X')
player = 'O';
else
player == 'X';
}
int main()
{
Draw();
while (1)
{
Input();
Draw();
Toggleplayer();
}
system("pause");
return 0;
}
In Toggleplayer, you do
player == 'X';
This is NOT assignment.
Enable more warnings on your compiler!
1 2 3 4 5
$ g++ -Wall -Wextra foo.cpp
foo.cpp: In function ‘void Toggleplayer()’:
foo.cpp:47:14: warning: statement has no effect [-Wunused-value]
player == 'X';
^
At first, you forgot to initialize your 'player' variable at line 4.
Then, at line 47, you wrote a comparison instead of an assignment. There you don't need even an if else. Instead you could use the ternary assignment: player = player=='X' ? 'O' : 'X';
And, at your Input() function, you could abbreviate the bunch of assignments by a single:
1 2
if (a >= 1 && a <= 9)
*matrix[a-1] = player;
This is been able because multidimensional arrays could be accessed as smaller dimensioned arrays.