从设置为类的固定数组生成随机值
本文关键字:数组 随机 设置 | 更新日期: 2023-09-27 18:26:49
我们正在进行一个学校项目(一个学习游戏)和atm,我们正在使用一个从"堆栈"生成值的类。我们的问题是,在堆栈的值用完后,游戏/程序崩溃并停止。
我想把它改成一个数组,里面有固定值和单元格,每次寻址时都会给我一个随机值。
现在的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for CellInfoRepository
/// </summary>
public class CellProducer
{
System.Collections.Generic.Stack<Cell> _stack = new Stack<Cell>();
public CellProducer()
{
_stack.Push(new Cell { InstrumentName = "גיטרה", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "עוד", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "סיטאר", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "כינור", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "צ'לו", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "ויולה", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "נבל", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "בנג'ו", InstrumentFamily = 1, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "תופים", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "מצילה", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "שליש", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "קסטנייטה", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "פעמוניה", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "קסילופון", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "טמבורין", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "פעמוני רוח", InstrumentFamily = 2, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "קרן יער", InstrumentFamily = 3, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "בריטון", InstrumentFamily = 3, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "טרומבון", InstrumentFamily = 3, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "טובה", InstrumentFamily = 3, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "סקסופון", InstrumentFamily = 3, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "חליל", InstrumentFamily = 3, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "קלרינט", InstrumentFamily = 3, myPlayer = 0 });
_stack.Push(new Cell { InstrumentName = "משרוקית", InstrumentFamily = 3, myPlayer = 0 });
}
public Cell ProduceNextCell()
{
return _stack.Pop();
}
}
知道我如何保留类的名称,但只更改生成随机值的数组的内部吗?
顺便说一句,"cell"是我们制作的另一个类,数组应该由他制作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for Cells
/// </summary>
public class Cell:Panel //מחלקת משושים
{
protected string _instrumentName; //שם כלי הנגינה //
protected int _insturmentFamily; //סוג כלי הנגינה - המשפחה //
protected int _myPlayer; //ערכים מספריים של 0 - אין שחקן - 1 ו2 לפי השחקנים
public string InstrumentName
{
get
{
return _instrumentName;
}
set
{
_instrumentName = value;
}
}
public int InstrumentFamily
{
get
{
return _insturmentFamily;
}
set
{
_insturmentFamily = value;
}
}
public int myPlayer
{
get
{
return _myPlayer;
}
set
{
_myPlayer = value;
}
}
}
public class CellPosition // מחלקה ששומרת את מיקומי התאים
{
private int _Column;
private int _Row;
public int Column
{
get
{
return _Column;
}
set
{
_Column = value;
}
}
public int Row
{
get
{
return _Row;
}
set
{
_Row = value;
}
}
}
谢谢你的帮助,很抱歉所有的希伯来语笔记!
我还没有测试过,但我认为它会起作用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for CellInfoRepository
/// </summary>
public class CellProducer
{
System.Collections.Generic.List<Cell> cells = new List<Cell>();
Random random = new Random();
public CellProducer()
{
cells.Add(new Cell { InstrumentName = "גיטרה", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "עוד", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "סיטאר", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "כינור", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "צ'לו", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "ויולה", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "נבל", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "בנג'ו", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "תופים", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "מצילה", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "שליש", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "קסטנייטה", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "פעמוניה", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "קסילופון", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "טמבורין", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "פעמוני רוח", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "קרן יער", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "בריטון", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "טרומבון", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "טובה", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "סקסופון", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "חליל", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "קלרינט", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "משרוקית", InstrumentFamily = 3, myPlayer = 0 });
}
public Cell ProduceNextCell()
{
return cells[random.Next(cells.Count)];
}
}
您可以使用List或数组来存储仪器。然后生成一个介于0和元素数量之间的随机整数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for CellInfoRepository
/// </summary>
public class CellProducer
{
System.Collections.Generic.List<Cell> cells = new List<Cell>();
int index = -1;
public CellProducer()
{
cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "?'??", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???'?", InstrumentFamily = 1, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "????????", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "????????", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "?????? ???", InstrumentFamily = 2, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "??? ???", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "??????", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "??????", InstrumentFamily = 3, myPlayer = 0 });
cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 });
}
public Cell ProduceNextCell()
{
index++;
if(index>cells.length){
index = 0;
return cells[index];
}
}