0

我在 C++ 中有一个基本的“生命游戏”程序,我需要更改以接受用户输入的网格大小和有多少邻居将使细胞存活或死亡的参数。目前,我专注于网格大小的用户输入,但我遇到了 maxrow 和 maxcol 变量的“未声明标识符”问题。这是代码,底部是我用来接受用户输入的函数。该程序按原样工作,但是当我尝试实现用户输入功能时,它给了我所有这些错误。我知道我必须删除头文件中的常量整数,但我认为我的错误来自某些不了解 maxrow 和 maxcol 是什么的函数,例如“指令”函数。如何以在整个程序中识别变量的方式实现它?感谢您的任何帮助。

生活.h

#ifndef LIFE_H
#define LIFE_H

const int maxrow = 20, maxcol = 60; 

class Life
{
  public:
    Life(void);
    ~Life(void);
    void initialize();
    void print();
    void update();
    int getNeighbor_count(int, int);
    void instructions();
    bool user_says_yes();

  private:
    int grid[maxrow+2][maxcol+2];   // allows for two extra rows and columns
    int neighbor_count(int row, int col);
};

#endif

生活.cpp

#include <iostream>
#include <string>
#include <fstream> 
#include "life.h"
using namespace std;


Life::Life()
{
}


Life::~Life(void)
{
}


int Life::neighbor_count(int row, int col)
/* Pre: The Life object contains a configuration, and the coordinates row and col define a cell inside its hedge.
Returns: The number of living neighbors of the specified */
{
    int i, j;
    int count = 0;
    for(i = row-1; i <= row+1; i++)
      for(j = col-1; j <= col+1; j++)
       count += grid[i][j]; // Increase if neighbor is alive.
    count -= grid[row][col]; // Reduce count, cell is not its own neighbor.
    return count;

}

void Life::update( )
/*Pre: The Life object contains a configuration.
  Post: The Life object contains the next generation of configuration.*/
{
   int row, col;
   int new_grid[maxrow + 2][maxcol + 2];
   for(row = 1; row <= maxrow; row++)
      for(col = 1; col <= maxcol; col++)
         switch (neighbor_count(row, col)) {
           case 2: // Status stays the same.
             new_grid[row][col] = grid[row][col];             
             break;
           case 3: // Cell is now alive.
             new_grid[row][col] = 1;
             break;
           default: // Cell is now dead
             new_grid[row][col] = 0;
         }
  // Copy new configuration back to original 
  for(row = 1; row <= maxrow; row++)
      for(col = 1; col <= maxcol; col++)
          grid[row][col] = new_grid[row][col];
}


void Life::initialize( )
/* Pre: None.
   Post: The Life object contains a configuration specified by the user.*/
{
   int row, col;
   for(row = 0; row <= maxrow+1; row++)
      for(col = 0; col <= maxcol+1; col++)
    grid[row][col] = 0;
   cout <<"List the coordinates for living cells." << endl;
   cout << "Terminate the list with the the special pair -1 -1"<< endl;
   cin >> row >> col;
   while (row != -1 || col != -1) 
   {
      if(row >= 1 && row <= maxrow)
         if(col >= 1 && col <= maxcol)
             grid[row][col] = 1;
         else
             cout << "Column " << col << " is out of range." << endl;
      else
             cout << "Row " << row << " is out of range." << endl;
      cin >> row >> col;
   }
}

void Life::print( )
/*  Pre: The Life object contains a configuration.
    Post: The configuration is written for the user.
*/
{
   int row, col;
   cout << "\nThe current Life configurations is: "<< endl;
   for(row = 1; row <= maxrow; row++) {
      for(col = 1; col <= maxcol; col++)
         if(grid[row][col] == 1) cout << '*';
         else cout << ' ';
      cout << endl;
   }
   cout << endl;
}   

主文件

#include <iostream>
#include <string>
#include <fstream> 
#include "life.h"
using namespace std;

void instructions();
bool user_says_yes();

