Creating a stand alone function that works with member functions?

Thanks to some previous help I was able to get this program running. Now I working on taking one of my member functions and turning it into a standalone function. I choose the create_board() function. Yet, if I declare it in my header file or my main.cpp it cant access any info from the original member functions? anyone see what I'm doing wrong here? The changes that I made are in bold. Thanks!


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include "ticTacToe.h"
#include <iostream>
#include <limits>



void run_game()
{
    char letter_o = 'O';
    char letter_x = 'X';
    ticTacToe ttt; //creates an object "ttt" to be used with functions
    ttt.player1Piece = letter_x;
    ttt.player2Piece = letter_o;
    std::cout << "Tic Tac Toe Game\n" << std::endl;
    std::cout << "Here is a blank board: " << std::endl;
    create_board();
    while (true){
        std::cout << "\nPlayer X, it is your turn\n" << std::endl;
        ttt.update_board(letter_x);
        if (ttt.determine_winner(letter_x) == true){
            break;
        }
        std::cout << "\nPlayer O, it is your turn\n" << std::endl;
        ttt.update_board(letter_o);
        if (ttt.determine_winner(letter_o) == true){
            break;
        }
    }
}

int main() //main runs program by calling "run_game()"
{
    run_game();

}


ticTacToe.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#include "ticTacToe.h"
#include <iostream>
#include <limits>


void ticTacToe::print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
{
    //prints out the updated board when called upon to do so
    for(int a = 0; a < 3; a++){
        std::cout << "\n";
        for (int b = 0; b < 3; b++){
            board[pos1][pos2] = playerPiece;
            std::cout << board[a][b];
        }
    }
}


int ticTacToe::check_for_overlap(int pos1, int pos2, char playerPiece)
{
    if (board[pos1-1][pos2-1] != '-'){
        std::cout << "\nOVERLAP DETECTED!!!!!!" << std::endl;
        return true;
        }
        return false;
}


void ticTacToe::update_board(char playerPiece)
{
    //updates the board based on user input
    int x, y;
    std::cout << "Enter a position to place the " << playerPiece << " on the board" << std::endl;
    while (true){
        std::cout << "Enter row: " << std::endl;
        std::cin >> x;
        if (x < 1 || x > 3 || std::cin.fail()){
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            std::cout << "Your number is invalid. Try again. " << std::endl;
        } else {
            break;
        }
    }

    while (true)
    {
        std::cout << "Enter col: " << std::endl;
        std::cin >> y;
        if (y < 1 || y > 3 || std::cin.fail()){
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            std::cout << "Your number is invalid. Try again. " << std::endl;
        } else {
            break;
        }
    }

    if (check_for_overlap(x, y, player1Piece) == true){
        x--;y--;print_board(player2Piece, x, y);
        std::cout << "\nThe board has been re-set. Try again!" << std::endl;
    } else if (check_for_overlap(x, y, player2Piece) == true){
        x--;y--;print_board(player1Piece, x, y);
        std::cout << "\nThe board has been re-set. Try again." << std::endl;
    } else {
        x--;y--;print_board(playerPiece, x, y);
    }
}


int ticTacToe::determine_winner(char playerPiece)
{
    /*slices the board and checks if playerPiece occupies that slot.
    If the player makes a line, print that playerPiece has won
    and exit the game.*/
    if (board[0][0] == playerPiece && board[0][1] == playerPiece && board[0][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[1][0] == playerPiece && board[1][1] == playerPiece && board[1][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[2][0] == playerPiece && board[2][1] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][0] == playerPiece && board[1][0] == playerPiece && board[2][0] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][1] == playerPiece && board[1][1] == playerPiece && board[2][1] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][2] == playerPiece && board[1][2] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][0] == playerPiece && board[1][1] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][2] == playerPiece && board[1][1] == playerPiece && board[2][0] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    } else {
        return false;
    }
}


ticTacToe.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#ifndef TICTACTOE_H_INCLUDED
#define TICTACTOE_H_INCLUDED

void create_board();

void create_board()
{
    //creates a blank board
    for(int a = 0; a < 3; a++){
       std::cout << "\n";
        for (int b = 0; b < 3; b++){
            board[a][b] = '-';
          std::cout << board[a][b];
        }
    }
}




class ticTacToe
{
    public: //Allow us to use the functions anywhere
    char player1Piece; //variable used in multiple functions
    char player2Piece; //variable used in multiple functions


    void print_board(char playerPiece, int pos1, int pos2);
    int check_for_overlap(int pos1, int pos2, char playerPiece);
    void update_board(char playerPiece);
    int determine_winner(char playerPiece);

    char board[3][3]; //Creates an 2d array for the board
};





#endif // TICTACTOE_H_INCLUDED



Now I working on taking one of my member functions and turning it into a standalone function.

Why?

Have you considered changing it into the constructor for ticTacToe?

If you still want to make it a stand-alone, you probably need to pass the board to the function. For example: if some code looked like:
1
2
3
4
5
6
7
8
9
10
11
#include "ticTacToe.h"
...
int main(void)
{
    ticTacToe ttt;
    ticTacToe vvv;
    ticTacToe zzz;

    create_board();  /* Which board should this create? */

}


One possible declaration void create_board(char board[3][3]);
You may then be able to call it with create_board(ttt.board);[Untested idea.]

You have put the declaration for create_board in ticTacToe.h. Why not put it into its own header file for example board.h?

You also have put the definition into ticTacToe.h. Why did you put there? I would suggest that you put the definition into one of board.cpp, main.cpp, or ticTacToe.cpp.



Last edited on
I'm confused as to why you are trying to do this.
First, you want to access an unknown object's internal fields from a global function (don't do that).
Second, you are separating an object's creation/initialization from the object itself (don't do that).

tl;dr leave it as a member function.

Hope this helps.
Topic archived. No new replies allowed.