2012-04-11 2 views
0

저는 C#의 Game of Life 프로그램을 작성하고 있습니다. 구조체의 2D 배열을 사용하고 있습니다. 이웃이나 뭔가의 알고리즘이 잘못되어있는 임의의 메소드를 표시 할 때 나타납니다. 그것이 세대를 거치면서 무작위적인 세포는 그들이 살아야 할 때 "살아있다". 어떤 도움이 필요합니까? 랜덤 객체가 한 번만 생성 클래스의 정적 멤버로 선언 class.By의 모든 객체에 의해 사용되어야한다생명의 게임 알고리즘 문제

public struct cellDetail 
{ 
    public int curGenStatus; 
    public int nextGenStatus; 
    public int age; 

} 
public class Class1 
{ 

    static cellDetail[,] Generations(cellDetail[,] cells) 
    { 

     int neighbours = 0; 

     for (int i = 0; i < 40; i++) 
      for (int j = 0; j < 60; j++) 
       cells[i, j].nextGenStatus = 0; 

     for (int row = 0; row < 39; row++) 
      for (int col = 0; col < 59; col++) 
      { 
       neighbours = 0; 

       if (row > 0 && col > 0) 
       { 
        if (cells[row - 1, col - 1].curGenStatus > 0) 
         neighbours++; 
        if (cells[row - 1, col].curGenStatus > 0) 
         neighbours++; 
        if (cells[row - 1, col + 1].curGenStatus > 0) 
         neighbours++; 

        if (cells[row, col - 1].curGenStatus > 0) 
         neighbours++; 
        if (cells[row, col + 1].curGenStatus > 0) 
         neighbours++; 

        if (cells[row + 1, col - 1].curGenStatus > 0) 
         neighbours++; 
       } 

       if (cells[row + 1, col].curGenStatus > 0) 
        neighbours++; 
       if (cells[row + 1, col + 1].curGenStatus > 0) 
        neighbours++; 

       if (neighbours < 2) 
        cells[row, col].nextGenStatus = 0; 
       if (neighbours > 3) 
        cells[row, col].nextGenStatus = 0; 
       if ((neighbours == 2 || neighbours == 3) && cells[row, col].curGenStatus > 0) 
        cells[row, col].nextGenStatus = 1; 
       if (neighbours == 3 && cells[row, col].curGenStatus == 0) 
        cells[row, col].nextGenStatus = 1; 
      } 

     for (int i = 0; i < 40; i++) 
      for (int j = 0; j < 60; j++) 
       cells[i, j].curGenStatus = cells[i, j].nextGenStatus; 

     return cells; 
    } 

    static void PrintCells(cellDetail[,] cells) 
    { 
     for (int row = 0; row < 40; row++) 
     { 
      for (int col = 0; col < 60; col++) 
      { 
       if (cells[row, col].curGenStatus != 0) 
       { 
        cells[row, col].curGenStatus = (char)30; 
        Console.Write((char)cells[row, col].curGenStatus); 
       } 
       else 
        Console.Write(" "); 
      } 
     } 
    } 

    public static void Random(int numOfGenerations) 
    { 
     cellDetail[,] cells = new cellDetail[40,60]; 
     Random rand = new Random(); 

     for (int row = 0; row < 40; row++) 
      for (int col = 0; col < 60; col++) 
       cells[row, col].curGenStatus = rand.Next(0, 2); 

     for (int i = 0; i < numOfGenerations; i++) 
     { 
      Console.ForegroundColor = (ConsoleColor.Green); 
      Generations(cells); 
      Console.SetCursorPosition(0, 3); 
      PrintCells(cells); 
     } 

    } 
} 
+0

숙제 태그가 있습니까? – Servy

+2

근본적인 문제인지 충분히 자세히 살펴 보지 못했지만 짧은 시간 안에 많은 수의 '임의'개체를 만들어서는 안됩니다. 그들은 기본적으로 현재 시간으로 시드됩니다. 간단히 대답하면'Random'을 한 번 만들고 저장하면 반복적으로'.Next'를 호출 할 수 있습니다. – Servy

+0

예. 숙제표가 있어야합니다. 나는 그것을 태그했다. –

답변

5

이 더 나은 옵션은 싱글 헬퍼 클래스를 생성하는 것입니다 achieved.A 될 수 있습니다 무작위로 개체.

+3

사람들을 혼란스럽게하지 않도록 적절한 용어를 사용하십시오. 'Static'은 C#에서 특별한 의미가 있습니다. 나는 당신이 의도 한 것이 '단일 인스턴스'라고 믿습니다. –

+0

문제에 대한 설명에서 나는 이것이 유일한 문제라고 생각하지 않는다. 아마도 OP가 요구하는 문제는 아니지만 그가 여전히 가지고있는 진정한 문제라고 생각한다. – Servy

+0

@Boo이 경우이 메서드를 사용하는 메서드는 정적이므로 정적 필드 여야합니다. –