I am trying to code a "Conways Game of Life" Visualization. I think I have a solid idea on how to go about it but my issue I am running into is this: When I attempt to output the rows and columns of my 2d array, it starts jumping between numbers towards the end and it never stops scrolling the numbers. It seems to get caught on the "x" of 78.
我正在试着编写一个“生活的游戏”的可视化。我想我对如何处理这个问题有一个很好的想法,但我遇到的问题是:当我尝试输出2d数组的行和列时,它开始在数字之间跳来跳去,而且它永远不会停止滚动数字。它似乎在78的“x”上被抓住了。
#include <iostream>
#include <cstring>
#include <cstdlib>
#define HEIGHT 25
#define WIDTH 80
using namespace std;
void makeBoard();
int seed = 0;
int main()
{
makeBoard();
}
void makeBoard()
{
int board[79][24] = {0};
/* Seed the random number generator with the specified seed */
srand(seed);
for(int x = 0; x <= 79; x++)
{
for(int y = 0; y <= 24; y++)
{
/* 50% chance for a cell to be alive */
if(rand() % 100 < 50)
{
board[x][y] = {1};
}
else
{
board[x][y] = {0};
}
/*if(board[x][y] == 1) {
cout << "SPAM" << endl;
}*/
//this is just printing out the current location it is iterating through.
cout << "X: " << x << " Y: " << y << endl;
}
cout << endl;
}
}
all of the code needed to run it should be right there.
运行它所需的所有代码都应该在这里。
Thank you for your help and patience.
谢谢你的帮助和耐心。
2 个解决方案
#1
6
Your indices are out of bounds. an array of [79][24] has indices going from 0-19, and 0-23. Your condition stop at 79 and 24 respectively. Replace <= with <.
你的指标超出界限。一个[79][24]的数组有0-19和0-23的索引。你的情况分别在79和24。用 <替换< =。< p>
#2
0
An array of size N goes from 0 to n-1. You need to replace the <= with <, since you're running out of bounds along each dimension of the array.
一个大小为N的数组从0到N -1。您需要将<=替换为<,因为在数组的每个维度上都超出了界限。
Note also that you only have 79 columns and 24 rows, not the 80 and 25 you defined at the top of your program. You can fix this by doing:
还要注意,您只有79列和24行,而不是在程序顶部定义的80和25行。你可以通过以下方法来解决这个问题:
int board[HEIGHT][WIDTH];
Then substitute the 79 and 24 with HEIGHT and WIDTH respectively, and change the <= in the loop conditions to <. That way all you need do is change those single values at the top to change the size of the board throughout.
然后将79和24分别替换为HEIGHT和WIDTH,并将循环条件中的<=更改为<。这样你所需要做的就是改变顶部的单个值来改变整个板的大小。
#1
6
Your indices are out of bounds. an array of [79][24] has indices going from 0-19, and 0-23. Your condition stop at 79 and 24 respectively. Replace <= with <.
你的指标超出界限。一个[79][24]的数组有0-19和0-23的索引。你的情况分别在79和24。用 <替换< =。< p>
#2
0
An array of size N goes from 0 to n-1. You need to replace the <= with <, since you're running out of bounds along each dimension of the array.
一个大小为N的数组从0到N -1。您需要将<=替换为<,因为在数组的每个维度上都超出了界限。
Note also that you only have 79 columns and 24 rows, not the 80 and 25 you defined at the top of your program. You can fix this by doing:
还要注意,您只有79列和24行,而不是在程序顶部定义的80和25行。你可以通过以下方法来解决这个问题:
int board[HEIGHT][WIDTH];
Then substitute the 79 and 24 with HEIGHT and WIDTH respectively, and change the <= in the loop conditions to <. That way all you need do is change those single values at the top to change the size of the board throughout.
然后将79和24分别替换为HEIGHT和WIDTH,并将循环条件中的<=更改为<。这样你所需要做的就是改变顶部的单个值来改变整个板的大小。