2016-09-21 47 views
0

嗨,我正在編寫康威的人生遊戲代碼。我需要在這裏分配0.5秒的迭代時間。矩陣應該每0.5秒更新一次。如何在這裏分配0.5秒的迭代時間?我想過使用計時器。但如何實現這一點?我的代碼如下:在conways生活遊戲中分配迭代時間

/* 
* pp1.cpp 
* 
* Created on: 21.09.2016 
*  Author: fislam 
*/ 
#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]) 
{ int temp[10][10]; 
copy (array,temp); 
for (int j=0;j<10;j++) 
    {  for (int i=0;i<1;i++) 
     { 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); 
} 
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; 
    } 
} 
+0

你要同步或異步計時器? –

回答

1

之間兩個電話接入life,插入this_thread::sleep_for有500毫秒

std::this_thread::sleep_for(std::chrono::milliseconds{500}); 

如果你喜歡C-風格,使用usleep

不要忘記包括必要的標題。

喜歡的東西:

int main() { 
    // how may iteration of 'life' 
    const int MAX_ITER=100; 
    int world[10][10]; 

    // set your world to the initial configuration 
    // .... 

    for(int i-0; i<MAX_ITER; i++) { 
    life(world); 
    print(world); 
    // wait for half a second 
    std::this_thread::sleep_for(std::chrono::milliseconds{500}); 
    } 
} 
+0

究竟在哪個地方?對不起,我是這個主題的新手。 –

+1

@TamannaIslam你沒有發佈你的所有代碼。 l'life'功能應該是做一次生命的重複。要進行整個演變,您需要在某個地方反覆調用它。 –

+0

嗨,謝謝。代碼的其餘部分很長,這就是爲什麼我沒有在這裏粘貼 –