| | |
C++ Tic Tac Toe using classes & operator overloading
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Jun 2005
Posts: 19
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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:
implementation file
Thanks,
Marcia
header file:
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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; } } };
•
•
Join Date: Jun 2005
Posts: 19
Reputation:
Solved Threads: 0
Marcia,
Here is a start.
and
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
Here is a start.
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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
implementation
error message:
Gamepiece.cpp(40) : error C2248: 'pName' : cannot access private member declared in class 'GamePiece'
thanks again...
Marcia
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
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Jun 2005
Posts: 19
Reputation:
Solved Threads: 0
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
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.
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Tic Tac Toe help (Java)
- Tic Tac Toe with Classes Problem (C++)
- Tic-Tac-Toe (C++)
- C++ Tic Tac Toe Question & Help (Game Development)
- Tic Tac Toe Help (Java)
Other Threads in the C++ Forum
- Previous Thread: Erroneus outputs in string manipulation simple program
- Next Thread: Dynamically Print Out Structure Contents
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






