編碼新手,參加 C# 課程。我們正在研究封裝和 get:set/properties。
作業說我們必須構建一個類來創建一個帶有輸入邊數的骰子,并“滾動”亂數的骰子。簡單!
在第二個類中,我們必須構建一個函式來向池中添加或洗掉任意數量的骰子,然后將它們全部擲出以獲得結果。
我假設他們希望骰子池是一個私有串列。
我的邏輯是創建單個 OneDie 類,然后在主程式提示中使用 xDy 表示法將 x 個具有 y 面的骰子添加到串列中。(即:添加 2d6)
我已經構建了一個應該這樣做的 AddDie 函式,但是當我在完成后檢查我的串列計數時,計數為 0。每次我嘗試添加一個新的時,私有串列 (_dicePool) 似乎都重新設定為零反對名單。我懷疑我沒有正確構建我的屬性 DicePool 的獲取/設定功能,但我不確定如何從 DicePool{set} 內部呼叫我的 2 引數 AddDice 函式,或者即使這是我應該采取的方法。

假設串列應該是私有的,我是否缺少將新物件永久添加到串列中的內容?
編輯添加:或者,創建一個 ManyDice 物件會更好嗎?但是我如何從 OneDie 物件構建 this.Sid??es 和 this.Roll 呢?
這是我的代碼,適用于將物件(骰子)添加到串列(骰子池)。
class ManyDice
{
private List<OneDie> _dicePool = new List<OneDie>();
//What I think I might have to do:
public List<OneDie> DicePool
{
get
{
return this._dicePool;
}
set
{
//???????????? how do I call AddDice, when I need 2 parameters for it?
}
}
public void AddDie(int number, int sides)
{
for (int i = 0; i < number; i )
{
this.dicePool.Add(new OneDie(sides));
}
}
}
class OneDie
{
private int _sides, _rolledValue;
public int Sides
{
get
{
return this._sides;
}
set
{
this._sides = value;
}
}
public int RollValue
{
get
{
return this._rolledValue;
}
set
{
this._rolledValue = value;
RollIt(value);
}
}
public OneDie()
{
}
public OneDie(int sides)
{
this.Sides = sides;
this.RollValue = sides;
}
private int RollIt (int sides)
{
Random random = new Random((int)DateTime.Now.Ticks);
return random.Next(1, (sides 1));
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Let's roll some dice!");
Console.WriteLine("Please enter the number of dice you want to roll in the following format:");
Console.WriteLine("xdy, where \"x\" is the number of dice you want and \"y\" is how many sides they have.");
Console.WriteLine("Example: 2d6 is 2 6-sided dice. Perfect for playing Catan! (or Monopoly)");
Console.WriteLine("Please limit your dice to d4, d6, d8, d10, d12, d20");
Console.WriteLine("To add a die, type \"add xdy\" to add x number of y sided dice.");
Console.WriteLine("To remove a die, type \"remove xdy.\" to remove x number of y sided dice.");
Console.WriteLine("Type \"dice\" to see a list of all the dice in the pile.");
Console.WriteLine("Type \"roll\" to roll all the dice and see the results!");
Console.WriteLine("Type \"clear\" to clear all the dice and start agin!");
Console.WriteLine("Type \"exit\" to exit program\n");
PlayDice();
Console.ReadKey();
}
static void PlayDice()
{
do
{
string[] xDy = null;
int numberOfDice = 1;
int numberOfSides=1;
Console.WriteLine("\nEnter your command:");
string option = Console.ReadLine();
option = option.ToLower().Trim();
string[] words = option.Split(' ');
string command = words[0];
//CheckCommand(command);
if (words.Length > 1)
{
xDy = words[1].Split('d');
numberOfDice = int.Parse(xDy[0]);
numberOfSides = int.Parse(xDy[1]);
}
ManyDice die = new ManyDice();
if (command == "exit")
{
Console.WriteLine("Thank you, play again, soon!");
break;
}
else if (command == "add")
{
//numberOfSides=CheckDice(numberOfSides);
die.AddDie(numberOfDice, numberOfSides);
Console.WriteLine("You have {0}ed {1} {2}-sided dice.", command, numberOfDice, numberOfSides);
}
else if (command == "remove")
{
Console.WriteLine("You have {0}d {1} {2}-sided dice.", command, numberOfDice, numberOfSides);
}
else if (command == "dice")
{
Console.WriteLine("These are your dice:");
die.Display();
}
else if (command == "roll")
{
Console.WriteLine("Here is your roll:");
}
else if (command == "clear")
{
Console.WriteLine("All dice have been cleared.");
}
} while (true);
}
static int CheckDice(int sides)
{
List<int> check = new List<int> {4,6,8,10,12,20};
while (!check.Contains(sides))
{
Console.WriteLine("{0}-sided dice are not available.\nPlease enter 4,6,8,10,12 or 20");
sides = int.Parse(Console.ReadLine());
}
return sides;
}
static string CheckCommand(string instructions)
{
List<string> check = new List<string> { "add", "remove", "dice", "roll","clear", "exit" };
while (!check.Contains(instructions))
{
Console.WriteLine("Command not recognized.\nPlease enter \"add\", \"remove\", \"dice\", \"roll\",\"clear\", or \"exit\"");
instructions = Console.ReadLine();
}
return instructions;
}
}
uj5u.com熱心網友回復:
基于評論和更新問題的新答案:
該行ManyDice die = new ManyDice();正在擦除您的骰子串列,以清除程式中的每個回圈。它用一個新的類實體替換你的變數,一個新的串列等等。
只需在回圈開始之前移動該行:
行前 do {
然后每次迭代都將使用 ManyDice 的相同實體,并且都將共享變數 die,而不會覆寫它。
舊答案:據我所知,您的程式只運行一次。然后你需要再次啟動它以放入另一個骰子。您的主要功能只要求輸入一次。每當您再次啟動程式時,程式中使用的所有記憶體都會被清除。除非我遺漏了什么,這就是為什么你的名單會繼續被重置。下次嘗試添加骰子時,您實際上正在運行一個全新的程式。所以它不知道以前的運行。
一種解決方案是說(偽代碼)
While (running) {
// what you have now
if (option == “done”) running = false;
if (option == “roll”) // roll all dice.
}
這將不斷提示用戶輸入命令,直到他們輸入完成。它保持相同的程式,這樣您就不會丟失早期命令中的資料。
根據評論更新:您在每次迭代時都重新創建 ManyDice 實體,有效地從頭開始。將實體保存在 while 回圈之外,然后重用它。
暗示:
您的滾動可能應該由 manyDice.RollAll() 完成,并且可能應該回傳一個 RollResults 串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340163.html
下一篇:在Python中創建嵌套字典
