Whenever I wanted to use header files I would just make a .cpp and .h file. However the professor I'm taking wants two .cpp files and a header (one cpp file having the same name name as the header, the other main).
What is the purpose of the .cpp file with the same name and how do I structure it?
My code if you need it, doubt you will, thanks!!!!!
Also would like to add that without you guys life in my C classes would have been hell. You helped me through a lot when I was learning this for fun (I still am, just not on my own anymore :P) and extremely grateful for that. I'll try to beat you guys to posts but you guys are fast!
main.cpp
Code:
#include <iostream>
using namespace std;
#include "TicTacToe.h"
int main()
{
int turns = 0;
TicTacToe game;
game.outputGrid();
for(;;)
{
do
{
cout << "Player 1's turn (X)" << endl;
do
{
game.getRow();
game.checkRow();
}while(game.checkRowOK() == false);
do
{
game.getColumn();
game.checkColumn();
}while(game.checkColumnOK() == false);
game.checkTile();
}while(game.isEmpty() == false);
game.fillTile('X');
cout << endl;
game.outputGrid();
game.checkWin();
if(game.isWin() == true)
{
cout << endl << "Player 1 (X) Wins!" << endl;
break;
}
game.checkSpaceLeft();
if (game.isSpaceLeft() == false)
{
cout << endl << "No more spaces left, it's a tie!" << endl;
break;
}
turns++;
do
{
cout << "Player 2's turn (O)" << endl;
do
{
game.getRow();
game.checkRow();
}while(game.checkRowOK() == false);
do
{
game.getColumn();
game.checkColumn();
}while(game.checkColumnOK() == false);
game.checkTile();
}while(game.isEmpty() == false);
game.fillTile('O');
cout << endl;
game.outputGrid();
game.checkWin();
if(game.isWin() == true)
{
cout << endl << "Player 2 (O) Wins!" << endl;
break;
}
turns++;
}
return 0;
}
.h
Code:
#include <iostream>
using namespace std;
class TicTacToe
{
private:
char Grid[3][3];
int inputrow;
int inputcolumn;
bool RowOK;
bool ColumnOK;
bool win;
bool empty;
char winningPlayer;
bool spaceLeft;
public:
TicTacToe();
void outputGrid();
void getRow();
void checkRow();
bool checkRowOK();
void getColumn();
void checkColumn();
bool checkColumnOK();
void checkTile();
bool isEmpty();
void fillTile(char);
void checkWin();
bool isWin();
void checkSpaceLeft();
bool isSpaceLeft();
};
TicTacToe::TicTacToe()
{
for(int a = 0; a<3; a++)
for(int b = 0; b<3; b++)
Grid[a][b] = ' '; // space = empty, 1 = X, 2 = O
win = false;
spaceLeft = true;
}
void TicTacToe::outputGrid()
{
cout << " 3 4 5" << endl;
cout << " " << (char)201 << (char)205 << (char)205 << (char)209 << (char)205 << (char)205 << (char)209 << (char)205 << (char)205 << (char)187 << endl;
cout << "0" << (char)186 << Grid[0][0] << " " << (char)179 << Grid[0][1] << " " << (char)179 << Grid[0][2] << " " << (char)186 << endl;
cout << " " << (char)199 << (char)196 << (char)196 << (char)197 << (char)196 << (char)196 << (char)197 << (char)196 << (char)196 << (char)182 << endl;
cout << "1" << (char)186 << Grid[1][0] << " " << (char)179 << Grid[1][1] << " " << (char)179 << Grid[1][2] << " " << (char)186 << endl;
cout << " " << (char)199 << (char)196 << (char)196 << (char)197 << (char)196 << (char)196 << (char)197 << (char)196 << (char)196 << (char)182 << endl;
cout << "2" << (char)186 << Grid[2][0] << " " << (char)179 << Grid[2][1] << " " << (char)179 << Grid[2][2] << " " << (char)186 << endl;
cout << " " << (char)200 << (char)205 << (char)205 << (char)207 << (char)205 << (char)205 << (char)207 << (char)205 << (char)205 << (char)188 <<"\n\n";
}
void TicTacToe::getRow()
{
cout << "Input Row (0-2): ";
cin >> inputrow;
}
void TicTacToe::checkRow()
{
if (inputrow < 0 || inputrow > 2)
{
cout << "Error. Please enter a number between 0 and 2." << endl;
RowOK = false;
}
else
RowOK = true;
}
bool TicTacToe::checkRowOK()
{
return RowOK;
}
void TicTacToe::getColumn()
{
cout << "Input Column (3-5): ";
cin >> inputcolumn;
}
void TicTacToe::checkColumn()
{
if (inputcolumn < 3 || inputcolumn > 5)
{
cout << "Error. Please enter a number between 3 and 5." << endl;
ColumnOK = false;
}
else
ColumnOK = true;
}
bool TicTacToe::checkColumnOK()
{
return ColumnOK;
}
void TicTacToe::checkTile()
{
if(Grid[inputrow][inputcolumn-3] == ' ')
empty = true;
else
{
cout << "Tile is already filled, please try again." << endl;
empty = false;
}
}
bool TicTacToe::isEmpty()
{
return empty;
}
void TicTacToe::fillTile(char player)
{
Grid[inputrow][inputcolumn-3] = player;
}
void TicTacToe::checkWin()
{
if(Grid[0][0] != ' ' && Grid[0][0] == Grid[0][1] && Grid[0][0] == Grid[0][2] )
{
win = true;
}
if(Grid[1][0] != ' ' && Grid[1][0] == Grid[1][1] && Grid[1][0] == Grid[1][2] )
{
win = true;
}
if(Grid[2][0] != ' ' && Grid[2][0] == Grid[2][1] && Grid[2][0] == Grid[2][2] )
{
win = true;
}
if(Grid[0][0] != ' ' && Grid[0][0] == Grid[1][0] && Grid[0][0] == Grid[2][0] )
{
win = true;
}
if(Grid[0][1] != ' ' && Grid[0][1] == Grid[1][1] && Grid[0][1] == Grid[2][1] )
{
win = true;
}
if(Grid[0][2] != ' ' && Grid[0][2] == Grid[1][2] && Grid[0][2] == Grid[2][2] )
{
win = true;
}
if(Grid[0][0] != ' ' && Grid[0][0] == Grid[1][1] && Grid[0][0] == Grid[2][2] )
{
win = true;
}
if(Grid[0][2] != ' ' && Grid[0][2] == Grid[1][1] && Grid[0][2] == Grid[2][0] )
{
win = true;
}
}
bool TicTacToe::isWin()
{
return win;
}
void TicTacToe::checkSpaceLeft()
{
int emptySpaces = 0;
for (int a = 0; a<3; a++)
for (int b = 0; b<3; b++)
{
if (Grid[a][b] == ' ')
{
emptySpaces++;
}
}
if (emptySpaces == 0)
spaceLeft = false;
}
bool TicTacToe::isSpaceLeft()
{
return spaceLeft;
}