2013-02-07 81 views
2

目前正在爲C#編寫Conways人生課程。我一直在採取一些小步驟來解決語言和遊戲編程問題,並且在打印2D字符數組時遇到了麻煩。目前,我正在使用GetLength-1不能越過界限,但它無法打印出陣列中的最後一個字符。正確地打印2D陣列?

什麼我最初的文件看起來像

+*++ 
++*+ 
**** 

後其讀入放入CHAR(我相信)

* 
    * 
**** 

什麼結束了印刷

* 

** 



using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Text; 

namespace ConwaysLife 
{ 
    class Program 
    { 
     static char[,] universe; 
     static void bigBang(int h, int w, List<string> grid) 
     { 
      universe = new char[h,w]; 

      int row = 0; 

      foreach (string line in grid) 
      { 

       for (int i = 0; i < line.Length; i++) 
       { 
        if (line.ToCharArray()[i] == '*') 
        { 
         universe[row, i] = '*'; 
        } 
       } 
       row++; 
      } 
     } 

     //How I'm attempting to print out my 2D char array 
     static void offspring() 
     { 
      StringBuilder cellLine = new StringBuilder(); 
      for (int y = 0; y < universe.GetLength(1)-1; y++) 
      { 
       for (int x = 0; x < universe.GetLength(0)-1; x++) 
       { 
        Console.Write(universe[y, x]); 
       } 
       Console.WriteLine();     
      } 
      //pause 
      Console.ReadLine(); 
     } 

     static void Main(string[] args) 
     { 
      List<string> tempLine = new List<string>(); 

      //Console.WriteLine("File Path?"); 
      int width = 0; 
      int height = 0; 

      //read file into List 
      using (StreamReader r = new StreamReader("life.txt")) 
      { 
       while (r.Peek() >= 0) 
       { 
        tempLine.Add(r.ReadLine()); 

        //compare current width to new width 
        if (tempLine[height].Length >= width) { width = tempLine[height].Length; } 

        //increase height when going to next row 
        height++; 
       } 
       bigBang(height, width, tempLine); 
      } 
      offspring(); 
     } 
    } 
} 

更新後代()

static void offspring() 
     { 
      StringBuilder cellLine = new StringBuilder(); 
      for (int x = 0; x <= universe.GetLength(1); x++) 
      { 
       for (int y = 0; y <= universe.GetLength(0); y++) 
       { 
        Console.Write(universe[x, y]); 
       } 
       Console.WriteLine(); 
      } 
      //pause 
      Console.ReadLine(); 
     } 
+0

無關,與你真正的問題,但在C#字符串有一個索引,因此沒有必要使用「ToCharArray」,你可以做線[I] – Kippie

+0

感謝。我可能只是把它寫下來,並認爲我找到了一條捷徑:p不要猜! – GoodBoyNYC

回答

0

最終輸出雖然我將深入研究,爲什麼它如我所料它不工作,我只是通過更深當我將它創建到我的後代()時,數組的大小和打印時使用這些值。一旦這個小小的改變完成,產出如預期一樣。

* 
    * 
**** 
0

您的offspring函數中存在錯誤的錯誤。請注意,您在bigBang函數中正確執行此操作。您正在循環,而x < GetLength()-1。你只需要x < GetLength(),因爲那個時候排除x == GetLength()的情況。

,以相似的循環:

for (i = 0; i < 4; i++) 
    Console.WriteLine(i); 

輸出:

0

我不熟悉遊戲原則,但您的存在問題方法。

y < universe.GetLength(1)-1 

這轉化爲y < 3 - 1y < 2,讓您的迭代從Y = 0到1

要解決,只需刪除的-1兩個出現次數。

 for (int y = 0; y < universe.GetLength(1); y++) 
     { 
      for (int x = 0; x < universe.GetLength(0); x++) 
      { 

此外,當您訪問universe時,您的指數反轉。

Console.Write(universe[y, x]); 

在那裏您使用y變量來訪問該行,x使用該列。逆應該做這樣的:

Console.Write(universe[x, y]); 

給予的

++* 
*+* 
+** 
++* 
+0

您的建議工作正常,但打印輸出顯示爲正面。我用更新的後代()更新了頂端,但在右側打印時它仍然沒有索引。 – GoodBoyNYC

+0

你能提供預期的產出嗎?我看到你切換了你的兩個for循環的順序,它應該打印與你的輸入相同的結構。 – siger