### 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).

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````#include #include 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: "<> 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.