tic tac toe

Can someone help me on understand where i went wrong on why the number chosen is not changing to X or O
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
  #include <iostream>
 using namespace 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;
  else if (a == 2)
    matrix[0][1] = player;
  else if (a == 3)
    matrix[0][2] = player; 
  else if (a == 4)
    matrix[1][0] = player;
  else if (a == 5)
    matrix[1][1] = player;  
  else if (a == 6)
    matrix[1][2] = player;
  else if (a == 7)
    matrix[2][0] = player;  
  else if (a == 8)
    matrix[2][1] = player;
  else if (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;
}
Reposting your code for posterity, in case you decide to delete it after answering (http://www.cplusplus.com/forum/beginner/249021/)
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
  #include <iostream>
 using namespace 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;
  else if (a == 2)
    matrix[0][1] = player;
  else if (a == 3)
    matrix[0][2] = player; 
  else if (a == 4)
    matrix[1][0] = player;
  else if (a == 5)
    matrix[1][1] = player;  
  else if (a == 6)
    matrix[1][2] = player;
  else if (a == 7)
    matrix[2][0] = player;  
  else if (a == 8)
    matrix[2][1] = player;
  else if (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.
Last edited on
so i added the win and instead of loop i did it manual but i cant get the x and the o to appear when selected..

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
#include <iostream>
 using namespace 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;
  else if (a == 2)
    matrix[0][1] = player;
  else if (a == 3)
    matrix[0][2] = player; 
  else if (a == 4)
    matrix[1][0] = player;
  else if (a == 5)
    matrix[1][1] = player;  
  else if (a == 6)
    matrix[1][2] = player;
  else if (a == 7)
    matrix[2][0] = player;  
  else if (a == 8)
    matrix[2][1] = player;
  else if (a == 9)
    matrix[2][2] = player; 
}
void Toggleplayer()
{
  if (player == 'X')
      player = 'O';
  else 
      player == 'X';
}
char win()
{
// X player
  if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][0] == 'X')
      return 'X';
  if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][0] == 'X')
      return 'X';
  if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][0] == 'X')
      return 'X';
  if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
      return 'X';

  if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
      return 'X';
  if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
      return 'X';
  if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
      return 'X';

  if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
      return 'X';
  if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
      return 'X';
  
//O player
   if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][0] == 'O')
      return 'O';
  if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][0] == 'O')
      return 'O';
  if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][0] == 'O')
      return 'O';
  if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
      return 'O';

  if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
      return 'O';
  if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
      return 'O';
  if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
      return 'O';

  if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
      return 'O';
  if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
      return 'O';

   return '/';
}
int main()
{
  // 1 2 3
  // 4 5 6
  // 7 8 9
  Draw();
  while (1)
  {
     Input();
     Draw();
     if (win () == 'X')
     {
       cout << "X Player Wins!" << endl;
       break;
     }
     else if (win() == 'O')
     {
       cout << "O Player Wins!" << endl;
       break;
     }
     Toggleplayer();
  }
 system("pause");
 return 0;
}
Topic archived. No new replies allowed.