我正在构建一个数字猜测游戏,它从控制台读取一个4位数字。计算机随机生成4位数字,用户试图猜测数字及其正确的顺序。程序以0A0B或1A3B或4A0B等格式返回答案,如果您猜对的数字正确且它的位置也是正确的,则返回A,如果您猜到的数字正确但位置错误,则返回B. / p>
我如何确保计算机生成的数字是4个不同的随机数?这甚至是可能的。并试图控制RANDOM数字的结果,打败RANDOM数字的整个目的
如何确保用户输入四个DISTINCT数字。即,确保用户不能重复例如用户不应该输入1031或9838.我已经搜索了谷歌并在21天内浏览了C#,但我找不到合适的答案。
评分函数+限制机会数。我知道我可以实现一个简单的for循环来运行我的程序,而得分不为零。一旦程序降至零,游戏就会终止。
的内容for (int score = 100; score > 0; score = score - 5) // gives a user 20 tries
{
//run program
Console.WriteLine("Your score is {0}", score);
}
这是否正确实施?有没有人知道更好的方法呢?
我的游戏的完整源代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GuessNumber
{
public struct answer
{
public int A;
public int B;
}
public class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("********************************");
Console.WriteLine("What number do you think it is ?");
Console.WriteLine("Type 'Instrustions' or 'help' to get the game rules ?");
Console.WriteLine("********************************");
Console.WriteLine();
bool GameOver = false;
int[] targetNumber = GenerateRandomNumber();
int hintNumber = 0;
Console.WriteLine(ConvertIntArrayToString(targetNumber));
while (!GameOver)
{
Console.Write("Answer: ");
string answer = Console.ReadLine();
if (answer.ToLower() == "i give up" || answer.ToLower() == "exit" || answer.ToLower() == "quit")
{
Console.WriteLine(ConvertIntArrayToString(targetNumber));
GameOver = true;
break;
}
if (answer.ToLower() == "hint" ) // Designed to Provide a hint.
{
if (hintNumber < 2) // Check that a user has only used 2 hints
{
int Rando = GenerateRandomNumberforHint();
// Console.WriteLine("The Random Number generated was {0}",Rando);
if (Rando == 0)
{
Console.WriteLine("{0}XXX", targetNumber[Rando]); // If Rando is 0, it takes the first position of the array
}
else if (Rando == 1)
{
Console.WriteLine("X{0}XX", targetNumber[Rando]); // If Rando is 1, it takes the second position of the array
}
else if (Rando == 2)
{
Console.WriteLine("XX{0}X", targetNumber[Rando]);
}
else if (Rando == 3)
{
Console.WriteLine("XXX{0}", targetNumber[Rando]);
}
}
else
{
Console.WriteLine("You've already used up your two hints");
}
hintNumber++;
continue;
}
if (answer.ToLower() == "instructions" || answer.ToLower() == "help")
{
Console.WriteLine("Instructions are as follows...");
continue;
}
if (answer.Length != 4)
{
continue;
}
answer currentAnswer = CompareNumbers(targetNumber, ConvertStringToIntArray(answer));
Console.WriteLine(currentAnswer.A + "A" + currentAnswer.B + "B");
if (currentAnswer.A == 4)
GameOver = true;
}
Console.WriteLine("Game Over!");
Console.ReadLine();
}
}
protected static int GenerateRandomNumberforHint()
{
Random rnd = new Random();
int Random = rnd.Next(0, 4);
return Random;
}
protected static int[] GenerateRandomNumber()
{
int[] RandomNumber = new int[4];
Random seed = new Random();
int i = 0;
while (i < 4)
{
// RandomNumber[i] = (seed.Next() % 10).ToString()[0];
RandomNumber[i]=seed.Next(0, 10);
int j = i - 1;
while (j >= 0)
{
if (RandomNumber[i] == RandomNumber[j])
i--;
j--;
}
i++;
}
return RandomNumber;
}
protected static answer CompareNumbers(int[] TargetNumber, int[] CompareNumber)
{
answer ReturnAnswer;
ReturnAnswer.A = 0;
ReturnAnswer.B = 0;
for (int i = 0; i < 4; i++)
{
if (TargetNumber[i] == CompareNumber[i])
ReturnAnswer.A++;
for (int j = 0; j < 4; j++)
{
if (TargetNumber[i] == CompareNumber[j] && i != j)
ReturnAnswer.B++;
}
}
return ReturnAnswer;
}
protected static int[] ConvertStringToIntArray(string String)
{
int[] IntValue = new int[String.Length];
for (int i = 0; i < String.Length; i++)
{
try
{
IntValue[i] = Convert.ToInt32(String[i].ToString());
}
catch (Exception ex)
{
return null;
// Console.WriteLine("You must input number");
}
}
return IntValue;
}
protected static string ConvertIntArrayToString(int[] IntArray)
{
char[] CharArray = new char[IntArray.Length];
for (int i = 0; i < CharArray.Length; i++)
{
CharArray[i] = Convert.ToChar(IntArray[i].ToString());
}
return new string(CharArray);
}
}
}
答案 0 :(得分:1)
1:你保留一个生成的随机数列表并生成新的数字,直到你有4个不同的数字。
2:你保留一个输入数字列表并告诉用户他输入的数字是否已输入。
这正是21天内像C#这样的书确实涵盖的基准内容。如果你完成了它。
3:如果你想在得分为0之后结束,是的,这是一个很好的循环。
答案 1 :(得分:-1)
用于生成独特的随机数
int GenerateRandomNumber()
{
Random random = new Random();
int randomNum = random.Next(1000, 9999);
return randomNum;
}
//Make sure numbers are distinct
int GenerateDistinctRandomNumber()
{
int randomNum = 0;
var hashSet = new HashSet<int>();
int randomNumForLoop = GenerateRandomNumber();
int[] testArray = GetIntArray(randomNumForLoop);
for (int i = 0; i<4; i++)
{
if (testArray.GroupBy(x => x).Any(g => g.Count() > 1))
{
randomNumForLoop = GenerateRandomNumber();
testArray = GetIntArray(randomNumForLoop);
i = 0;
}
randomNum = randomNumForLoop;
}
return randomNum;
}
//Convert int number to an array.
int[] GetIntArray(int num)
{
List<int> tempList = new List<int>();
while (num > 0)
{
tempList.Add(num % 10);
num = num / 10;
}
tempList.Reverse();
return tempList.ToArray();
}
关键部分在这里,检查是否重复:
if (testArray.GroupBy(x => x).Any(g => g.Count() > 1)){...}