0

I am trying to switch from functional programming to object oriented, however I am running into an error while trying to call a member function from within a class.

The code is as follows:

GameFinal.h

    #ifndef GAMEFINAL_H
#define GAMEFINAL_H
using namespace std;
#include <string>
class TicTac
{
    public://MEMBER FUNCTION DECLARATION
    
    void printBoard();
    bool playerMove(int play);
    int checkWin();
    void player_turn();
    void game();
    void player1Info();
    void player2Info();

    protected:
    char xo;                                                                            
    int play, player_win;                                                                                                                                                                   
    std::string player1, player2;
            
};

#endif

GameFinal.cpp

#include "GameFinal.h"
#include <iostream>
using namespace std;




void TicTac::player1Info()
{
    cout << "Enter name for player 1: \n";
    
}

MainGameFinal.cpp

#include <iostream>
#include "GameFinal.h"
using namespace std;

int main (){

TicTac player1();
player1.player1Info();





    return 0;
}

The functional code in which I am trying to convert into object orientated looks like this

#include <iostream>
using namespace std;

char tictacBoard[3][3]= {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; 
char xo;                                                                            
int play, player_win;                                                                                                                                                                   
string player1, player2;

void printBoard();
bool playerMove(int play);
int checkWin();
void player_turn();
void game();
void player1Info();
void player2Info();



int main ()
{
player1Info();
player2Info();
game();


}





void printBoard()
{
    cout << " " << tictacBoard[0][0]<< " | " << tictacBoard[0][1]<< " | " << tictacBoard[0][2]<< endl;
    cout << "-------------\n";
    cout << " " << tictacBoard[1][0]<< " | " << tictacBoard[1][1]<< " | " << tictacBoard[1][2]<< endl;
    cout << "-------------\n";
    cout << " " << tictacBoard[2][0]<< " | " << tictacBoard[2][1]<< " | " << tictacBoard[2][2]<< endl;

    
}

bool playerMove(int cord)
{
    int row = cord /3;
    int col;
    
    if(cord % 3 == 0)
    {
        row = row - 1;
        col = 2;
    }
    else col = (cord %3) - 1;
    
    if(tictacBoard[row][col] != 'X' && tictacBoard[row][col] != 'O')
    {
    tictacBoard[row][col]=xo;
    return true;
    }
    else return false;

}


int checkWin()  
{
    for(int i = 0; i<3; i++)
    {
    
    if(tictacBoard[i][0] == tictacBoard[i][1] && tictacBoard[i][1] == tictacBoard[i][2]) return play;
    if(tictacBoard[0][i]==tictacBoard[1][i] && tictacBoard[1][i] == tictacBoard[2][i]) return play;
    }
    
    if(tictacBoard[0][0] == tictacBoard[1][1] && tictacBoard [1][1] == tictacBoard[2][2]) return play;
    if(tictacBoard[0][2] == tictacBoard[1][1] && tictacBoard [1][1] == tictacBoard[2][0]) return play;

    return 0;
}

void player_turn()
{
    if(xo=='X') xo='O';
    else xo = 'X';
    
    if(play == 1) play =2;
    else play =1;
}

void game()
{
    
    cout << player1 << ", would you like to be X or O? (Enter Captial)" << endl;
    char player1mark;
    cin >> player1mark;
    
    play = 1;
    xo = player1mark;
    
    printBoard();
    
    
    for(int i = 0; i < 9; i++)
    {
        
        if(play == 1) {cout << "It's "<< player1 << "'s move. Enter your move : " << endl;}
        else cout << "It's " << player2 << "'s move. Enter your move : ";
        
        int playermark; 
        cin >> playermark;

        if(playermark < 1 || playermark > 9)
        { cout << "Invalid move. Try again!"; i--; continue;}       
        
        if(!playerMove(playermark))
        {cout << "Invalid move. Try again!"; i--; continue; }
        
        printBoard();
        
        player_win = checkWin();
        
        if(player_win == 1) {cout << "Player 1 wins!"; break;}
            
        if(player_win == 2) {cout << "Player 2 wins!"; break;}
            
        player_turn();
        
        }
        
        if(player_win == 0) cout << "No winner this time.";
    }
void player1Info()
{
    cout << "Enter name for player 1: \n";
    cin >> player1; 
    int player1Score = 0;
}

void player2Info()
{
    cout << "Enter name for player 2: \n";
    cin >> player2; 
    int player2Score = 0;
}
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35

0 Answers0