#include "grid.h" #include Grid::Grid() { numRows = 20; numCols = 10; Initialize(numRows, numCols); } Grid::Grid(int rows, int cols) { numRows = rows; numCols = cols; Initialize(numRows, numCols); } void Grid::Initialize(int Rows, int Cols) { /* 변수 설명 numRows : Grid 클래스 내의 행 갯수 numCols : Grid 클래스 내의 열 갯수 Rows : 이 함수가 인자로 받는 행의 갯수. 사실 굳이 인자로 받을 필요는 없어 보이긴 한다. Cols : 이 함수가 인자로 받는 열의 갯수. 사실 굳이 인자로 받을 필요는 없어 보이긴 한다. */ // 2차원 배열에서 행의 범위 설정 arr = new int*[Rows]; // 2차원 배열 열의 범위 설정 for (int row =0; row < Rows; row++) { arr[row] = new int[Cols]; } // 위에서 설정한 2차원 배열에 값을 초기화 for (int row = 0; row < Rows; row++) { for (int col =0; col < Cols; col++) { arr[row][col] = 0; } } } void Grid::Print() { // 설정한 numRows와 numCols 값의 범위 내에서 배열의 값을 출력 for (int row = 0; row < numRows; row++) { for (int col =0; col < numCols; col++) { std::cout << arr[row][col] << " "; } std::cout << std::endl; } } Grid::~Grid() { for (int row = 0;row < numRows; row ++) delete[] arr[row]; delete[] arr; } void Grid::SetGridValue(int row, int col, int value) { arr[row][col] = value; }