2

目前正在为课堂编写 C# 中的康威生活。我一直在采取一些小步骤来学习语言和游戏编程,并且在打印我的 2D 字符数组时遇到了障碍。目前我正在使用 GetLength - 1 来不超过界限,但它无法打印出数组中的最后一个字符。

我的初始文件是什么样的

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

读入后放入 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();
        }
4

3 回答 3

0

您的offspring函数中有一个错误。请注意,您在bigBang函数中正确执行此操作。

您正在循环 while x < GetLength()-1。您只需要x < GetLength(),因为不包括 when 的情况x == GetLength()

一个类比循环:

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

输出:

0
1
2
3

于 2013-02-07T22:35:52.570 回答
0

我不熟悉游戏原理,但你的offspring方法有问题。

y < universe.GetLength(1)-1

这转换为y < 3 - 1or y < 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]);

给出最终输出

++*
*+*
+**
++*
于 2013-02-07T22:44:08.383 回答
0

虽然我将深入研究为什么它没有按预期工作,但我只是在创建数组时将数组的大小传递给我的 offspring() 并在打印时使用这些值。一旦完成了那个小的改变,输出就会按预期出现。

 *  
  * 
****
于 2013-02-10T23:23:05.050 回答