Creating object in 2D board, using dynamic array
Feb 17, 2014 at 5:12pm UTC
I try to create 2D board with random object (#), but the position of "#" do not equal with cords (showing by function show_wall). What I do wrong? My second question is - where I must use delete operator?
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
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class board
{
public :
void size (int n_size) {m_size = n_size; }
void create_board ();
void show_wall ();
private :
int m_size;
int m_chance_wall = 80;
int m_max_wall = 10;
int * p_wall_x = new int [m_max_wall];
int * p_wall_y = new int [m_max_wall];
};
void board::show_wall()
{
for (int i=0; i < m_max_wall; i++)
{
cout << "x[" << i << "] = " << *p_wall_x + i << endl;
cout << "y[" << i << "] = " << *p_wall_y + i << endl;
}
}
void board::create_board ()
{
for (int i=0; i < m_size; i++)
{
for (int j=0; j < m_size; j++)
{
if (rand() % m_chance_wall > 50)
{
cout << "[#]" ;
*p_wall_x++ = i;
*p_wall_y++ = j;
continue ;
}
cout << "[ ]" ;
}
cout << endl;
}
}
#endif // BOARD_H
MAIN
#include <iostream>
#include "include\board.h"
// m_ private class
// n_ public class
// p_ pointer
using namespace std;
int main()
{
srand( time( NULL ) );
board board_1;
board_1.size(10);
board_1.create_board();
board_1.show_wall();
return 0;
}
Topic archived. No new replies allowed.