2017-04-02 287 views
0

我试图在控制台应用程序中编写生命程序游戏,但保留运行时异常,即我的实例化单元对象为空。C#实例化对象为空

我正在通过一个2d数组循环并创建对象,然后我将它们可视化,如果它们还活着。我不断收到一个错误,当我尝试访问的位置

(Console.WriteLine(cell.Isalive.ToString());)

它总是发生在我尝试访问我的对象的属性甚至可以通过他们的值

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Drawing; 
using System.IO; 
using System.Diagnostics; 

namespace Game_of_life 
{ 
class Program 
{ 

    static Random rnd = new Random(); 

    static void Main(string[] args) 
    { 


     Console.SetWindowSize(40, 40); 


     Cell[,] Points = new Cell[Console.WindowHeight, Console.WindowWidth]; 
     for(int i = 0; i < Console.WindowHeight; i++) 
     { 
      for(int j = 0; j < Console.WindowWidth; j++) 
      { 
       bool Alive = false; 
       if (rnd.Next(0, 10) == 1) 
        Alive = true; 
       Cell cell = new Cell(i, j, Alive); 
      } 

     } 

     while (true) 
     { 
      Console.Clear(); 
      foreach(Cell cell in Points) 
      { 
       Console.WriteLine(cell.Isalive.ToString()); 
       Tuple<int, int>[] points = GetNeighbors(new Point(cell.Position.X, cell.Position.Y)); 

       for(int i = 0; i < 4; i++){ 
        if (points[0] != null) 
         cell.Neighbors[i].Position = new Point(points[i].Item1, points[i].Item2); 
       } 

       if(cell.Isalive == true) 
       { 
        Console.SetCursorPosition(cell.Position.X, cell.Position.Y); 
        Console.Write("X"); 
       } 
       System.Threading.Thread.Sleep(500); 
      } 
     } 

    } 


    static Tuple<int, int>[] GetNeighbors(Point pos) 
    { 
     Tuple<int, int>[] points = new Tuple<int, int>[4]; 
     if (pos.X - 1 > 0) { 
      points[0] = Tuple.Create(pos.X - 1, pos.Y); 
       } 
     else 
     { 
      points[0] = null; 
     } 
     if(pos.X + 1< Console.WindowWidth) 
     { 
      points[1] = Tuple.Create(pos.X + 1, pos.Y); 
     } 

     if(pos.Y - 1 > 0) 
     { 
      points[2] = Tuple.Create(pos.X, pos.Y - 1); 
     } 
     else 
     { 
      points[0] = null; 
     } 

     if (pos.Y + 1 < Console.WindowHeight) 
     { 
      points[3] = Tuple.Create(pos.X, pos.Y + 1); 
     } 
     else 
     { 
      points[0] = null; 
     } 


     return points; 
    } 
} 

}

这里是我的类代码:

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Drawing; 

    namespace Game_of_life 
    { 
     class Cell 
     { 
      //Defines if the cell is currently alive 
      private bool isAlive; 
      public bool Isalive { get; set; } 


    //Neighbors of the cell 
    private Cell[] neighnors = new Cell[4]; 
    public Cell[] Neighbors { get; set; } 

    private Point position; 
    public Point Position 
     { 
     get 
     { 
      return position; 
     } 
     set 
     { 
      position = value; 
     } 
     } 

    private int neighborCount; 
    public int NeighborCount { get; set; } 



    public Cell (int x , int y, bool Alive) 
    { 
     this.position = new Point(x, y); 
     isAlive = Alive; 
    } 

} 
    } 

回答

2

您正在为整个窗口中的每个x,y坐标对创建Cell对象,但是您没有在Point数组中设置这些单元对象。

Cell[,] Points = new Cell[Console.WindowHeight, Console.WindowWidth]; 
    for(int i = 0; i < Console.WindowHeight; i++) 
    { 
     for(int j = 0; j < Console.WindowWidth; j++) 
     { 
      bool Alive = false; 
      if (rnd.Next(0, 10) == 1) 
       Alive = true; 
      Cell cell = new Cell(i, j, Alive); 
      Points[i, j] = cell; //this is missing 
     } 
    } 

这样的事情应该有所帮助。