我目前正在将文本游戏作为C#(WPF)的学校项目。关于必须使用哪些结构和OOP原则(类,继承,异常......)有一些限制因此我最终创建了3个基本类:
风景(应该是场景I quess)
{
public class Scenery
{
/* VARIABLES */
// used as reference for the creator, possibly for printout into label
internal string _ID;
public string ID { get { return _ID; } }
// is written into text box after entering the scene
// records changes the user made on the scene
private string _phrase;
internal string Phrase { get { return _phrase; } }
// keys are IDs of game items within the scene
// values are items themselves
internal Dictionary<string, Item> _items;
// keys are IDs of people within the scene
// values are people themselves
internal Dictionary<string, Person> _people;
public Dictionary<string, Person> People { get { return _people; } }
// directions in which the player can go
internal Scenery[] _directions;
/* CONSTRUCTORS */
/* METHODS */
人
{
public class Person
{
/* VARIABLES */
// basic identifier
internal string _ID;
public string ID { get { return _ID; } }
// keys are keywords the person knows about
// values are answers given when asking about a specific keyword
internal Dictionary<string, string> _answers;
public bool Answering { get { if (_answers == null) return false; else return true; } }
// tree-like structure used to have a dialog with this person
internal Node _talk;
public bool Talking { get { if (_talk == null) return false; else return true; } }
// phrase to be written out when you start talking to the person
// when reseting/looping is set to current _talk value
private string _firstPhrase;
public string FirstPhrase { get { return _firstPhrase; } }
// talking interface window
internal Dialog _dialog;
public Dialog Dialog { get { return _dialog; } }
/* CONSTRUCTORS */
/* METHODS */
项目
{
public class Item
{
/* VARIABLES */
// identifier, used for writing out general messages and identifying user input
private string _ID;
public string ID { get { return _ID; } }
// indicates if this item can be broken
protected bool _breakable;
// message to be written out after this item is broken
protected string _breakPhrase;
public string BreakPhrase { get { return _breakPhrase; } }
// list of item got after breaking this item
protected Dictionary<string, Item> _afterBreak;
public Dictionary<string, Item> AfterBreak { get { return _afterBreak; } }
public string AfterBreakPrint
{
get
{
string toPrint = "";
foreach (string key in _afterBreak.Keys) toPrint += '\n' + key;
return toPrint;
}
}
// indicates if this item can be investigated
private bool _investigate;
public bool Investigate { get { return _investigate; } }
// reference to the investigation scene
private Scenery _investigation;
public Scenery Investigation { get { return _investigation; } }
/* CONSTRUCTORS */
/* METHODS */
有一些子类,例如PickableItem,UsableItem等。结构相当复杂(在子类中得到Dictionary&gt;等等),但这些方法在方法中很容易使用,至少从我的观点来看。
现在问题:我有一个单独的类GameData,我在其中初始化游戏的所有实例 - 场景,项目,人物然后放入正确的结构,另外还有一个特殊的公共变量用于一个函数来确定正确的项目键。现在看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
namespace Textova_Hra
{
public static class GameData
{
public static Scenery start; // starting location
public static List<string> NA_ID_LIST = new List<string>(); // list of IDs in format 'x na y'
// used in Addons -> GetTwoItems
// Basically compiles the whole game returning the starting point
// Declarations must be written from the inside out
// Hra je pro češtinu, ID objektů zadávejte ve 4.pádě, jinak nebude správně fungovat!
// First goes items and people "containing" only strings(and numbers)
// Then goes lists of these items which you want to use in other items etc.
// Last goes sceneries with directions arrays declared first, then scenery declarations,
// then filling corresponding directions arrays
// Finaly the starting scene is saved into the start variable and returned
// The game may begin!
public static Scenery make_start()
{
Dictionary<string, Node> alisTalk1 = new Dictionary<string, Node>();
Dictionary<string, Node> alisLoop = new Dictionary<string, Node>();
Dictionary<string, string> alisAnswers = new Dictionary<string, string>();
alisAnswers.Add("cestu", "Tady zrovna žádné cesty nevedou.");
Node alisTalk11 = new Node("Tak to je skvělé. Už musím běžet.");
Node alisTalk12 = new Node("To je ale velice smutné. No nic, už budu muset jít.", alisLoop, true);
alisTalk1.Add("dobře", alisTalk11);
alisTalk1.Add("špatně", alisTalk12);
Node alisTalk = new Node("Ahoj, jak se máš?", alisTalk1, false);
alisLoop.Add("reset", alisTalk);
Person Alis = new Person("Alis", alisAnswers, alisTalk);
Dictionary<string, Person> start_people = new Dictionary<string, Person>();
start_people.Add("Alis", Alis);
Dictionary<string, Item> canBreak = new Dictionary<string, Item>();
UsableItem hammer = new UsableItem("kladivo", "Sebrali jste pevné železné kladivo. Určitě se jen tak nerozbije.", -1, "", canBreak);
Dictionary<string, Item> flowerVaseAfter = new Dictionary<string, Item>();
Item flowerVase = new Item("váza s květinou", false, "Váza se s ohlušujícím třískotem rozbila na střepy, vypadly z ní mince. Květina leží uvadle na zemi.", flowerVaseAfter);
Dictionary<string, Tuple<string, Item>> flowerChange = new Dictionary<string, Tuple<string, Item>>();
Tuple<string, Item> flowervaset = new Tuple<string, Item>("vázu s květinou", flowerVase);
flowerChange.Add("vázu", flowervaset);
Dictionary<string, string> flowerPhrase = new Dictionary<string, string>();
flowerPhrase.Add("vázu", "Dali jste květinu do vázy. Toto aranžmá místnost poněkud oživilo.");
UsableItem flower = new UsableItem("květina", "Sebrali jste zvláštní namodralou květinu, poněkud uvadlou.", 1, "Květina je nyní ve váze.", flowerChange, flowerPhrase);
PickableItem fragments = new PickableItem("střepy", "Sebrali jste hromádku střepů, třeba se budou ještě k něčemu hodit.");
PickableItem coins = new PickableItem("mince", "Sebrali jste pár zlatých mincí, kterým jste zkrátka nemohli ododlat.");
Dictionary<string, Item> afterVase = new Dictionary<string, Item>();
afterVase.AddMany(fragments, coins);
flowerVaseAfter.AddRange(afterVase);
Scenery[] vaseBack = new Scenery[4];
Scenery vaseInvestigate = new Scenery("Zkoumáte vázu", "Ve váze vidíte několik mincí, ale hrdlo je příliš úzké, aby se daly vysypat.", vaseBack);
Item vase = new Item("váza", true, "Váza se s ohlušujícím třískotem rozbila na střepy a vypadly z ní mince.", afterVase, vaseInvestigate);
canBreak.Add("vázu", vase);
canBreak.Add("vázu s květinou", flowerVase);
Dictionary<string, Item> startItems = new Dictionary<string, Item>();
startItems.Add("vázu", vase);
startItems.Add("květinu", flower);
startItems.Add("kladivo", hammer);
Scenery[] startDirs = new Scenery[4];
Scenery x = new Scenery("Úvod", "Vítejte v nové hře." + '\n' + "Jste v místnosti v malém domku. Mítnost je poloprázdná, je zde akorát malý stůl s vázou, vedle vázy leží květina a pod stolem někdo zapomněl kladivo.", startItems, start_people, startDirs);
vaseBack[0] = x;
start = x;
return start;
}
}
}
这表示1个场景共有5个项目和1个人,可以说话和简单的问题。我称之为:
public MainWindow()
{
InitializeComponent();
/* GameData - static class containing declarations of all game instances + helpful variables
method make_start inicializes all game instances and returns a starting point */
current = GameData.make_start();
// print out starting scene description
tbStory.Text = current.Phrase;
lblSceneName.Content = current.ID;
Application.Current.MainWindow.Closed += new EventHandler(MainWindow_Closed);
}
游戏本身在wpf窗口中播放,其中组合框用于用户输入,文本框用于显示所有文本和列表框以及库存。这一切都很好,但游戏的初始化真的很乱,无法检查。
我想知道,如果有类似实例管理器的东西(使用Visual Studio 2012),或者它可以以更简单的方式完成。我也在考虑为游戏自己创建一个实例管理器,使其成为一个可用的引擎,但是没有足够的时间进行编程而且我还不擅长编程(仅在C#6周工作) 。
有什么想法吗?
PS:一个班级有多少个构造函数有限制?我的UsableItem类最终有24个覆盖了所有类型的可用项目,对我来说似乎太过分了......
答案 0 :(得分:0)
您可以像这样使用初始化:
Dictionary<string, Node> list = new Dictionary<string, string>
{
{ "One", new Node("One") },
{ "Two", new Node("Two") }
};
只要签名不匹配,您添加到类中的构造函数的数量就没有限制(即每个构造函数都必须拥有它自己唯一的输入参数集)。
public class Test1
{
public Test1() { }
public Test1(int i) { }
public Test1(int i) { } /* Syntax error 'Test already defines a member called Test with the same parameter types */
}
public class Test2
{
public Test2() { }
public Test2(int i) { }
public Test2(int i, int j) { }
}