2014-11-13 2 views
-3

gameOfLife.cpp, life.cpp 및 life.h 파일이 제공됩니다. 프로그램을 작동 시키려면 life.cpp 만 편집 할 수 있습니다. 나는 내가 익숙하지 않은 것들이 너무 많기 때문에 life.cpp를 편집하는 곳을 모른다. 나는 파일 checkoutLife.cpp를 받으면서 내 일을 점검한다.Conway의 Game of Life에 기반한 과제에 의해 압도 됨

나는 지난 2 일 동안 다른 사람들의 Game of Life 파일을 보면서 진행 방법을 알아 내려했지만 잃어 버렸습니다. 나는 누군가가 내 일을하기를 원하지 않지만 어떤 방향이 필요합니다.

gameOfLife.cpp

#include <iostream> 
#include "life.cpp" 
#include "life.h" 

const int GENERATIONS=100; 

using namespace std; 

//make a random array of initial living cells 
void gen(bool a[ROWS][COLS]){ 
    for(int i=0;i<ROWS;++i){ 
     for(int j=0;j<COLS;++j){ 
      if(rand()%100<10)a[i][j]=true; 
      else a[i][j]=false; 
     } 
    } 
    a[5][5]=true; 
    a[5][6]=true; 
    a[5][7]=true; 
    return; 
} 

// check to see if two arrays are equal 
bool equal(const bool a[ROWS][COLS], const bool b[ROWS][COLS]){ 
    int i,j; 
    for(i=0;i<ROWS;++i)for(j=0;j<COLS;++j)if(a[i][j]!=b[i][j])return false; 
    return true; 
} 

//copy the b array into the a array 
void copy(bool a[ROWS][COLS], const bool b[ROWS][COLS]){ 
    for(int i=0;i<ROWS;++i){ 
     for(int j=0;j<COLS;++j){ 
      a[i][j]=b[i][j]; 
     } 
    } 
    return; 
} 

//print out the array 
void print(const bool a[ROWS][COLS]){ 
    for(int i=0;i<ROWS;++i){ 
     for(int j=0;j<COLS;++j){ 
      if(a[i][j])cout << 'X'; 
      else  cout << ' '; 
     } 
     cout << endl; 
    } 
    return; 
} 


int main(){ 
    bool current[ROWS][COLS]; 
    bool next[ROWS][COLS]; 
    int i=0; 

    //initialze the cell array and print it out 
    gen(current); 
    print(current); 

    while(i<GENERATIONS){ 
     //get a carriage return before the next generation 
     cin.get(); 

     //give the current generation to life() 
     //it puts the next generation into next 
     life(current,next); 

     //copy the next generation into the current 
     copy(current,next); 

     //print it out 
     print(current); 
     i++; 
    } 

    return 0; 
} 

life.cpp

/*1. You need to write a file life.cpp that implements the function prototyped in life.h. You can and should write other functions 
    and tuck them into the same file; whatever you need to get your function working in an elegant manner. 

    2. Compile your file with checkoutLife.cpp and run the resulting executable to see if it passes all the tests. 

    3. Compile yourfile with gameOfLife.cpp and run the resulting executable to see if it makes pretty pictures. 

    4. If you are convinced steps 2 and 3 are working, submit your life.cpp via canvas. 
*/ 

#include <iostream> 
#include <cstdlib> 
#include "life.h" 

using namespace std; 

void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]){ 

} 

life.h

#ifndef LIFE_H 
#define LIFE_H 
const int ROWS=25; 
const int COLS=25; 

// Simulate one generation of Conways Game of Life 
// 
// Given: 
//  a constant 2D bool array called "current" where each true element 
//  indicates a live cell and each false element indicates a dead cell. 
// 
//  an empty 2D bool array called "next" 

void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]); 
#endif 
+0

당신은 게임을 이해합니까? 즉, 다음 세대에 셀을 생기게하거나 죽게 만드는 개념을 개념적으로 이해합니까? –

+0

예. 나는 Wikipedia 페이지를 두 번 읽었고 그것을 이해하기 위해 다른 페이지를 보았다. 나는 어떻게 각 세포가 살고, 죽고, 재생산 되겠는가를 이해하지만 C++로 번역하는 방법을 모른다. – Zelphh

답변

1

수명은() 두 개의 매개 변수와 함께 호출됩니다 보드의 현재 상태의 배열 (아마 당신은 만지지 않을 것입니다) 그리고 이사회의 다음 주 (당신이 거주 할 것입니다)의 배열. COL의 각 COL 들어

  • : 행의 각 행에 대해

    • : 여기

      당신이 사용할 수있는 알고리즘이다
      • 현재 [] [] 모든 주변 이웃을 추가, "이웃"
      • 현재 [행] [COL]가 살아 있으면 이웃에 저장 <이 죽은 다음 [행] [COL] =
      • 그렇지
      • 현재 [행] [COL]가 알 경우 ive 및 neighbor == 2 또는 3, 다음 [row] [col] = alive
      • 현재 [row] [col]이 살아 있고 이웃> 4, 다음 [row] [col] = dead 인 경우
      • Else 현재 [행] [COL]가 죽은 이웃 == 3 인 경우, 다음 [행] [COL]는

당신은 약간의 논리를 정리할 수있는 살아있는 =,하지만 난 그것을 인쇄 Wikipedia의 Game of Life 항목에 나온 규칙처럼 복잡한 부분 (IMO)은 "주변에있는 모든 이웃을 추가하십시오."- 여기서 많은 경계를 확인합니다. 나는 당신이 쓰는 다른 방법을 부름을 제안한다.

관련 문제