TIC TAC TOE GAME (SIMPLE)

I HAVE IT LAID OUT BUT SOMEWHERE I WENT WRONG WITH THE CODE THAT ONLY THE X APPEARS... CAN SOMEONE POINT IT OUT AND CLUE ON HOW TO FIX IT

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
  #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;
}
Why would you type in all caps?
CAPS LOCK ON MY KEYBOARD IS LOCKED
SORRY
A clue you say?

Well try compiling with more warnings enabled.
1
2
3
4
5
6
7
8
9
$ g++ -Wall foo.cpp
foo.cpp: In function ‘void Toggleplayer()’:
foo.cpp:47:14: warning: statement has no effect [-Wunused-value]
       player == 'X';
              ^
foo.cpp: In function ‘int main()’:
foo.cpp:119:16: error: ‘system’ was not declared in this scope
  system("pause");
                ^
OHH THATS HELPFUL THANKS. MORE WARNING SIGNS
Lying and being a dick is not the way to get help.
There are a lot of little things wrong with your code that I could've helped you with.
Your caps lock key seems fine when it comes to typing in code.
@garza07,
You assign the variable player to the matrix array; however, you do not define the player variable on line 4. By the way, why are you using an integer data type on the player variable on line 4? Since you are using the variable to compare characters, did you mean the variable type of the player variable as a char?

On line 47, player == 'X' // Comparing the variable should be player = 'X' \\ Assign the variable.

I would take a look at your win() function when you are determine the winner. On line 52, if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][0] == 'X') do you see the issue on the matrix array comparison?

There are additional minor issues; however, once you begin to debug the code, you may be able to see it.
Could have just opened your on-screen keyboard and detoggled capslock. Anyways I'm probably late as by now you've probably rebooted your system and the capslock would have reset.
don't know why @dutch is being rude. I'm just a kid with keyboard problems lol. and thank you @chicofeo I looked into line 47 and on the matrix and was able to fix it thank you for the help unlike dutch calling me mean names
Last edited on
when i switched
1
2
3
4
5
 if (player != 'X')
      player = 'O';
  else if
      (player = 'X')
       player = 'X';

now only O appears and also when starting the game and pressing the first number, nothing comes out its just blank on the number pressed first
Last edited on
[...]when starting the game and pressing the first number, nothing comes out its just blank on the number pressed first


@garza, I do not know your updated code. However, the reason the first selection comes out blank is due to the variable player is not initiated.

My suggestion would be to initialize the variable player like char player = 'X'; This would initialize the variable and once the player 1 makes the selection, the letter 'X' will populate (I assume that player 1 will always begin).
so i re deleted the code and started blank again here is where im at 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
#include <iostream>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X'; 
void Draw()
{
  cout << "Tic Tac Toe " << endl;
  for (int i = 0; i< 3; i++)
{
  for (int j = 0; i < 3; j++)
  {
    cout << matrix[i][j] << " ";
  }
  cout << endl;
 }
}
void input()
{
  int a;
  cout << "press the number you want: ";
  cin >> a;

// 00 01 02
// 10 11 12
// 20 21 22

  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;
  
  }
int main()
{
  Draw();
  system("pause");
  return 0;
}
On line 10:
for (int j = 0; i < 3; j++)

Should be:

for (int j = 0; j < 3; j++)

I would avoid global functions (if possible) and pass it between functions, instead.
Last edited on
You're not doing a bad job at all for a newbie.

I have some suggestions.
1) Improve your indentation. It's hard to read with bad indentation. For instance you didn't indent your second for-loop. And I would have used 3-spaces or a 1 tab-space for indenting.

2) Using a 2D array will only complicate it.
The barrage of if-statements is just horrible.

I would use a one-dimensional array.
char board[8] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
And for input simply do:
1
2
3
4
5
6
7
8
9
10
11
void input()
{
   char input; // note that I'm using char instead of int
   cout << "press the number you want: ";
   cin >> input;
   
   if (board[input-1] == input) // if not already chosen by player
      board[input-1] = player_symbol; // set board
   else
      cout << "Already taken"; 
}

Anyways you still haven't completed the game. So I'll let you finish your program and then you can ask again for any suggestions. I just wanted to suggest those two things because I think they would help your code. You still have to figure the rest of the logic like how to determine if there's a win and etc. on your own. I think you will especially become confused while trying to check for wins in a 1D array.

There are lots of other things like not using a global variable for the board and etc. but you don't worry about it for now.
Topic archived. No new replies allowed.