Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

evilclown

classes

This topic is 5933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.
#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;
}
 

Share this post


Link to post
Share on other sites
Advertisement
What is not working ?
- Not compiling ?
-- What errors ?
- Not doing what you want ?
-- What do you get ?
-- What do you want ?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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[i];
}
delete [] data;
}

void TICTACTOE_BOARD::show() {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cout << i << " " << j << " - ";
//cout data[i][j];
}
cout << endl;
}
}

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
data[row - 1][col - 1] = val;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!