#include <iostream>
enum TICTACTOE_DATA {none, x, o};
class TICTACTOE_BOARD {
public:
TICTACTOE_BOARD(int, int);
~TICTACTOE_BOARD();
void show();
int rows;
int cols;
TICTACTOE_DATA *data;
private:
int getvalue(int, int);
void setvalue(int, int, int);
};
TICTACTOE_BOARD::TICTACTOE_BOARD(int board_rows, int board_cols) {
rows = board_rows;
cols = board_cols;
TICTACTOE_DATA *data = new TICTACTOE_DATA[rows][cols];
}
TICTACTOE_BOARD::~TICTACTOE_BOARD() {
delete [] data;
}
void TICTACTOE_BOARD::show() {
//show board
}
int TICTACTOE_BOARD::getvalue(int row, int col) {
//return value of row, col
return 0;
}
void TICTACTOE_BOARD::setvalue(int row, int col, int val) {
//set row, col to val
}
int main() {
TICTACTOE_BOARD board(3, 3);
board.show();
return 0;
}
classes
This isn''t working. Am I heading in the right direction? I wanted to make a tictactoe game that would be able to play in any dimension. Should I be defining it differently? I know i''m not using pointers right, but don''t know the right way.
What is not working ?
- Not compiling ?
-- What errors ?
- Not doing what you want ?
-- What do you get ?
-- What do you want ?
- Not compiling ?
-- What errors ?
- Not doing what you want ?
-- What do you get ?
-- What do you want ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by evilclown
TICTACTOE_DATA *data = new TICTACTOE_DATA[rows][cols];
Bingo!
You can''t allocate a multidimensional array like that, because your levels of indirection don''t match.
TICTACTOE_DATA **data; // notice 2 levels of indirection...// allocate the rows:data = new TICTACTOE_DATA *[rows];for(int n = 0; n < rows; ++n){ // allocate the columns in each row: data[n] = new TICTACTOE_DATA[cols];}
Similarly, you have to delete the arrays per level of indirection:
for(int n = 0; n < rows; ++n){ delete [] data[n];}delete [] data;
Also, your rows and cols should be private, and the accessor functions public.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
I''m getting a segmentation fault. It''s probably i''m not assigning the pointer right. I''m confused how the pointer to a pointer works. Heres''s what I have now:
const int TTT_NONE = 0;const int TTT_X = 1;const int TTT_O = 2;class TICTACTOE_BOARD { public: TICTACTOE_BOARD(); ~TICTACTOE_BOARD(); void show(); int getvalue(int, int); void setvalue(int, int, int); private: int rows; int cols; int **data;//two levels of indirection};TICTACTOE_BOARD::TICTACTOE_BOARD() { //allocate rows rows = 3; cols = 3; int i; int j; data = new int * [rows]; for(i = 0; i < rows; i++) { //allocate the columns data = new int[cols]; } for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { setvalue(i, j, TTT_NONE); } }}TICTACTOE_BOARD::~TICTACTOE_BOARD() { int i; for(i = 0; i < rows; i++) { delete [] data;<br> }<br> delete [] data;<br>}<br><br>void TICTACTOE_BOARD::show() {<br> for(int i = 0; i < rows; i++) {<br> for(int j = 0; j < cols; j++) {<br> cout << i << " " << j << " - ";<br> //cout data[j];<br> }<br> cout << endl;<br> }<br>}<br><br>int TICTACTOE_BOARD::getvalue(int row, int col) {<br> //return value of row, col<br> return 0;<br>}<br><br>void TICTACTOE_BOARD::setvalue(int row, int col, int val) {<br> //set row, col to val<br> data[row - 1][col - 1] = val;<br>}<br> </pre> </i>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement