我已经被设置了一个任务,这意味着我需要创建一个'3个或更多的骰子游戏‘。我所坚持的是这个游戏所需要的计分系统,它是这样的:“玩家依次掷出所有五个骰子,并为同类中的三个或更好的骰子得分。如果玩家只有同类中的两个,他们可能会重新掷出剩余的骰子,试图提高匹配的骰子值。如果没有匹配的数字被掷出,玩家得分为0。
游戏进行了一定数量的回合(比如50回合),游戏结束时总分最高的玩家是获胜者。“我需要计算出如何将随机骰子的数字相加,看看哪些数字是匹配的。
namespace DiceGame
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, I am your computer and I am speaking to you, welcome to the dice game, here are the rules:");
Console.WriteLine("3-of-a-kind: 3 points ");
Console.WriteLine("4-of-a-kind: 6 points ");
Console.WriteLine("5-of-a-kind: 12 points");
Console.WriteLine("First player to reach 50 wins");
//integer array to store 5 values for the dice
int[] Rolls = new int[5];
//Calls from class Dice (calls integer value)
for (int numdice = 0; numdice < 5; numdice++)
{
Rolls[numdice] = Dice.Roll();
//For each loop, get position of array and place number we want in that position
Console.WriteLine(Rolls[numdice]);
}
//Makes a new game object which is used to call functions
Game game = new Game();
game.startGame();
Console.ReadLine();
}
//produces one random number
public class Dice
{
static Random rng = new Random();
public static int Roll()
{
//create a random number generator
return rng.Next(1, 7);
}
}
public class Game
{
public void startGame()
{
//prompts user to input value and then stores it
int playerNo = numberofPlayers();
while (playerNo < 2)
{
Console.WriteLine("Please enter a number between 2-4");
playerNo = numberofPlayers();
}
//Now have number of players, need to loop through now
//creates the number of players in array
player[] listofPlayers = new player[playerNo];
//this looks at the current player that the code is looking at
for (int currentPlayer = 0; currentPlayer < playerNo; currentPlayer++)
{
listofPlayers[currentPlayer] = new player();
Console.WriteLine("It is player {0}'s turn", currentPlayer + 1);
listofPlayers[currentPlayer].rollplayerDice();
Console.WriteLine(listofPlayers[currentPlayer].score);
listofPlayers[currentPlayer].playersScore();
}
}
void inputPlayers()
{
//create number of players code
//create a way to input name of players
//depending on the number of players, repeat the code below that many times
string player1 = Console.ReadLine();
}
public int numberofPlayers()
{
int playerNum = 0;
try
{
Console.WriteLine("Please Enter the number of players you would like to play with 2-4");
playerNum = int.Parse(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return playerNum;
//if playerNo is equal to 2 = 2 players
//if playerNO is equal to 3 = 3 players
//if playerNO is equal to 4 = 4 players
//if playerNo is less than 2 and more than 4 then loop back through the if statement and ask to input and number "between 2-4"
}
public class player
{
public int score = 0;
int[] playerRoll = new int[5];
public void rollplayerDice()
{
for (int currentDice = 0; currentDice < playerRoll.Length; currentDice++)
{
playerRoll[currentDice] = Dice.Roll();
Console.WriteLine("Dice {0} rolled a {1}", currentDice, playerRoll[currentDice]);
}
}
public int playersScore()
{
int[] diceFaces = new int[6];
/*for (int diceFace = 0; diceFace < playerRoll.Length; diceFace++)
{
int oneCounter = 0;
//number of 1's =
//number of 2's =
//number of 3's =
//number of 4's =
//number of 5's =
//number of 6's =
//create a system to work out the score
//create a switch to see what the player score is equal to (switch 3, 4, 5 and add up the points that correlate)
}
*/
foreach (int d in playerRoll)
diceFaces[d]++;
int caseSwitch = 0;
switch (caseSwitch)
{
case 3:
//add on the points to a players score
score += 3;
break;
case 4:
//add on the points to a player score
break;
case 5:
//add on the points of a players score
break;
}
return 0;
}
}
}
}
}
^上面是我的全部代码,下面是我现在正在尝试得分的代码。
public int playersScore()
{
int[] diceFaces = new int[6];
/*for (int diceFace = 0; diceFace <
playerRoll.Length; diceFace++)
{
int oneCounter = 0;
//number of 1's =
//number of 2's =
//number of 3's =
//number of 4's =
//number of 5's =
//number of 6's =
//create a system to work out the score
//create a switch to see what the player score is equal to (switch 3, 4, 5 and add up the points that correlate)
}
*/
foreach (int d in playerRoll)
diceFaces[d]++;
int caseSwitch = 0;
switch (caseSwitch)
{
case 3:
//add on the points to a players score
score += 3;
break;
case 4:
//add on the points to a player score
break;
case 5:
//add on the points of a players score
break;
}
return 0;
}
您可以使用groupby()
,然后使用count()
:
int score = 0;
var grouped = dice.GroupBy(x => x);
//Score 3 of a kinds
score += grouped.Count(x => x.Count() == 3) * 3;
//Score 4 of a kinds
score += grouped.Count(x => x.Count() == 4) * 6;
//Score 5 of a kinds
score += grouped.Count(x => x.Count() == 5) * 12;
工作小提琴
我有一个任务,它获取一个int值“n”和一个Int Array作为参数,并且应该返回一个布尔值。该方法应该确定给定数组中有多少个“n”。如果数字是偶数,则方法应该返回true,否则返回false。如果数组的长度为0,它也应该返回“false”。 我设法做到的是: 老实说,我真的很困惑,我不知道该怎么办。我真的已经尽力了,但是我在这项任务上工作的时间越长,我就越不理解。感谢任何帮助,并提前感谢您!:
问题内容: 对于SQL数据库中的用户日志记录表,我跟踪报告请求中的某些参数。该报告允许将多个ID传递给它,我将所有ID都存储在数据库列的单个列中。如果这是一组标准化的数据,则肯定会有一个附加的表设置,但这就是继承的内容。。。 现在,我被要求提供一个传递了2个以上ID的报告的运行次数的快速计数。我可以轻松获取请求的报告数量超过1个的记录的数量,因为它们都包含逗号。 接下来需要做的是计算逗号在列中出现
问题内容: 我有如下的对象数组: 我想计算不同值的出现次数,例如: 假设有3次出现,并且有2次出现。 我正在做。我能够像使用此答案那样获得不同的价值。 首选ES6语法。 问题答案: 您将需要知道计数属于哪个名称,所以我建议不要输出不提供任何线索的数组,而要输出一个以名称为键并以值作为对应值的对象:
问题内容: 我已经看到了一些这样的示例,但是所有这些似乎都依赖于知道要计算发生次数的元素。我的数组是动态生成的,所以我无法知道要计算哪个元素的出现(我想计算所有元素的出现)。有人可以建议吗? 提前致谢 编辑: 也许我应该更清楚一点,数组将包含多个不同的字符串(例如 在不知道它们是什么的情况下,如何计算foo,bar和foobar的出现? 问题答案: Swift 3和Swift 2: 您可以使用类型
计算数组中值的出现次数。 每次遇到数组中的某个特定值时,使用 Array.reduce() 来递增计数器。 const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a + 0), 0); countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3
问题内容: 我有一个数组如下 预期结果 尝试如下 问题答案: 无需使用jQuery即可完成此任务-此示例将构建一个对象,其中包含数组中每个不同元素的出现次数