【发布时间】:2014-03-17 09:49:31
【问题描述】:
我正在构建一个数字猜谜游戏,它从控制台读取一个 4 位数字。计算机随机生成 4 位数字,用户尝试猜测数字及其正确顺序。程序以 0A0B 或 1A3B 或 4A0B 等格式返回答案,如果您猜的数字正确且位置也正确,则返回 A,如果您猜的数字正确但位置错误,则返回 B。
如何确保计算机生成的数字是 4 个不同的随机数?这还可能吗。并且试图控制随机数的结果会破坏随机数的全部目的
如何确保用户输入了四个 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);
}
}
}
【问题讨论】:
-
这听起来很像“Master Mind”。 :-)