Need help with tic tac toe game using inheritance please,, much appreciated!!!

(If other header or cpp files are necessary, I will provide them accordingly)

here is the compiler error I'm receiving:

Undefined symbols for architecture x86_64:
"TicTacToe::rungame()", referenced from:
_main in main-672692.o
"TicTacToe::TicTacToe()", referenced from:
_main in main-672692.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

here is my code:

header:

#ifndef ____tictactoe__
#define ____tictactoe__
#include <iostream>
#include "player.h"
#include "cell.h"
#include "date.h"
using namespace std;

class TicTacToe
{
public:
TicTacToe();
TicTacToe(Player [],Player []);
void setplayer();
void Displayboard();
void makeMove();
bool checkWin();
bool validmove();
void chooseplayer();
bool validatebounds(int, int);
bool validatechoice(int, int);
void rungame();
void displayBlank();
void clearBoard();
void stats();


private:
static const int ROW = 3;
static const int COL = 3;
int currentplayer;
static const int DEFAULT_NUM_PLAYERS = 2;
Player p[DEFAULT_NUM_PLAYERS];
Cell board[ROW][COL];
Date d;
int numGamesPlayed, turn;

};

#endif /* defined(____tictactoe__) */


cpp file:

#include "tictactoe.h"
#include "cell.h"
#include "date.h"
#include <iostream>
using namespace std;
int ROW = 3;
int COL = 3;
//Tic Tac Toe default constructor
TicTacToe::TicTacToe()
{
cout << "Welcome to my TicTacToe Program!" << endl;
turn=0;
currentplayer=0;
numGamesPlayed = 0;
d.input();

}
//Function to input player information and assign a marker to each player
void TicTacToe::setplayer()
{
cout << "Player 1 please input your information:\n";
p[0].input();
cout << endl;
cout << "Player 2 please input your information:\n";
p[1].input();
cout << p[currentplayer].gethandle() <<", what marker would you like to use? " << endl;
cout << "\t1. X" << endl << "\t2. O\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
p[0].setmarker('X');
p[1].setmarker('O');
break;
case 2:
p[0].setmarker('O');
p[1].setmarker('X');
break;
default:
p[0].setmarker('X');
p[1].setmarker('O');
break;
}

}
//Function that chooses which player's turn it is
void TicTacToe::chooseplayer()
{
if(turn%2==0)
{
currentplayer = 0;
}
else
currentplayer = 1;
}
//Display board function
void TicTacToe::Displayboard(){
int i = 0;
int r = 0;

for ( i = 0; i < COL; i++)
{
for (r = 0; r < ROW; r++)
{
if (r < ROW-1)
{
cout << board[i][r].getmarker() << " | ";
}
else
{
cout << board [i][r].getmarker() << endl;
if(i !=COL-1){
cout << "---------------";
cout << endl;
}
else
cout << endl;
}
}

}
}
//Function to allow player to make move
//Function validates if the choice is within the bounds and if it has chosen already
void TicTacToe::makeMove(){
int x,y;
do{
cout << p[currentplayer].gethandle() << ", please enter the space of the coordinates (ROW,COL)" << endl;
cin >> x >> y;
}while(!(validatebounds(x, y)&&validatechoice(x, y)));
board[x-1][y-1].setmarkers(p[currentplayer].getmarker());
}
//Validation functions
bool TicTacToe::validatebounds(int x, int y)
{
bool validate = true;
if(x<0||x>COL)
{
validate = false;
cout << "Your entry was invalid" << endl;
}
if(y<0||y>ROW)
{
validate = false;
cout << "Your entry was invalid" << endl;
}
return validate;
}
bool TicTacToe::validatechoice(int x, int y)
{
bool validate = true;
if(board[x-1][y-1].getmarker() == 'O' || board[x-1][y-1].getmarker()== 'X')
{
validate = false;
cout << "Please choose another space, this one has been taken" << endl;
}

return validate;
}
//Function to check for wins
bool TicTacToe::checkWin()
{
bool winner(false);
int x;
//Checks for wins on diagonals
if (board[0][0].getmarker() != ' ' && board[0][0].getmarker() == board[1][1].getmarker() && board[0][0].getmarker() == board[2][2].getmarker())
return winner = true;

else if(board[ROW][0].getmarker() != ' ' && board[ROW][0].getmarker() == board[1][1].getmarker() && board[ROW][0].getmarker() == board[0][ROW].getmarker())
return winner = true;

//Checks for wins in rows
for(x = 0; x < ROW; x++){
if (board[x][0].getmarker() != ' ' && board[x][0].getmarker() == board[x][1].getmarker() && board[x][0].getmarker() == board[x][2].getmarker())
return winner = true;
}
//Checks for wins in columns
for(x = 0; x < COL; x++)
{
if(board[0][x].getmarker() != ' ' && board[0][x].getmarker() == board[1][x].getmarker() &&board[1][x].getmarker() && board [0][x].getmarker() == board[2][x].getmarker())
return winner = true;
}
return(winner);
}
//Run game function
void TicTacToe::rungame()
{
char ans;
bool validate = false;
bool endgame = false;
setplayer();
do{
displayBlank();
do{
makeMove();
Displayboard();
turn++;
if(turn==9)
{
validate = true;
if(!checkWin())
{
cout << "TIE" << endl;
}
}
if (turn>4)
{
if(checkWin())
{
validate=true;
cout << p[currentplayer].gethandle() << " wins!" << endl;
p[currentplayer].increasewins();

}

}
chooseplayer();
}while (!validate);
numGamesPlayed++;
clearBoard();
stats();
turn=0;
cout << "Would you like to play again?(Y/N)";
cin >> ans;
if(ans=='n' || ans=='N')
{
endgame=true;
clearBoard();
}
}while(!endgame);

}
//Initializes board to blank
void TicTacToe::displayBlank()
{
for(int x=0; x<COL;x++){
for (int y = 0; y<ROW; y++) {
if(y==ROW-1)
{
cout << " ";
}
else
cout << " " << "|";
}
if (x==COL-1) {
cout << endl;
}
else
{
cout << endl << "-----------------" << endl;
}
}
}
//Function to clear board after game has ended
void TicTacToe::clearBoard()
{
for (int x = 0; x < COL; x++)
for (int y = 0; y < ROW; y++)
board[x][y].setmarkers(' ');
}
//Function to display end stats for game
void TicTacToe::stats()
{

cout << "Games Played: " << numGamesPlayed << endl;

}


main driver:

#include <iostream>
#include "player.h"
#include "cell.h"
#include "tictactoe.h"
using namespace std;

int main()
{
TicTacToe t;
t.rungame();

}
Please don't post the same question more than once.

http://www.cplusplus.com/forum/beginner/181719/
Last edited on
Topic archived. No new replies allowed.