Switching code to Class/object structure

My latest task is to take a code for a game of tic tac toe and change the format into a class/object structure. I have begun to make the change but could I get some help with how to proceed here? I am very new to programming and even newer to class/object structures. Thanks. I have placed into the code the class definition and main I am required to use, as well as an optional code for a class constructor at the bottom of the code. Any insight on how to proceed?

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>


// Program # 7

// This program is a recreation of program #6, a game of tic tac toe, using a class/object structure. 


using namespace std;

//Required to use this class definition.
class TicTacToe{

public:
	TicTacToe();
	void play();

private:
	char sigil[3];
	int board[3][3];
	int winner();
	bool isGameOver();
	void drawBoard();
	bool isMoveLegal(int row, int column);

};


// return 1 if player 1 'X' has won
// return 2 if player 2 'O' has won
// return 0 if neither player has won
int winner(int board[3][3]){
	int win = 0;

	// Checks for:
	//     X X X
	for (int i = 0; i < 3; i++)
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
			win = board[i][0];

	// Checks for:
	// X
	// X
	// X
	for (int i = 0; i < 3; i++)
	if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
		win = board[0][i];

	// Checks for:
	// X                 X
	//   X      or     X
	//     X         X 
	if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
		(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
		win = board[1][1];

	return win;
}


// using this board as a guide
// draw the board using "." for empty squares
// or 'X' or 'O' for player 1 or player 2
void drawBoard(int board[3][3]){

	cout << "   0  1  2" << endl;
	for (int i = 0; i < 3; i++){
		cout << i ;
		
		
		for (int j = 0; j < 3; j++){
			cout.width(3);
			switch (board[i][j])
			{
			case 0:{
					   cout << ".";
			}break; case 1:{
					   cout << "X";
			}break;
			case 2:{
					   cout << "0";
			}break;
			default:
				break;
			}
		} cout << endl;
	}
	
	
}


// return false if row or column are out of bounds
// or if that spot on the board is already taken 
// otherwise return true
bool isMoveLegal(int board[3][3], int row, int column){
	if (row >= 0 && row <= 3 && column <= 3 && column >= 0)		{
		if (board[row][column] == 0)
			return true;
		else return false;
	}
	return false;
}


// if any player has three in a row or if the board is full
// return true otherwise return false
bool isGameOver(int board[3][3]){
	if (winner(board) == 1 || winner(board) == 2) return true;
	for (int r = 0; r <= 2; r++)
	for (int c = 0; c <= 2; c++)
	if (board[r][c] == 0)
		return false;
	return true;
}

// Required to use this main function
main(){

  TicTacToe game;

  game.play();

}

//You may use, if you wish, the following code for your class constructor:

TicTacToe::TicTacToe(){
for(int r=0; r<3; r++)

 for (int c=0; c<3; c++)

   board[r][c] = 0;
sigil[0]='.';
sigil[1]='X';
sigil[2]='O';

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Required to use this class definition.
class TicTacToe{

public:
	TicTacToe();
	void play();

private:
	char sigil[3];
	int board[3][3];
	int winner();
	bool isGameOver();
	void drawBoard();
	bool isMoveLegal(int row, int column);

};


This is a poor class definition; it is not const-correct.
http://www.parashift.com/c++-faq-lite/const-member-fns.html.
Rewrite:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TicTacToe{

public:
	TicTacToe();
	void play();

private:
        enum { N = 3 } ;
	char sigil[N] ; // [3];
	int board[N][N] ; // [3][3];

	int winner() const ;
	bool isGameOver() const ;
	void drawBoard() const ;
	bool isMoveLegal(int row, int column) const ;

};


Rewrite the functions as member functions of the class.
(Member functions of the class have access to member variables;
for instance we do not need to pass board as a parameter.)

1
2
3
4
// bool isGameOver( const int board[3][3] ){
bool TicTacToe::isGameOver() const {
    // ...
}
That's funny you mention its a poor definition as it was the one provided by my instructor that we are required to use haha. Is it really as simple as redefining the functions and then making sure it all matches up? Thanks for the help.
Topic archived. No new replies allowed.