康威的生命游戏错误

时间:2016-09-21 10:31:29

标签: c++

我在写康威的生活游戏。代码运行完美,但在控制台窗口中运行后,写入“清除”命令或拼写错误 找不到。“我不明白这一点。为什么会发生这种情况?我怎样才能解决明确的命令?

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

void copy(int array1[10][10], int array2[10][10])
{
  for(int j = 0; j < 10; j++)
  {
        for(int i = 0; i < 10; i++)
            array2[j][i] = array1[j][i];
    }
}


void life(int array[10][10], char choice)
{

  int temp[10][10];
  copy(array, temp);
    for(int j = 0; j < 10; j++)
    {
        for(int i = 0; i < 10; i++)
        {
            if(choice == 'm')
            {

                int count = 0;
                count = array[j-1][i] +
                    array[j-1][i-1] +
                    array[j][i-1] +
                    array[j+1][i-1] +
                    array[j+1][i] +
                    array[j+1][i+1] +
                    array[j][i+1] +
                    array[j-1][i+1];

        if(count < 2 || count > 3)
                    temp[j][i] = 0;

        if(count == 2)
                    temp[j][i] = array[j][i];

        if(count == 3)
                    temp[j][i] = 1;
            }


        }
    }

  copy(temp, array);
}


bool compare(int array1[10][10], int array2[10][10])
{
    int count = 0;
    for(int j = 0; j < 10; j++)
    {
        for(int i = 0; i < 10; i++)
        {
            if(array1[j][i]==array2[j][i])
                count++;
        }
    }

    if(count == 10*10)
      return true;
    else
      return false;
}


void print(int array[10][10])
{
    for(int j = 0; j < 10; j++)
    {
        for(int i = 0; i < 10; i++)
        {
            if(array[j][i] == 1)
                cout << '*';
            else
                cout << ' ';
        }
        cout << endl;
    }
}

int main()
{
    int gen0[10][10];
    int todo[10][10];
    int backup[10][10];
    char neighborhood;
    char again;
  char cont;
  bool comparison;
    string decoration;




  do
    {

    do
        {
      cout << "Which neighborhood would you like to use (m): ";
          cin >> neighborhood;
        }while(neighborhood != 'm');

    system("clear");
    int i = 0;

    do
        {

            srand(time(NULL));

            for(int j = 0; j < 10; j++)
            {
                for (int i = 0; i < 10; i++)
                    gen0[j][i] = rand() % 2;
            }



      if(i == 0)
                copy(gen0, todo);
            copy(todo, backup);
            print(todo);
          life(todo, neighborhood);
          i++;


      system("sleep .5");

      if(i % 10 == 1 && i != 1)
      {
        cout << endl;

        do
        {
          cout << "Would you like to continue this simulation? (y/n): ";
          cin >> cont;
        }while(cont != 'y' && cont != 'n');
        if(cont == 'n')
          break;
      }

      comparison = compare(todo, backup);
      if(comparison == false)
        system("clear");
      if(comparison == true)
        cout << endl;
        }while(comparison == false);

    do
    {
      cout << "Would you like to run another simulation? (y/n): ";
          cin >> again;
    }while(again != 'y' && again != 'n');
    }while(again == 'y');
    return 0;
}

1 个答案:

答案 0 :(得分:0)

窗口清除方法取决于系统。你好像不知道命令&#34;清楚&#34;。试试system("CLS")

您也可能有兴趣查看:system("clear"); not working but system("cls"); works