尝试使用链表在 C# 中创建迷宫。不知道怎么开始游戏
Trying to create a maze in C# using a linked list. Can't figure out how to start the game
我已经创建了列表和 class 'Rooms'。我已将房间添加到列表中。现在我坚持编码游戏本身。基本上我想从房间 A 开始,只能通过按 (S)outh 按钮到达房间 E 和按 (W)est 按钮到达房间 B 等从房间 A 迭代到房间 E 和房间 B。
此外,我正在考虑为游戏调用不同的 class,以便代码可读。因此,main 将只有几行代码。
这是我的代码。请随时指出如何优化它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
roomsList theRooms = new roomsList();
theRooms.allRoomsList();
theRooms.addRoomToEnd("A");
theRooms.addRoomToEnd("B");
theRooms.addRoomToEnd("C");
theRooms.addRoomToEnd("D");
theRooms.addRoomToEnd("E");
theRooms.addRoomToEnd("F");
theRooms.addRoomToEnd("G");
theRooms.addRoomToEnd("H");
theRooms.addRoomToEnd("I");
theRooms.addRoomToEnd("J");
theRooms.addRoomToEnd("K");
theRooms.addRoomToEnd("L");
Console.WriteLine("What is your name?");
string playerName = Console.ReadLine();
Console.WriteLine("================================================================");
Console.WriteLine(playerName + " You have been Chosen, Enter if you dare!!");
Console.WriteLine("================================================================");
Console.WriteLine("(Y)es/(N)o");
string decision = Console.ReadLine();
if (decision == "y")
{
Console.WriteLine("You need to make you way to Room L, so you may live!!");
}
else
{
Console.WriteLine("Goodbye");
}
}
}
class rooms //create class rooms
{
private string roomname;
private rooms next;
public rooms(string rname) //constructor
{
roomname = rname;
next = null;
}
public void setRoom (rooms nxtRoom)
{
next = nxtRoom;
}
public rooms ftchNext()
{
return next;
}
public string ftchName()
{
return roomname;
}
}//done creating class rooms
class roomsList //create linked list
{
public rooms start, end;
public roomsList()
{
start = null;
end = null;
}
public void addRoomToEnd(string rname)
{
rooms current = new rooms(rname);
if (end == null)
{
start = current;
end = current;
}
else
{
end.setRoom(current);
end = current;
}
}
public void allRoomsList()
{
rooms current;
if (start != null)
{
current = start;
while (current != null)
{
current = current.ftchNext();
}
}
else { }
}
}//class list ends
}
总的来说,您的方向似乎是正确的,但缺少一些关键功能。您可以使用以下几点来完成实施
首先,问题陈述,正如我从你的问题中理解的那样:你的迷宫是房间的集合。您可以通过向北、向南、向东或向西移动来在房间之间移动。当您到达定义为出口的某个房间时,您就完成了迷宫。
房间 Class: 你的房间应该保持这样的概念,即它可以连接到不同的房间。我建议如下:
class Room
{
public string Name;
public Room NorthRoom;
public Room SouthRoom;
public Room EastRoom;
public Room WestRoom;
pulic bool isExit;
public ConnectTo(Room room, Direction direction)
{
// Add room connection logic here...
}
}
迷宫Class:迷宫class有一个起始房间,仅此而已。要创建迷宫,您将创建房间并根据需要在它们之间进行连接。
class Maze
{
public Room StartingRoom;
}
那么我们如何创建迷宫呢?我将展示一种简单的方法,但您可以在此处编写一些辅助函数以使其更容易。
//Lets say we want to have the following maze:
//
// start - room 1 - room 2
// | |
// room 3 room 4 (exit)
var maze = new Maze();
var startingRoom = new Room();
maze.StartingRoom = startingRoom;
var room1 = new Room();
var room2 = new Room();
var room3 = new Room();
var room4 = new Room();
room4.IsExit = true;
startingRoom.ConnectTo(room1, "East");
room1.ConnectTo(room2, "East");
room1.ConnectTo(room3, "South");
room2.ConnectTo(room4, "South");
要实现连接,请检查您作为输入获得的方向并设置适当的房间(北、南等)。
玩游戏:在您的主 class 中,在用户同意开始后,有一个 while 循环,当用户进入一个有退出 = 真。您跟踪用户所在的当前房间。他们从迷宫的起点开始。用户选择是否要前往 N、S、E、W。根据他们的选择,您将当前房间分别移动到北、南、东、西房间。您需要处理部分房间可能为空的情况。
希望这能给你足够的思考点来完成你的游戏!
我已经创建了列表和 class 'Rooms'。我已将房间添加到列表中。现在我坚持编码游戏本身。基本上我想从房间 A 开始,只能通过按 (S)outh 按钮到达房间 E 和按 (W)est 按钮到达房间 B 等从房间 A 迭代到房间 E 和房间 B。 此外,我正在考虑为游戏调用不同的 class,以便代码可读。因此,main 将只有几行代码。 这是我的代码。请随时指出如何优化它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
roomsList theRooms = new roomsList();
theRooms.allRoomsList();
theRooms.addRoomToEnd("A");
theRooms.addRoomToEnd("B");
theRooms.addRoomToEnd("C");
theRooms.addRoomToEnd("D");
theRooms.addRoomToEnd("E");
theRooms.addRoomToEnd("F");
theRooms.addRoomToEnd("G");
theRooms.addRoomToEnd("H");
theRooms.addRoomToEnd("I");
theRooms.addRoomToEnd("J");
theRooms.addRoomToEnd("K");
theRooms.addRoomToEnd("L");
Console.WriteLine("What is your name?");
string playerName = Console.ReadLine();
Console.WriteLine("================================================================");
Console.WriteLine(playerName + " You have been Chosen, Enter if you dare!!");
Console.WriteLine("================================================================");
Console.WriteLine("(Y)es/(N)o");
string decision = Console.ReadLine();
if (decision == "y")
{
Console.WriteLine("You need to make you way to Room L, so you may live!!");
}
else
{
Console.WriteLine("Goodbye");
}
}
}
class rooms //create class rooms
{
private string roomname;
private rooms next;
public rooms(string rname) //constructor
{
roomname = rname;
next = null;
}
public void setRoom (rooms nxtRoom)
{
next = nxtRoom;
}
public rooms ftchNext()
{
return next;
}
public string ftchName()
{
return roomname;
}
}//done creating class rooms
class roomsList //create linked list
{
public rooms start, end;
public roomsList()
{
start = null;
end = null;
}
public void addRoomToEnd(string rname)
{
rooms current = new rooms(rname);
if (end == null)
{
start = current;
end = current;
}
else
{
end.setRoom(current);
end = current;
}
}
public void allRoomsList()
{
rooms current;
if (start != null)
{
current = start;
while (current != null)
{
current = current.ftchNext();
}
}
else { }
}
}//class list ends
}
总的来说,您的方向似乎是正确的,但缺少一些关键功能。您可以使用以下几点来完成实施
首先,问题陈述,正如我从你的问题中理解的那样:你的迷宫是房间的集合。您可以通过向北、向南、向东或向西移动来在房间之间移动。当您到达定义为出口的某个房间时,您就完成了迷宫。
房间 Class: 你的房间应该保持这样的概念,即它可以连接到不同的房间。我建议如下:
class Room
{
public string Name;
public Room NorthRoom;
public Room SouthRoom;
public Room EastRoom;
public Room WestRoom;
pulic bool isExit;
public ConnectTo(Room room, Direction direction)
{
// Add room connection logic here...
}
}
迷宫Class:迷宫class有一个起始房间,仅此而已。要创建迷宫,您将创建房间并根据需要在它们之间进行连接。
class Maze
{
public Room StartingRoom;
}
那么我们如何创建迷宫呢?我将展示一种简单的方法,但您可以在此处编写一些辅助函数以使其更容易。
//Lets say we want to have the following maze:
//
// start - room 1 - room 2
// | |
// room 3 room 4 (exit)
var maze = new Maze();
var startingRoom = new Room();
maze.StartingRoom = startingRoom;
var room1 = new Room();
var room2 = new Room();
var room3 = new Room();
var room4 = new Room();
room4.IsExit = true;
startingRoom.ConnectTo(room1, "East");
room1.ConnectTo(room2, "East");
room1.ConnectTo(room3, "South");
room2.ConnectTo(room4, "South");
要实现连接,请检查您作为输入获得的方向并设置适当的房间(北、南等)。
玩游戏:在您的主 class 中,在用户同意开始后,有一个 while 循环,当用户进入一个有退出 = 真。您跟踪用户所在的当前房间。他们从迷宫的起点开始。用户选择是否要前往 N、S、E、W。根据他们的选择,您将当前房间分别移动到北、南、东、西房间。您需要处理部分房间可能为空的情况。
希望这能给你足够的思考点来完成你的游戏!