如何将Java枚举移植到C#?
我是一名C#(以及一般编程)的初学者,我决定为学校彻底检查我的地下城爬虫项目,而不是使用预先确定的地图生成随机地图,由于我有一些Java经验,而且我对c语言非常熟悉,我认为使用我已经理解的Java代码来基于Java代码用c语言编写一些东西是明智的,但我遇到了一个问题 目前我正在尝试编写以下代码: 在c#中,进展相当顺利 只有如何将Java枚举移植到C#?,java,c#,enums,console-application,Java,C#,Enums,Console Application,我是一名C#(以及一般编程)的初学者,我决定为学校彻底检查我的地下城爬虫项目,而不是使用预先确定的地图生成随机地图,由于我有一些Java经验,而且我对c语言非常熟悉,我认为使用我已经理解的Java代码来基于Java代码用c语言编写一些东西是明智的,但我遇到了一个问题 目前我正在尝试编写以下代码: 在c#中,进展相当顺利 只有 private void generateMaze(int cx, int cy) { 及 部分根本不起作用,它现在已经创建了一个完全填充的映射,但它仍然需要添加路径,经
private void generateMaze(int cx, int cy) {
及
部分根本不起作用,它现在已经创建了一个完全填充的映射,但它仍然需要添加路径,经过一些搜索后,我发现C#Enum根本没有Java的功能,如何“转换”Enum Dir部分和generateMaze到C#代码
目前我有:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DungeonCrawler
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Wide?");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Height?");
int y = Convert.ToInt32(Console.ReadLine());
MazeGen maze = new MazeGen(x, y);
maze.display();
Console.ReadKey();
}
}
}
马泽根
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DungeonCrawler
{
public class MazeGen
{
private int x;
private int y;
private int[,] maze;
public MazeGen(int x, int y)
{
this.x = x;
this.y = y;
maze = new int[this.x, this.y];
generateMaze(0, 0);
}
public void display()
{
for (int i = 0; i < y; i++)
{
//Bovenkant
for (int j = 0; j < x; j++)
{
Console.Write((maze[j, i] & 1) == 0 ? "+---" : "+ ");
}
Console.WriteLine("+");
//Linkerkant
for (int j = 0; j < x; j++)
{
Console.Write((maze[j, i] & 8) == 0 ? "| " : " ");
}
Console.WriteLine("|");
}
//Onderkant
for (int j = 0; j < x; j++)
{
Console.Write("+---");
}
Console.WriteLine("+");
}
private void generateMaze(int cx, int cy)
{
}
private static Boolean between(int v, int upper)
{
return (v >= 0) && (v < upper);
}
private enum DIR
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间地下城爬虫
{
公共级马泽根
{
私人INTX;
私营企业;
私有int[,]迷宫;
公共马塞根(内部x,内部y)
{
这个.x=x;
这个。y=y;
maze=newint[this.x,this.y];
generateMaze(0,0);
}
公共空间显示()
{
for(int i=0;i=0)和(v<上限);
}
私有枚举目录
{
}
}
}
这个问题/答案可能会提供一些很好的见解:我已经打开了一个,但我必须承认,我不明白那里发生了什么,以及我将如何将其实现到我现在拥有的代码中。重要的是:C中最像java枚举的结构是私有的,没有公共构造函数的C#类的静态实例。这样,您就可以在编译时声明类和它的所有可能实例。好吧,我明白您的意思,但是我如何在C#中实现这一部分呢?N(1,0,-1),S(2,0,1),E(4,1,0),W(8,-1,0)<代码>公共静态目录N=新目录(1,0,-1)代码>然后对S、E和W重复此操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DungeonCrawler
{
public class MazeGen
{
private int x;
private int y;
private int[,] maze;
public MazeGen(int x, int y)
{
this.x = x;
this.y = y;
maze = new int[this.x, this.y];
generateMaze(0, 0);
}
public void display()
{
for (int i = 0; i < y; i++)
{
//Bovenkant
for (int j = 0; j < x; j++)
{
Console.Write((maze[j, i] & 1) == 0 ? "+---" : "+ ");
}
Console.WriteLine("+");
//Linkerkant
for (int j = 0; j < x; j++)
{
Console.Write((maze[j, i] & 8) == 0 ? "| " : " ");
}
Console.WriteLine("|");
}
//Onderkant
for (int j = 0; j < x; j++)
{
Console.Write("+---");
}
Console.WriteLine("+");
}
private void generateMaze(int cx, int cy)
{
}
private static Boolean between(int v, int upper)
{
return (v >= 0) && (v < upper);
}
private enum DIR
{
}
}
}