tic-tac-toe - dont know whats wrong

Hi guys,

Im programming tic tac toe but the board is not showing numbers, and it is simply stating the winner without any input: (the win conditions are not complete, but it shouldnt matter).

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
127
128
129
130
#include <iostream>
#include<string>
using namespace std;
 
//Write a two-player tic-tac-toe game; use enums when possible to represent the values of the
//board
 
char ticBoard[3][3];
char board[10];
void showBoard();
bool moveIsValid(int m);
int whoWon(); // 0 tied, 1 p1, 2 p.2
int totalMoves(0); // if = 9 return 0
 
void main()
{
char board[10]= {' ', '1', '2', '3','4','5','6','7','8','9'};
string playerOne;
string playerTwo;
int playerTurn(1);
int move;
// Requesting Player names
cout << "Enter player 1's name: "<< endl;
cin >> playerOne;
cout << "Enter player 2's name: "<<endl;
cin >> playerTwo;
 
while(whoWon() == 0 && totalMoves < 9)
{
// Do this until players chooses a valid move
do
{
////initializing the board
showBoard();
//// Get player input
if(playerTurn==1)
{
cout << "Player " << playerOne << "'s turn: " << endl;
}else
{
cout << "Player " << playerTwo << "'s turn: " << endl;
}
 
//////////Get move
cout << "Enter your move: " << endl;
cin >> move;
}while(moveIsValid (move) != true);
totalMoves++;
// Switch turns
switch (playerTurn)
{
case 1:
{
board[move] = 'x';
playerTurn = 2;
break;
}
case 2:
{
board[move] = 'o';
playerTurn = 1;
break;
}
 
 
}
}
 
//show board
showBoard();
 
if (whoWon() == 1)
{
cout << playerOne << " Won!! " << endl;
}else if(whoWon() == 2)
{
cout << playerTwo << " Won!!" << endl;
}else
{
cout << "Its a tie!" << endl;
}
 
 
 
 
system ("pause");
}
 
 
 
bool moveIsValid(int m)
{
if(board[m] != 'x' && board[m] != 'o')
{
return true;
}else
return false;
}
 
void showBoard()
{
 
cout << board[1] << "|" << board[2] << "|" << board[3] << endl;
cout << "-.-.-"<< endl;
cout << board[4] << "|" << board[5] << "|" << board[6] << endl;
cout << "-.-.-"<< endl;
cout << board[7] << "|" << board[8] << "|" << board[9] << endl;
}
 
int whoWon()
{
if(board[1] == board[2] && board[2] == board[3])
{
if (board[1] == 'x')
{
return 1;
}else
return 2;
}
if(board[1] == board[5] && board[5] == board[9])
{
if (board[1] == 'x')
{
return 1;
}else
return 2;
}
return 0;
 
}
1) I strongly recommend you start using a consistent indentation style. It will make your code easier to read, for us and for you. In particular, it will make the flow of control through your code a lot easier to see at a glance, helping you spot errors quickly.

2) Your problem is that you're declaring two variables called board. The first is a global variable, that you define at line 9. The second is a local variable in main, that you define at line 17. Within main, this local variable hides the global one.

The local array is the one you're initializing to contain numbers. The global array is never initialised. And it's the global one that's being used by moveIsValid(), showBoard() and whoWon().

You should avoid using global variables as much as possible. I'd advise sticking with the local one, and passing that as an argument into your other functions.
tyvm it works.

I had indentations, but when I paste it in it doesnt work.

edit: How would you recommend I use my char board array? I deleted the local one and pasted char board[10]= {' ', '1', '2', '3','4','5','6','7','8','9'}; in the global (instead of the other one).
Last edited on
closed account (iAk3T05o)
Start by changing that void main() to int main()
Your global board should be initialized like this:
char board[10]= {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
This way your showBoard() function prints out empty spaces instead of numbers.
@NZheadshot, i dont want empty spaces, I want numbers. It currently works as planned, but the removal of void main to int main gives me errors. So I put it back to void main.
venros wrote:
How would you recommend I use my char board array? I deleted the local one and pasted char board[10]= {' ', '1', '2', '3','4','5','6','7','8','9'}; in the global (instead of the other one).

Um, I already gave you my recommendation:

MikeyBoy wrote:
You should avoid using global variables as much as possible. I'd advise sticking with the local one, and passing that as an argument into your other functions.


venros wrote:
the removal of void main to int main gives me errors. So I put it back to void main.

Assuming you actually changed your function to return something (because, obviously, if you define your function to have a return value, then you need to actually return something) then it shouldn't have caused an error. What error message did you get?

Nathan2222 is correct - main functions should always return an int.
Last edited on
Topic archived. No new replies allowed.