I'm trying to put together a tic tac toe game for a c++ class. Requirements are that it have two classes - gameboard and gamepiece. The gameboard I've pretty much taken care of, but I'm confused on how to set up the gamepiece class.

Here's the requirements as given to us:
Game Board must have at least the following:
Constructor specified game board size, with default parameter set to 3
Overloaded assignment operator for Game Boards
Operator overloads to insert and remove game pieces
Member functions to play either side of the game
Member functions to check for a winner
Member functions to reset the game
Overloaded ostream operator to print the board and piece.
Overloaded ofstream operator to save the board and piece.
Overloaded ifstream operator input the board and pieces from a file
Game pieces must have at least
Identifier to tell if X or O
A fixed size char string to hold the player’s name
Operator overloads required above
Overloaded == and != operator

I'm especially confused about overloading == and !=. Also confused on how to use one identifier for x or o.

Any ideas??? Help???

Thanks...

M Tritt,

Are you currently studying operator overloading in class? I would guess yes.

You overload operator!= and operator==. You will use those two operators to compare two instances of the class to see if they are the same or different.

Re: X and O
Can you use the same identifier for 0 and 1. Yes. Then why not X and O.

Take care,
Bruce

Thanks for the input. We are talking a bit about overloading operators and I'm struggling a bit with them. I'm have a hard time setting up the gamepiece class as well as the print functions for the both the gameboard and gamepiece functions. I'm not sure how to go about printing the board without a set number of indexes for the array. I have most of the GameBoard class set up, but because I'm not clear on the gamepiece function, I'm not really clear on the play functions.

I'm new to this website and I'm wondering if there's a way to attach code files or if code needs to be included in this window. I'd really like to have someone look over what I have so far and let me know if it makes sense, but it's more than one file.

Thanks again...

M,

Use [code] place code here [\code]

But, instead of a \code use /code

Bruce

[reference to BBCODE not valid anymore - please use Markdown]

Edited 3 Years Ago by diafol: update formatting

I can't get these constructors and operator overloads to compile... Any suggestions? I'm sure there are numerous other errors, but I can't get past the first few lines...

Thanks,
Marcia

header file:

#ifndef GAMEPIECE_H
#define GAMEPIECE_H

#include <iostream>
#include <fstream>

using namespace std;
//********************************************************

class GamePiece
{
public:
	const char pName[50];

private:
	int pType;

public:
//	GamePiece(); // default constructor
//	GamePiece(const GamePiece& p); // copy constructor
	~GamePiece();
	GamePiece& operator=(const GamePiece& right); // assignment operator

	friend ostream& operator<<(ostream& os, const GamePiece& p);
	friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
	friend ifstream& operator<<(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece

	int operator==(const GamePiece& p);
	int operator !=(const GamePiece& p);

	void PlayerVsPlayer(GamePiece& p1, GamePiece& p2);
	void PlayerVsComputer(GamePiece& p1, GamePiece& c1);
	void ComputerVsComputer(GamePiece& c1, GamePiece& c2);
	void ResetPieces(GamePiece& p1, GamePiece& p2, int& size); // reset game
};
#endif

implementation file

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

#include "gamepiece.h"


//********************************************************
GamePiece::GamePiece() // default constructor
{
	pName=NULL;
	pType=0;
};

GamePiece(const GamePiece& p) // copy constructor
:pName(p.pName), pType(p.pType)
{
	if(p.pName!=NULL) {
		strcpy (pName, p.pName);
	}
};

GamePiece& operator=(const GamePiece& right) // assignment operator
{
	if(this==&right) return *this;
	strcpy(pName, right.pName);
	pType = right.pType;
	return *this;
};

ostream& operator<<(ostream& os, const GamePiece& p)
{
	os << p.pName << endl;
	os << p.pType << endl;
};

ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
{
	ofs << p.pName << endl;
	ofs << p.pType << endl;
};

ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
{
	ifs >> p.pName >> endl;
	ifs >> p.pType >> endl;
};

int operator==(const GamePiece& p) const {
	return GamePiece == p;

};

int operator !=(const GamePiece& p) const {
	return GamePiece != p;

};

void GamePiece::PlayerVsPlayer(GamePiece& p1, GamePiece& p2)
{
	cout << "Player 1 ("X"), enter name: ";
	cin >> p1.pName;
	cout << endl;
	cout << "Player 2 ("O"), enter name: ";
	cin >> p2.pName;
	cout << endl << endl;

	p1.pType = 1;//"X"
	p2.pType = 2;//"O"
};

void GamePiece::PlayerVsComputer(GamePiece& p1, GamePiece& c1)
{
	GamePiece o = new GamePiece();
	
	cout << "Player 1 ("X"), enter name: ";
	cin >> p1.pName;
	cout << endl;
	cout << "Player 2 ("O") will be the computer ";
	cout << endl << endl;
	
	c1.pName = "Computer";
	p1.pType = 1;
	c1.pType = 2;

};

void GamePiece::ComputerVsComputer(GamePiece& c1, GamePiece& c2)

