Ошибка динамического массива в игре Conways Game of LifeC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Ошибка динамического массива в игре Conways Game of Life

Сообщение Anonymous »

Я работаю над программой, которая имитирует игру «Жизнь Конвея», и она отлично работает с заданными размерами. Однако как только я попытаюсь использовать динамические размеры, как показано в варианте е, у меня начнутся проблемы. Основная проблема заключается в функции «жизни», которая выполняет итерацию по массиву и решает, следует ли оживлять ячейку. Я некоторое время занимаюсь отладкой и ввожу размеры как 50*40, он повторяется до 61, 1. Технически это должно работать, но каждый раз просто ломается. Имейте в виду, что я добавляю 12 к каждому размеру, чтобы учесть буферную зону, которую я помещаю по краям. Технически это должно сработать, верно? Если у вас есть какие-либо предложения, я буду очень признателен!
#include
#include
#include
#include
#include
#include // i havent used this one yet
#include

using namespace std;

// REMEMBER: The outside of the array is 6 more than what we show so that nothing interferes
//also that it goes y,x and that x is going to be bigger so that we get a rectange

//we use the copy function to copy an array from eachother, either the current one to the temp one or
//vise versa. This is so that we can alter the cells one step at a time without affecting everything else.

void copy(int **array1, int **array2, int o, int p)
{
for(int j = 0; j < o; j++)
{
for(int i = 0; i < p; i++)
array2[j] = array1[j];
}
} // the second array sent is assigned the first array sent!

//this array will initialize our arrays so that we can use them later
int** init(int n, int m)
{
int **array;
array = new int*[m]; // x
array = new int*[n]; // y

for (int q=0; q < n; q++)

{
array[q] = new int[n];

for (int w=0; w < m; w++)
{
array[w] = new int[m];
}
}
return array;
}

void populate(int o, int p, int** board){ // THIS FUNCTION HASN'T BEEN USED YET

for(int i=0; i < p; i++)
{
for(int j=0; j < o; j++) // It was in a in-class demo but i dont think i need it
{
board[j] = pow(i, j);
}
}
}

//The life function looks at the pieces around the cell and figures out what happens next.
// Probably the most important in the entire program, feast your eyes!

void life(int **array, int o, int p)
{
//Copies the main array to a temp array so changes can be made without affecting anyone else
int **temp;
temp = init(o, p);

copy(array, temp, o, p);

for(int j = 1; j < o; j++)
{
for(int i = 1; i < p; i++)
{
// checks all 8 cells surrounding it
int count = 0;
cout

Подробнее здесь: https://stackoverflow.com/questions/306 ... me-of-life
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»