I have all the way to this part of displaying the board and now I need help on how to play it. I don't want someone to just give me the code, I want to learn what to do. Thanks very much.

p.s.= using array

#include <iostream>
using namespace std;



void tictactoe(char board[3][3])
{
	
	for(int i=1; i<=3; i++){
		
	
		for(int j=1; j<=3; j++){
			cout<< "["<< board[i][j] << "]";

		}
		cout<<endl;
	}

}

int main()
{
	char board[3][3];
	char answer;
	cout << "Welcome to Tic Tac Toe." <<endl;
	cout << "Are you ready to play Tic Tac Toe?"<<endl;
	cin >> answer;
	if(answer=='y'){
		tictactoe(board);
	}
	else {
		cout << "Bye quiter." <<endl;
	}
  return 0;
}

Recommended Answers

All 3 Replies

Well, I would rename the tictactoe function "DisplayBoard" or something. I would add a function called "PlayGame". main would call "PlayGame". PlayGame would call DisplayBoard initially, then after every move. PlayGame would initialize the 2-D board to empty spaces, then pass to DisplayBoard. I would define the board array in PlayGame rather than main. Something like this, using your existing code:

#include <iostream>
using namespace std;


void DisplayBoard(char board[3][3])
{	
	for(int i=1; i<=3; i++){	
		for(int j=1; j<=3; j++){
			cout<< "["<< board[i][j] << "]";

		}
		cout<<endl;
	}
}


void PlayGame ()
{
     char board[3][3];
     char answer;
     // initialize board to all empty spaces
     DisplayBoard (board);

     bool gameIsOver = false;
     while (!gameIsOver)
     {
          // prompt for next move.  answer variable may be used here
          // change board array to reflect new move
          DisplayBoard (board);
          // check if game is over.  If so, set gameIsOver to true
     }
}


int main()
{
	cout << "Welcome to Tic Tac Toe." <<endl;
	cout << "Are you ready to play Tic Tac Toe?"<<endl;
	cin >> answer;
	if(answer=='y'){
		PlayGame();
	}
	else {
		cout << "Bye quiter." <<endl;
	}
        return 0;
}

Also, regarding your tictactoe function (my DisplayBoard function), you have a potential array index problem. C++ arrays start at 0, not 1.

You need to make a decision on how to input a move. Once you make that decision, make another function to accept moves.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.