我正在开展一个扑克项目。我试图从文件生成随机卡,没有任何重复。这就像一张5张牌抽奖游戏,我只是不希望它能够获得同一套房中的5张或两张牌。
以下是我的代码
for (int i = 0; i < 5; i++)
{
CardDisplay[i] = getRandomImage();
}
if (!IsPostBack)
{
Discard.Enabled = false;
PokerCard1.Enabled = false;
PokerCard2.Enabled = false;
PokerCard3.Enabled = false;
PokerCard4.Enabled = false;
PokerCard5.Enabled = false;
Hold1.Visible = false;
Hold2.Visible = false;
Hold3.Visible = false;
Hold4.Visible = false;
Hold5.Visible = false;
PokerCard1.ImageUrl = Path.Combine("/My_Portfolio/App_Themes/Portfolio/Images/Poker/", CardDisplay[0]);
PokerCard2.ImageUrl = Path.Combine("/My_Portfolio/App_Themes/Portfolio/Images/Poker/", CardDisplay[1]);
PokerCard3.ImageUrl = Path.Combine("/My_Portfolio/App_Themes/Portfolio/Images/Poker/", CardDisplay[2]);
PokerCard4.ImageUrl = Path.Combine("/My_Portfolio/App_Themes/Portfolio/Images/Poker/", CardDisplay[3]);
PokerCard5.ImageUrl = Path.Combine("/My_Portfolio/App_Themes/Portfolio/Images/Poker/", CardDisplay[4]);
}
public string getRandomImage()
{
string[] fileNames = Directory.GetFiles(MapPath("/My_Portfolio/App_Themes/Portfolio/Images/Poker/"));
string CardToShow = fileNames[rand.Next(fileNames.Length)];
return Path.GetFileName(CardToShow);
}
获取随机图像方法显然是我从文件中调用图像的地方。
以下是
的截图答案 0 :(得分:0)
实现这一目标的一个简单方法是创建一个变量来保存所有当前的选择&#34;,并在从getRandomImage方法返回一张卡之前检查一下,如下所示:
private List<int> _elections = new List<int>();
private string[] _fileNames = Directory.GetFiles(MapPath("/My_Portfolio/App_Themes/Portfolio/Images/Poker/"));
public string getRandomImage()
{
int currentPick;
while (true){
currentPick = rand.Next(_fileNames.Length);
if (!_elections.Contains(currentPick)){
_elections.Add(currentPick);
break;
}
}
string CardToShow = _fileNames[currentPick];
return Path.GetFileName(CardToShow);
}
请注意,我提取了您的文件名变量,因此每次点击getRandomImage方法时都不会重新计算文件。