944,020 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7209
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 24th, 2005
0

C++ Tic Tac Toe using classes & operator overloading

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 25th, 2005
0

Re: C++ Tic Tac Toe using classes & operator overloading

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BruceWilson512 is offline Offline
19 posts
since Jun 2005
Jun 26th, 2005
0

Re: C++ Tic Tac Toe using classes & operator overloading

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 26th, 2005
0

Re: C++ Tic Tac Toe using classes & operator overloading

M,

Use [code] place code here [\code]

But, instead of a \code use /code


Bruce
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BruceWilson512 is offline Offline
19 posts
since Jun 2005
Jun 26th, 2005
0

Re: constructors & overloading

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:
C++ Syntax (Toggle Plain Text)
  1. #ifndef GAMEPIECE_H
  2. #define GAMEPIECE_H
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. //********************************************************
  9.  
  10. class GamePiece
  11. {
  12. public:
  13. const char pName[50];
  14.  
  15. private:
  16. int pType;
  17.  
  18. public:
  19. // GamePiece(); // default constructor
  20. // GamePiece(const GamePiece& p); // copy constructor
  21. ~GamePiece();
  22. GamePiece& operator=(const GamePiece& right); // assignment operator
  23.  
  24. friend ostream& operator<<(ostream& os, const GamePiece& p);
  25. friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
  26. friend ifstream& operator<<(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece
  27.  
  28. int operator==(const GamePiece& p);
  29. int operator !=(const GamePiece& p);
  30.  
  31. void PlayerVsPlayer(GamePiece& p1, GamePiece& p2);
  32. void PlayerVsComputer(GamePiece& p1, GamePiece& c1);
  33. void ComputerVsComputer(GamePiece& c1, GamePiece& c2);
  34. void ResetPieces(GamePiece& p1, GamePiece& p2, int& size); // reset game
  35. };
  36. #endif


implementation file
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. #include "gamepiece.h"
  7.  
  8.  
  9. //********************************************************
  10. GamePiece::GamePiece() // default constructor
  11. {
  12. pName=NULL;
  13. pType=0;
  14. };
  15.  
  16. GamePiece(const GamePiece& p) // copy constructor
  17. :pName(p.pName), pType(p.pType)
  18. {
  19. if(p.pName!=NULL) {
  20. strcpy (pName, p.pName);
  21. }
  22. };
  23.  
  24. GamePiece& operator=(const GamePiece& right) // assignment operator
  25. {
  26. if(this==&right) return *this;
  27. strcpy(pName, right.pName);
  28. pType = right.pType;
  29. return *this;
  30. };
  31.  
  32. ostream& operator<<(ostream& os, const GamePiece& p)
  33. {
  34. os << p.pName << endl;
  35. os << p.pType << endl;
  36. };
  37.  
  38. ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
  39. {
  40. ofs << p.pName << endl;
  41. ofs << p.pType << endl;
  42. };
  43.  
  44. ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
  45. {
  46. ifs >> p.pName >> endl;
  47. ifs >> p.pType >> endl;
  48. };
  49.  
  50. int operator==(const GamePiece& p) const {
  51. return GamePiece == p;
  52.  
  53. };
  54.  
  55. int operator !=(const GamePiece& p) const {
  56. return GamePiece != p;
  57.  
  58. };
  59.  
  60. void GamePiece::PlayerVsPlayer(GamePiece& p1, GamePiece& p2)
  61. {
  62. cout << "Player 1 ("X"), enter name: ";
  63. cin >> p1.pName;
  64. cout << endl;
  65. cout << "Player 2 ("O"), enter name: ";
  66. cin >> p2.pName;
  67. cout << endl << endl;
  68.  
  69. p1.pType = 1;//"X"
  70. p2.pType = 2;//"O"
  71. };
  72.  
  73. void GamePiece::PlayerVsComputer(GamePiece& p1, GamePiece& c1)
  74. {
  75. GamePiece o = new GamePiece();
  76.  
  77. cout << "Player 1 ("X"), enter name: ";
  78. cin >> p1.pName;
  79. cout << endl;
  80. cout << "Player 2 ("O") will be the computer ";
  81. cout << endl << endl;
  82.  
  83. c1.pName = "Computer";
  84. p1.pType = 1;
  85. c1.pType = 2;
  86.  
  87. };
  88.  
  89. void GamePiece::ComputerVsComputer(GamePiece& c1, GamePiece& c2)
  90.  
  91. cout << "Sit back and enjoy the show...";
  92. cout << endl << endl;
  93.  
  94. c1.pName = "Computer 1";
  95. c2.pName = "Computer 2";
  96. c1.pType = 1;
  97. c2.pType = 2;
  98. };
  99.  
  100. void GamePiece::ResetPieces(GamePiece& p1, GamePiece& p2, int& size) // reset game
  101. {
  102. for (int i=0, i<size, i++) {
  103. for (int j=0, j<size, j++) {
  104. p1.pType[i][j]=0;
  105. p2.pType[i][j]=0;
  106. }
  107. }
  108. };
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 27th, 2005
0

Re: constructors & overloading

Marcia,

Here is a start.

C++ Syntax (Toggle Plain Text)
  1. #ifndef GAMEPIECE_H
  2. #define GAMEPIECE_H
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. //********************************************************
  9.  
  10. class GamePiece
  11. {
  12. public:
  13. char pName[50]; // Change - Removed const in declaration
  14.  
  15. private:
  16. int pType;
  17.  
  18. public:
  19. GamePiece(); // default constructor // Change - removed comment
  20. ~GamePiece(){}; // Change - added curly brackets
  21.  
  22. int operator==(const GamePiece& p) const {return this->pType == p.pType;}; // You must also check to see that the name is the same.too.
  23. int operator!=(const GamePiece& p) const {return this->pType != p.pType;}; // You must check that type or name is different.
  24.  
  25. };
  26. #endif


and

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. #include "gamepiece.h" // Change include file name
  7.  
  8.  
  9. //********************************************************
  10. GamePiece::GamePiece() // default constructor
  11. {
  12. pName[0]=NULL; // Change - added [0]
  13. pType=0;
  14. };


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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BruceWilson512 is offline Offline
19 posts
since Jun 2005
Jun 27th, 2005
0

Re: C++ Tic Tac Toe using classes & operator overloading

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
C++ Syntax (Toggle Plain Text)
  1. friend ostream& operator<<(ostream& os, GamePiece& p);
  2. friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
  3. friend ifstream& operator>>(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece

implementation
C++ Syntax (Toggle Plain Text)
  1. ostream& operator<<(ostream& os, GamePiece& p)
  2. {
  3. os << p.pName << endl << p.pType << endl;
  4. };
  5.  
  6. ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
  7. {
  8. ofs << p.pName << endl;
  9. ofs << p.pType << endl;
  10. };
  11.  
  12. ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
  13. {
  14. ifs >> p.pName >> endl;
  15. ifs >> p.pType >> endl;
  16. };

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


thanks again...
Marcia
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 27th, 2005
0

Re: C++ Tic Tac Toe using classes & operator overloading

Marcia,

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

Take care,
Bruce
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BruceWilson512 is offline Offline
19 posts
since Jun 2005
Jun 27th, 2005
0

Re: C++ Tic Tac Toe using classes & operator overloading

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BruceWilson512 is offline Offline
19 posts
since Jun 2005
Jun 28th, 2005
0

Re: C++ Tic Tac Toe using classes & operator overloading

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Erroneus outputs in string manipulation simple program
Next Thread in C++ Forum Timeline: Dynamically Print Out Structure Contents





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC