ok so i need to create a cell class for a tic tac toe game. the cell class should have a row, column coordinate, and an internal and external display value.

heres what i have so far. can anyone give me some hints or point me in a direction where i can make this work? thx

#include <iostream>
using namespace std;

class Cell
{
public:
	Cell()
	{
		for ( int i = 0; i < 9; i++)
		{
			cells[9] ;

		}
	}

void askUser()
{
	char s = ' ';
	cout << "Please choose where you would like to enter your letter:" << endl;
	cin >> r >> s >> c;
}
private:
	int r;
	int c;
	int cells[9];
};

Recommended Answers

All 7 Replies

For starters, if you ask me, this looks like a poorly thought out throw together simply for posting purposes. I think you need to pay closer attention to what you are trying to accomplish.

I would advise you to sit down with a pencil and paper and do some more brainstorming:
You want an array of cells, which requires that the array not be a member of your cell class. You need something either in your main() or in the global scope. Depending on the scope of your lessons thus far, you may also want to consider a 2-dimensional array rather than a 1-d array

Second, this loop does absolutely nothing. In fact, it will probably cause an error.

for ( int i = 0; i < 9; i++)
		{
			cells[9] ;

		}

You need to use "i" as the index to reference, then assign some sort of value to, the array elements. In addition, since the array shouldn't exist inside the class definition anyway, this section of code is completely unnecessary.

Third, in askUser(), what is the purpose of "s"?

For starters, if you ask me, this looks like a poorly thought out throw together simply for posting purposes. I think you need to pay closer attention to what you are trying to accomplish.

I would advise you to sit down with a pencil and paper and do some more brainstorming:
You want an array of cells, which requires that the array not be a member of your cell class. You need something either in your main() or in the global scope. Depending on the scope of your lessons thus far, you may also want to consider a 2-dimensional array rather than a 1-d array

Second, this loop does absolutely nothing. In fact, it will probably cause an error.

for ( int i = 0; i < 9; i++)
		{
			cells[9] ;

		}

You need to use "i" as the index to reference, then assign some sort of value to, the array elements. In addition, since the array shouldn't exist inside the class definition anyway, this section of code is completely unnecessary.

Third, in askUser(), what is the purpose of "s"?

i was going to initialize the cells in the constructor. so what ur saying is i need a dimensional array like cell[3][3]? but then what i want to know is how to do display the board/cells to the user cause i was going to use nested for loops to make a 3 by 3 grid and then ask the user for info as in where they would like to place the letter.(in which cell) so what should i do? initialize array in the constructor? becuase i was thinking of rather than use row and column coordinates just use a single number to represent each cell? what do u think?heres what ive changed

#include <iostream>
using namespace std;

class Cell
{
public:
	Cell()
	{
		for ( int i = 0; i < 9; i++)
		{
			char a = '1';
			cells[9] = a;
			a++;
		}
	}

void setCellInfo()
{
	bool move = false;
	while ( !move )
	{
		cout << "Please choose where you would like to enter your letter:" << endl;
		cin >> numOfCell;
		validMove();
	}
}

int getCellInfo()
{
	return numOfCell;
}
bool validMove()
{
	bool move;
	if (numOfCell > 1 || numOfCell < 9)
		{
			move = false;
		} else
		{
			cout << "Try again.";
			move = true;
		}
	return move;
}

private:
	int numOfCell;
	int cells[9];
};

Like I mentioned before, the array shouldn't even be part of the class definition.

How you display the board is up to you, but it will have to be from main or another function that main calls. You will need some sort of loop:

//overly-simplified for loop example for a 1-d array:
const int ARRAY_SIZE = /* put a value here */
for (int index = 0; index < ARRAY_SIZE; index++) {
  cout <<  array[index];
}

Like I mentioned before, the array shouldn't even be part of the class definition.

How you display the board is up to you, but it will have to be from main or another function that main calls. You will need some sort of loop:

//overly-simplified for loop example for a 1-d array:
const int ARRAY_SIZE = /* put a value here */
for (int index = 0; index < ARRAY_SIZE; index++) {
  cout <<  array[index];
}

ok. well for the whole program there will be three classes: a player class-keep track of players, the cell class-keep track of info in cells, and then a tic tac toe class which have a board declared like this: Cell board[3][3]; and a player array. Now what i want to ask is since i am declaring the board to be of type cell in the ttt class, does that mean i should still create an array for the cell? and if so it should have nine elements right?

OK. You've made the array part of a "board-like" class, that makes more sense. You shouldn't need any more instances of the cell class anywhere.

Now you just need to fine tune your class definitions to meet the descriptions in your assignment handout and do your implementations. A pen and paper may be helpful again.

OK. You've made the array part of a "board-like" class, that makes more sense. You shouldn't need any more instances of the cell class anywhere.

Now you just need to fine tune your class definitions to meet the descriptions in your assignment handout and do your implementations. A pen and paper may be helpful again.

i was thinking it over. i have to initialize the cells in the constructor dont I? because i made it private data and i need to use it elsewhere in the cell class to keep track of where the player wants to enter their letter. here is my updated code for the cell classtell me what u think. Where u see pName is just player x or player o which i will plan to call from the player class

#include <iostream>
using namespace std;

class Cell
{
public:
	Cell()
	{
		for ( int i = 0; i < 9; i++)
		{
			char a = '1';
			cells[9] = a;
			a++;
		}
	}

void setCellInfo()
{
		cout << "Please choose where you would like to enter your letter:" << endl;
		cin >> numOfCell;
}
int getCellInfo()
{
	return numOfCell;
}
void trackCell()
{
	getCellInfo();
	if ( numOfCell == '1')
		numOfCell = pName;
	else if ( numOfCell == '2')
		numOfCell = pName;
	else if ( numOfCell == '3')
		numOfCell = pName;
	else if ( numOfCell == '4')
		numOfCell = pName;
	else if ( numOfCell == '5')
		numOfCell = pName;
	else if ( numOfCell == '6')
		numOfCell = pName;
	else if ( numOfCell == '7')
		numOfCell = pName;
	else if ( numOfCell == '8')
		numOfCell = pName;
	else if ( numOfCell == '9')
		numOfCell = pName;
}

private:
	int numOfCell;
	int cells[9];
};

Until you take that array and initialization loop out of your "cell" class definition and put them elsewhere, I'm done helping you. Unless there's something you're not sharing, they're neither implemented correctly nor do they belong in that class. I've mentioned it to you 3 times.

for ( int i = 0; i < 9; i++)
		{
			char a = '1';
			cells[9] = a;
			a++;
		}

This loop still is not in the correct spot. Plus, even if it was, it still only references element 9, which doesn't even exist. You need to figure out how to step through your array and assign an appropriate value to all elements (those would be 0-8, NOT 0-9).

Your definition of "cell" should only have row, column, and value variables, read your assignment more closely. Each of those values need to be initialized in the "cells" constructor, yes.

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.