2017-04-02 290 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 
     } 
    } 

這樣的事情應該有所幫助。