【发布时间】: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