void Life::instructions( )
/* Pre: None.
   Post: Instructions for using the Life program have been printed.*/
{
   cout << "Welcome to Conway's game of Life." << endl;
   cout << "This game uses a grid of size "
        << maxrow << " by " << maxcol << " in which" << endl;
   cout << "each cell can either be occupied by an organism or not." << endl;
   cout << "The occupied cells change from generation to generation" << endl;
   cout << "according to the number of neighboring cells ";
   cout << "which are alive." << endl;
}

bool Life::user_says_yes()
{
   int c;
   bool initial_response = true;
   do { // Loop until an appropriate input is received.
      if(initial_response)
         cout << "Would you like to see the next generation (y,n)? " << flush;
      else
         cout << "Respond with either y or n: " << flush;
      do { // Ignore white space.
         c = cin.get( );
      } while (c == '\n' || c == ' ' || c == '\t');
      initial_response = false;
  } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
 return (c == 'y' || c == 'Y');
}

int Life::getNeighbor_count(int row, int col)
{
    return neighbor_count(row, col);
}


int main()// driver for neighbor_count( )
/* Pre: None.
   Post: Verifies that the method neighbor_count( )returns the correct values.
   Uses: The class Life and its method initialize( ).
*/
{
    Life configuration;
    configuration.instructions();
    configuration.initialize();
   do{
   for(int row = 1; row <= maxrow; row++){
      for(int col = 1; col <= maxrow; col++)
         cout << configuration.getNeighbor_count(row,col) << " ";
         cout << endl;
   }
   configuration.print();
   configuration.update();
   }while(configuration.user_says_yes());

}

最大列和最大行的用户输入函数

void getGridParameters(int& maxrow, int& maxcol)
{
    cout << "Enter number of rows for the grid: ";
    cin >> maxrow;
    cout << "Enter number of columns for the grid ";
    cin >> maxcol;
}
4

3 回答 3

0

问题是您的网格被声明为int grid[maxrow+2][maxcol+2];,这需要maxrowmaxcol是常量。

C++ 有一种用于运行时大小的数组的类型,std::vector< >. 由于您有一个 2D 网格,因此您需要一个向量向量:std::vector<std::vector<int>>. 使用grid保持不变:grid[i][j]仍然有效。但是你必须设置初始大小:

void Life::setupGrid()
{
  // I've got maxrow rows, and each row has maxcol columns.
  grid.resize(maxrow, std::vector<int>(maxcol));
}

现在不再要求 maxrow 和 maxcol 保持不变。作为一个好处,您现在可以说for (auto& row : grid) { for (auto& cell: row) { /** stuff **/ } }对每个单元格做一些事情。

于 2013-09-30T22:11:32.637 回答
0

任何其他函数都可以看到生成变量的旧方法是将它们声明global为不好的变量。对于maxrow 和 maxcol的instruction函数,您可以将这些值传递给该函数,如下所示understand

void instructions(int maxrow, int maxcol );

如果要更改传入的值,可以通过引用传递

void instructions(int &maxrow, int& maxcol );

(虽然对于这个函数,你可能不需要通过引用传递。)

于 2013-09-30T20:53:57.987 回答
-1

maxrow并且maxcolconst并且它们在头文件中声明。正如您所说,这归结为它们在您编译的二进制文件中作为变量消失。

要使它们成为变量,您应该将它们声明为Life类的成员变量:

class Life {
  private:
    int maxcol, maxrows;
    ..
}

由于它们将变为非常量,因此您不能直接在Life类中声明二维数组,您需要动态分配它,例如:

int **grid = new int*[maxrows];
for (int i = 0; i < maxrows; ++i)
  grid[i] = new int[maxcols];

这将要求您在Life析构函数中释放内存。另一种解决方案是使用单维数组,例如int **grid = new int[maxrows*maxcols]为了简化分配,但需要更改索引功能。

关于如何存储它们,由于它们将成为 Life 类的一部分,因此您可以直接将输入值保存在其中:

void Life::getNeighbor_count() {
  cin >> maxrows;
  cin >> maxcols;
  allocateGrid();
}
于 2013-09-30T20:56:01.277 回答