我正在做一个骰子游戏任务,我正在重新掷骰子,希望在6个骰子中找到3种或4种类型等。但是如果你滚动两种,我想保持这些值并重新滚动以找到3,4,5种。我正在努力弄清楚如何让程序保持价值,同时重新滚动其他人,而不是完全重新推动一切。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class die
{
// attributes
private int numberOnTop;
private Random randomNumberGenerator;
// methods
// roll the die, get the number currently showing on top
public die(Random randomGenerator)
{
//initialise the random number generator
randomNumberGenerator = randomGenerator;
}
public void roll()
{
//create a random number from 1 to 6 and store it in the numberOnTop attribute
numberOnTop = randomNumberGenerator.Next(1, 7);
}
public int getNumberOnTop()
{
// a "get" method to get the value of the number currently on top and return the value to where the call was made
return numberOnTop;
}
}
class diceGame
{
//main program
public static void Main(String[] args)
{
Int32 UserSelect;
int playerone = 0;
int playertwo = 0;
int playeroneturn = 0;
Console.WriteLine("Welcome to my two person dice game. Would you like to play \r\n 1. Person vs Person \r\n 2. Person vs AI");
UserSelect = Convert.ToInt32(Console.ReadLine());
// A switch will select which option that the user input using the Value Selected variable
switch (UserSelect)
{
case 1:
do
{
Console.WriteLine("This is players 1's turn");
//create a random number generator to pass to the die objects
Random myRandomiser = new Random();
die[] myDie = new die[6];
//instantiate the three dice (the constructor is called)
for (int i = 0; i < 6; i++)
{
myDie[i] = new die(myRandomiser);
}
for (int i = 0; i < 6; i++)
{
myDie[i].roll();
switch (myDie[i].getNumberOnTop())
{
case 1:
Console.WriteLine(" ---------\r\n | |\r\n | O |\r\n | |\r\n ---------");
break;
case 2:
Console.WriteLine(" ---------\r\n | O |\r\n | |\r\n | O |\r\n ---------");
break;
case 3:
Console.WriteLine(" ---------\r\n | O |\r\n | O |\r\n | O |\r\n ---------");
break;
case 4:
Console.WriteLine(" ---------\r\n | O O |\r\n | |\r\n | O O |\r\n ---------");
break;
case 5:
Console.WriteLine(" ---------\r\n | O O |\r\n | O |\r\n | O O |\r\n ---------");
break;
case 6:
Console.WriteLine(" ---------\r\n | O O |\r\n | O O |\r\n | O O |\r\n ---------");
break;
}
}
// two of a kind check goes here and therefore the rerolls will happen here
var twoofakind = new[] { myDie[0].getNumberOnTop(), myDie[1].getNumberOnTop(), myDie[2].getNumberOnTop(), myDie[3].getNumberOnTop(), myDie[4].getNumberOnTop(), myDie[5].getNumberOnTop() };
var isLargeGroupTwo = twoofakind.GroupBy(n => n, (key, group) => group.Count())
.Any(count => count >= 2);
if (isLargeGroupTwo == true) {
Console.WriteLine("Please reroll to try and get a 3, 4, or 5 of a kind");
if (myDie[0].getNumberOnTop() == myDie[1].getNumberOnTop())
{
int i = 2;
myDie[i].roll();
}
}
// ends here
var threeofakind = new[] { myDie[0].getNumberOnTop(), myDie[1].getNumberOnTop(), myDie[2].getNumberOnTop(), myDie[3].getNumberOnTop(), myDie[4].getNumberOnTop(), myDie[5].getNumberOnTop() };
//three of a kind check
var isLargeGroupThree = threeofakind.GroupBy(n => n, (key, group) => group.Count())
.Any(count => count >= 3);
Console.WriteLine("Is there any 3 of a kind's in the roll?");
Console.WriteLine(isLargeGroupThree);
if (isLargeGroupThree == true)
{
playerone = playerone + 3;
Console.WriteLine("Player one has scored a 3 of a kind therefore earning 3 points.");
Console.WriteLine(playerone);
}
else if (isLargeGroupThree == false)
{
var fourofakind = new[] { myDie[0].getNumberOnTop(), myDie[1].getNumberOnTop(), myDie[2].getNumberOnTop(), myDie[3].getNumberOnTop(), myDie[4].getNumberOnTop(), myDie[5].getNumberOnTop() };
//four of a kind check
var isLargeGroupFour = fourofakind.GroupBy(n => n, (key, group) => group.Count())
.Any(count => count >= 4);
Console.WriteLine("Is there any 4 of a kind's in the roll?");
Console.WriteLine(isLargeGroupFour);
if (isLargeGroupFour == true)
{
playerone = playerone + 6;
Console.WriteLine("Player one has scored a 4 of a kind therefore earning 6 points.");
Console.WriteLine("Player one's total score is" + playerone);
}
else if (isLargeGroupFour == false)
{
var fiveofakind = new[] { myDie[0].getNumberOnTop(), myDie[1].getNumberOnTop(), myDie[2].getNumberOnTop(), myDie[3].getNumberOnTop(), myDie[4].getNumberOnTop(), myDie[5].getNumberOnTop() };
//four of a kind check
var isLargeGroupFive = fiveofakind.GroupBy(n => n, (key, group) => group.Count())
.Any(count => count >= 5);
Console.WriteLine("Is there any 5 of a kind's in the roll?");
Console.WriteLine(isLargeGroupFive);
if (isLargeGroupFive == true)
{
playerone = playerone + 12;
Console.WriteLine("Player one has scored a 5 of a kind therefore earning 12 points.");
Console.WriteLine("Player two's total score is" + playerone);
}
}
}
Console.ReadLine();
} while (playerone < 50);
Console.WriteLine("Player one has reached 50 points in " + playeroneturn + "turns");
break;
}
}
}
我会继续努力,但对此的任何帮助都会非常棒:)
答案 0 :(得分:0)
在你的两张支票中:
geom_line
这可能有用。