gpt4 book ai didi

c# - 初学者,用 C# 制作基于控制台的 RPG 但有问题

转载 作者:行者123 更新时间:2023-12-03 23:07:00 28 4
gpt4 key购买 nike

以下是我目前所拥有的:玩家可以输入诸如北、东、南、西和帮助之类的命令,并且它可以工作。当我执行 Look 命令时,我的问题就出现了。我记录了当前房间的描述,但它永远不会改变。我相信,问题可能与那些神秘的引用和值类型有关,但我没有足够的经验来确定,或者如何解决它。
然而 ,当我把 Console.WriteLine(CurrentRoom.RoomDescription) 放在任何 currentRoom.SetCurrentRoom(rooms, player) 下时,它工作得很好。

主要类:

class Program
{
static void Main(string[] args)
{
Room currentRoom;
List<Room> rooms = new List<Room>();
Player player = new Player();

//Rooms
Room startingRoom = new Room("Starting room", "This is the starting room", 0, 0);
Room northRoom = new Room("North room", "This is the north room", 0, 1);
Room eastRoom = new Room("East room", "This is the east room", 1, 0);
Room southRoom = new Room("South room", "This is the south room", 0, -1);
Room westRoom = new Room("West room", "This is the west room", -1, 0);

//Add and set starting room as current room
rooms.Add(startingRoom);
currentRoom = startingRoom;

//Adding rooms
rooms.Add(northRoom);
rooms.Add(eastRoom);
rooms.Add(southRoom);
rooms.Add(westRoom);

Console.WriteLine("Welcome to the Dungeon. This is the starting room.");
GetHelp();
Console.WriteLine("There are four exits. Which way do you want to go?");
while (true)
{
HandleInput(player, currentRoom, rooms);
}



}

private static void GetHelp()
{
Console.WriteLine("You can move to locations by entering 'North', 'East', 'South', and 'West'.");
Console.WriteLine("You can also examine the room by entering 'Look'.");
Console.WriteLine("Type 'Help' at any time to view instructions.");
}

private static Room SetCurrentRoom(List<Room> rooms, Player player)
{
foreach (var room in rooms)
{
if (room.XPos == player.PlayerXPos && room.YPos == player.PlayerYPos)
{
return room;
}

}
Console.WriteLine("Invalid direction.");

return null;

}

private static void HandleInput(Player player, Room currentRoom, List<Room> rooms)
{
var input = Console.ReadLine();
if (input == "North")
{
player.Move(Direction.Directions.North);
currentRoom = SetCurrentRoom(rooms, player);


}
else if (input == "East")
{
player.Move(Direction.Directions.East);
currentRoom = SetCurrentRoom(rooms, player);


}
else if (input == "South")
{
player.Move(Direction.Directions.South);
currentRoom = SetCurrentRoom(rooms, player);


}
else if (input == "West")
{
player.Move(Direction.Directions.West);
currentRoom = SetCurrentRoom(rooms, player);

}
else if (input == "Help")
{
GetHelp();
}
else if (input == "Look")
{
Console.WriteLine(currentRoom.RoomDescription);
}
else
{
Console.WriteLine("Invalid input.");
}

}
}

房间等级:
public class Room
{
public string RoomName { get; set; }
public string RoomDescription { get; set; }
public int XPos { get; set; }
public int YPos { get; set; }

public Room(string roomName, string roomDescription, int xPos, int yPos)
{
this.RoomName = roomName;
this.RoomDescription = roomDescription;
this.XPos = xPos;
this.YPos = yPos;

}
}

球员等级:
public class Player
{
public int Health { get; set; }
public string Name { get; set; }
public int PlayerXPos { get; set; }
public int PlayerYPos { get; set; }

public void Move(Direction.Directions direction)
{
switch (direction)
{
case Direction.Directions.North:
PlayerYPos += 1;
Console.WriteLine("Moving north");
break;
case Direction.Directions.East:
PlayerXPos += 1;
Console.WriteLine("Moving east.");
break;
case Direction.Directions.South:
PlayerYPos -= 1;
Console.WriteLine("Moving south.");
break;
case Direction.Directions.West:
PlayerXPos -= 1;
Console.WriteLine("Moving west.");
break;

}
}


}

方向类:
public class Direction
{
public enum Directions
{
North,
East,
South,
West
}
}

我是初学者,所以代码可能会让有经验的程序员反感。

最佳答案

I believe, the problem may have to do with those mystical reference and value types



你很亲近。问题是因为 currentRoom传递给 HandleInput方法“按值”而不是“按引用”,但您正在尝试分配不同的 Room到它。

您的签名 HandleInput方法是这样的:
private static void HandleInput(Player player, Room currentRoom, List<Room> rooms)

在您尝试设置的方法中 currentRoom到另一个 Room .

但是默认情况下,当某些东西传递给方法时,它是“按值”传递的,这意味着您可以更改该变量的属性,但不能更改 实际它所指的变量 - 就像 currentRoom = SetCurrentRoom(rooms, player); - 除非你通过“引用”传递它,像这样:
private static void HandleInput(Player player, ref Room currentRoom, List<Room> rooms)

在这种情况下,您必须像这样调用它:
HandleInput(player, ref currentRoom, rooms);

什么 ref关键字的意思是你调用的方法可以改变值类型 currentRoom的“底层对象”指。

关于c# - 初学者,用 C# 制作基于控制台的 RPG 但有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61925043/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com