c#数字猜谜游戏:获取不同的随机数和不同的用户输入
本文关键字:随机数 输入 用户 获取 数字 游戏 | 更新日期: 2023-09-27 17:50:45
我正在构建一个数字猜谜游戏,它从控制台读取4位数。计算机随机生成4位数,用户尝试猜测数字及其正确顺序。程序以0A0B或1A3B或4A0B等格式返回答案,如果您猜对的数字正确并且它的位置也正确,则返回a,如果您猜对的数字正确但它的位置错误,则返回B。
-
我如何确保计算机生成的数字是4个不同的随机数?这可能吗?试图控制随机数的结果是否会破坏随机数的全部目的
-
我如何确保用户输入四个不同的数字。例如,确保用户不能重复一个数字,例如用户不能输入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);
}
}
}
1:您保留生成的随机数列表并生成新数字,直到您有4个不同的数字。
2:保存一个已输入的数字列表,并告诉用户他输入了一个他已经输入过的数字。
这正是一本像c#这样的书在21天内所涵盖的基本内容。
3:如果你想在score为0后结束,是的,这是一个很好的循环。
不同随机数生成
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)){...}