【发布时间】:2017-09-30 05:16:33
【问题描述】:
我有一个掷骰子游戏,你可以掷骰子来得分。目前,一旦达到 25 分,就会出现一条消息,说该玩家是获胜者,但程序卡在一个高达 50 分的循环中。一旦达到分数限制,我想结束游戏,但我不太确定怎么做。我想我需要一个 do-while 循环,但我不确定如何添加,因为总分限制在另一个类中。
class Game
{
private static void Main(string[] args) ----- Main method:
{
if (gamemode == 1)
{
quickgame();
}
}
private static void quickgame() ---- Game carried out
{
Console.WriteLine("\nInstructions: Players take turns rolling all five dice and scoring for three-of-a-kind or better. \n\t If a player only has two-of-a-kind, they may re-throw the remaining dice in an \n\t attempt to improve the matching dice values. If no matching numbers are\n\t rolled, a player scores 0. The first player to reach 25 points wins. ");
Console.WriteLine("\nScoring: 3-Of-A-Kind = 3 Points \n 4-Of-A-Kind = 6 Points \n 5-Of-A-Kind = 12 Points\n");
Random RandomNum = new Random();
Player[] player1 = new Player[5];
Die[] myDie = new Die[5];
for (int i = 0; i < 5; i++)
{
myDie[i] = new Dice_v4.Die(RandomNum);
player1[i] = new Dice_v4.Player();
}
for (int i = 0; i < 2; i++) // Number of players
{
Console.Write("Enter Name for Player {0}:", i + 1);
string NewName = Console.ReadLine();
player1[i].SetName(NewName);
}
Console.WriteLine("\nPress enter in turns to roll the five dice");
Console.ReadLine();
Console.WriteLine();
for (int j = 1; j < 50; j++)
{
for (int i = 0; i < 2; i++)
{
myDie[i].roll();
Console.WriteLine("{0} Rolled:{1} on the first dice", player1[i].GetName(), myDie[i].GetTopNumber());
Console.WriteLine("{0} Rolled:{1} on the second dice", player1[i].GetName(), myDie[i].GetTopNumber1());
Console.WriteLine("{0} Rolled:{1} on the third dice", player1[i].GetName(), myDie[i].GetTopNumber2());
Console.WriteLine("{0} Rolled:{1} on the fourth dice", player1[i].GetName(), myDie[i].GetTopNumber3());
Console.WriteLine("{0} Rolled:{1} on the fifth dice", player1[i].GetName(), myDie[i].GetTopNumber4());
myDie[i].points();
Console.WriteLine("\t\t\t\t\tTotal Throws:{0}\n ------------------------------------------------------", j);
myDie[i].Totally();
Console.ReadLine();
}
}
}
}
积分:
class Die
{
private int NumberTop1; //attributes
private int NumberTop2;
private int NumberTop3;
private int NumberTop4;
private int NumberTop5;
int threepoints = 0;
int sixpoints = 0;
int twelvepoints = 0;
int TotalPoints = 0;
private Random RandomNumGenerator;
public Die(Random RandomGenerator) // constructor
{
RandomNumGenerator = RandomGenerator; // initialises random number
}
public int GetTopNumber()
{
return NumberTop1; // Returns number on top
}
public int GetTopNumber1()
{
return NumberTop2;
}
public int GetTopNumber2()
{
return NumberTop3;
}
public int GetTopNumber3()
{
return NumberTop4;
}
public int GetTopNumber4()
{
return NumberTop5;
}
public void roll()
{
NumberTop1 = RandomNumGenerator.Next(1, 7);
NumberTop2 = RandomNumGenerator.Next(1, 7);
NumberTop3 = RandomNumGenerator.Next(1, 7);
NumberTop4 = RandomNumGenerator.Next(1, 7);
NumberTop5 = RandomNumGenerator.Next(1, 7);
List<int> diceValues = new List<int>();
diceValues.Add(GetTopNumber());
diceValues.Add(GetTopNumber1());
diceValues.Add(GetTopNumber2());
diceValues.Add(GetTopNumber3());
diceValues.Add(GetTopNumber4());
var duplicates = diceValues
.GroupBy(i => i)
.Where(g => g.Count() == 3)
.Select(g => g.Key);
foreach (var d in duplicates)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("\n{0} Appeared three times --- 3 Points Awarded\n", d);
Console.BackgroundColor = ConsoleColor.Black; threepoints += 3;
}
var fourting = diceValues
.GroupBy(i => i)
.Where(g => g.Count() == 4)
.Select(g => g.Key);
foreach (var e in fourting)
{
Console.WriteLine("\n{0} Appeared four times --- 6 Points Awarded\n", e);
sixpoints += 6;
}
var fiveting = diceValues
.GroupBy(i => i)
.Where(g => g.Count() == 5)
.Select(g => g.Key);
foreach (var f in fiveting)
{
Console.WriteLine("\n{0} Appeared five times --- 12 Points Awarded\n", f);
twelvepoints += 12;
}
}
public string points()
{
TotalPoints = threepoints + sixpoints + twelvepoints;
Console.WriteLine("\n\t\t\t\t\tTotal Score: {0}", TotalPoints);
return pointss;
}
public string Totally()
{
if (TotalPoints >= 25)
{
Console.WriteLine("This Player Won the game");
}
return tots;
}
}
【问题讨论】:
-
在 Dice 类中从
Totally返回之前,您为变量tots设置的值是多少?您如何总结 Dice 类中TotalPoints中的值? -
据我所知没有。我只是在玩弄它以使其正常工作
-
如果您不清楚逻辑,那么很难回答这个问题。即使我们回答了这个问题,如果你无法理解它的逻辑,也没有意义。你能分享整个骰子课吗?
-
您的 Die 类没有意义,并且您没有显示程序中使用的类(例如
Dice_v4.Die)。骰子不应该知道任何关于玩家或“总分”的信息。如果您考虑该对象,它有两个主要特征:Roll()的能力和FaceValue属性。我建议在编写更多内容之前重新审视您的对象设计并为代码绘制工作流程图。 -
更新了骰子类。
标签: c# if-statement dice