【问题标题】:My program keeps changing the color of my entire array to one color instead of different colors for each one我的程序不断将整个数组的颜色更改为一种颜色,而不是每种颜色都不同
【发布时间】:2021-01-03 17:49:53
【问题描述】:

我正在为我的课堂作业制作一个小板生成器,我在其中获取一个 2D 数组并用一个名为“tile”的对象填充它,该对象具有自己的符号、背景和前景色(即 empty = " #",背景为红色,前景为蓝色,而 room = ".",背景为绿色,前景为黄色)。

我已经创建了电路板并开始在上面绘制房间以及我的瓷砖课程。我唯一的问题是,每当我绘制我的板时,它只使用给定的最后一个瓷砖对象参数,所以如果我最后绘制一个房间,它就有房间参数。我尝试通过更改创建磁贴的位置并尝试创建磁贴来覆盖它来解决它。

这是我的板类代码的代码:

namespace BrandonPlayerGen
{
public class Board
{
    public static object[,] board;
    public static int height;
    public static int width;
    
    
    
    public static Array MakeBoard(int bwidth, int bheight)
    {
        Tile empty = new Tile("#", ConsoleColor.Red, ConsoleColor.Yellow,false);
        board = new object[bheight, bwidth];
        for (int hcheck = 0; hcheck < (bheight-1); hcheck++)
        {
            for(int wcheck = 0; wcheck < (bwidth-1); wcheck++)
            {
                
                
                board[hcheck, wcheck] = empty;
                
            }
        }

        return board;
    }

    
    
    public static void DrawBoard(int dheight, int dwidth)
    {
        dwidth = width;
        dheight = height;
        for (int hcheck = 0; hcheck<dheight; hcheck++)
        {
            for (int wcheck = 0; wcheck < dwidth; wcheck++)
            {
                Tile.DrawTile();
            }
            Console.Write("\n");
        }
    }



    public static Array MakeRoom()
    {
        Tile room = new Tile(".", ConsoleColor.Blue, ConsoleColor.Red,true);

        int bheight = (height-1);
        int bwidth = (width-1);

        int hstart = Random.randInt(1, bheight);
        int wstart = Random.randInt(1, bwidth);
        int rheight = Random.randInt(1, 5);
        int rwidth = Random.randInt(1, 5);

        while (hstart > height || hstart < 1 || wstart < 1 || wstart > width||
            (hstart+rheight)>bheight|| (wstart+rwidth)>bwidth)
        {
            hstart = Random.randInt(1, bheight);
            wstart = Random.randInt(1, bwidth);
            rheight = Random.randInt(1, 5);
            rwidth = Random.randInt(1, 5);
        }

        for (; hstart<rheight; hstart++)
        {
            for (; wstart < rwidth; wstart++)
            {
                board[hstart, wstart] = room;
            }
        }

        return board;

    }

这是我的 tile 类的代码:

namespace BrandonPlayerGen
{
public class Tile
{
    public static string symbol;
    public static ConsoleColor foreColor;
    public static ConsoleColor backColor;
    public static string stairisHere;

    public Tile(string boardSymbol, ConsoleColor 
        back, ConsoleColor fore, bool stairs)
    {
        backColor = back;
        foreColor = fore;
        symbol = boardSymbol;
        if (stairs == true)
        {
            symbol = "<";
            backColor = ConsoleColor.Green;
            foreColor = ConsoleColor.White;
        }
    }

    public static void DrawTile()
    {
        
        Console.BackgroundColor = backColor;
        Console.ForegroundColor = foreColor;
        Console.Write(symbol);
        Console.BackgroundColor = ConsoleColor.Black;
        Console.ForegroundColor = ConsoleColor.White;

    }

    
}
}

这里是主要调用板的地方:

int height, width;
        Console.Write("Please Enter Board height: ");
        height = Convert.ToInt32(Console.ReadLine());
        Console.Write("Please Enter Board height: ");
        width = Convert.ToInt32(Console.ReadLine());
        Board.height = height;
        Board.width = width;

        Board.MakeBoard(height, width);
        Board.DrawBoard(Board.height, Board.width);
        Console.ReadKey();
        Board.MakeRoom();
        
        Board.DrawBoard( Board.width, Board.height);
        Console.ReadKey();

我们将不胜感激任何帮助,如果我的问题格式不正确,这是我的第一个问题。

编辑: 修复了瓷砖的生成方式,每次都会创建一个新的瓷砖,但是当我显示棋盘时,整个棋盘的颜色和符号都是相同的。

【问题讨论】:

  • 您只创建了一个Tile 实例。您需要为板上的每个单元格创建一个新单元格(使用 new 关键字),即在您的内部 for 循环中。
  • 或者,您可以将Tile 设为结构,以便按值而不是按引用传递

标签: c# multidimensional-array colors console


【解决方案1】:

您只创建了一个Tile 实例。您需要为板上的每个单元格创建一个新单元格。

另外,除非你打算在你的板上包含其他东西,否则我建议它的类型应该是 Tile[,] 而不是 object[,]

board = new Tile[bheight, bwidth];
for (int hcheck = 0; hcheck < (bheight-1); hcheck++)
{
    for(int wcheck = 0; wcheck < (bwidth-1); wcheck++)
    {
        Tile empty = new Tile("#", ConsoleColor.Red, ConsoleColor.Yellow,false);            
        board[hcheck, wcheck] = empty;
    }
}

【讨论】:

  • 感谢@John Wu 的帮助,我按照你说的做了,我还通过执行以下过程更改了我的房间创建。但颜色问题仍然存在。 for (; hstart
  • 很抱歉打扰您的另一条评论,并感谢您的光临,但在看到混乱的混乱之后,我决定让您的眼睛免于阅读时的痛苦。所以我做了你展示的,并为我的 MakeRoom 函数实现了这个解决方案。问题是颜色仍然存在,符号仍然保持不变。问题是我的 DrawTile 函数还是完全是其他地方的问题?任何帮助或至少在正确方向上的引导将不胜感激。
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2017-05-09
相关资源
最近更新 更多