	cout << "Sit back and enjoy the show...";
	cout << endl << endl;

	c1.pName = "Computer 1";
	c2.pName = "Computer 2";
	c1.pType = 1;
	c2.pType = 2;
};

void GamePiece::ResetPieces(GamePiece& p1, GamePiece& p2, int& size) // reset game
{
	for (int i=0, i<size, i++) {
		for (int j=0, j<size, j++) {
			p1.pType[i][j]=0;
			p2.pType[i][j]=0;
		}
	}
};

Marcia,

Here is a start.

#ifndef GAMEPIECE_H
#define GAMEPIECE_H

#include <iostream>
#include <fstream>

using namespace std;
//********************************************************

class GamePiece
{
public:
	char pName[50];  // Change - Removed const in declaration

private:
	int pType;

public:
	GamePiece(); // default constructor		// Change - removed comment
	~GamePiece(){};							// Change - added curly brackets

	int operator==(const GamePiece& p) const {return this->pType == p.pType;};  // You must also check to see that the name is the same.too.
	int operator!=(const GamePiece& p) const {return this->pType != p.pType;};  // You must check that type or name is different.

};
#endif

and

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

#include "gamepiece.h"    // Change include file name


//********************************************************
GamePiece::GamePiece() // default constructor
{
	pName[0]=NULL;			// Change - added [0]
	pType=0;
};

I don't have any more time tonight to look at this, but it does give you a good start on the operator overloading.


Best Wishes,
Bruce

Bruce -
Thanks so much for taking time to look at my code. I really appreciate it.

I made the changes you suggested, but I can't get the gamepiece class to compile. It stops on my operator overload for ostream.. Here's what I have along with the error I'm getting...

header

friend ostream& operator<<(ostream& os, GamePiece& p);
	friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
	friend ifstream& operator>>(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece

implementation

ostream& operator<<(ostream& os, GamePiece& p)
{
	os << p.pName << endl << p.pType << endl;
};

ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
{
	ofs << p.pName << endl;
	ofs << p.pType << endl;
};

ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
{
	ifs >> p.pName >> endl;
	ifs >> p.pType >> endl;
};

error message:
Gamepiece.cpp(40) : error C2248: 'pName' : cannot access private member declared in class 'GamePiece'


thanks again...
Marcia

Marcia,

Please send your entire code to me at << moderator edit: removed email address >>. Including your main program.

Take care,
Bruce

Marcia,

I now have all the compile errors out except a small section of code. That section may be a design error anyway. But, I won't know until I see your entire code.

When is your assignment due?

To make this a good learning experience for you, you need to work the code more.

Work just one function at a time. If you are having problems with a function, just copy it down to the end of you .cpp file. And, then tackle the next function.

Not the errors you mentioned, but other errors are in your code that are basic errors that you should be able to fix.

Also, see my previous note.

Take care,
Bruce

Please do not ask for help or offer assistance via email.

Post questions to the forum and post replies to the forum. This helps others who may find this question in the future, and it also allows for constructive criticism of proposed solutions.

Bruce...

Assignment was actually due yesterday (Mon), but I'm hoping to get it in by next Tues with a small penalty...

Thanks for your input... I'm reworking the code and will post any more questions...

thanks!
mt

This article has been dead for over six months. Start a new discussion instead.