向各位大哥大姐求一点C#编写的小游戏源代码!!(最好新颖一点的)
感谢大家的关注,不过有一点小小的问题,这源代码怎么不能运行啊?usingSystem.Linq;这一句是不是少什么东西啊?...
感谢大家的关注,不过有一点小小的问题,这源代码怎么不能运行啊?using System.Linq;这一句是不是少什么东西啊?
展开
2个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace NumberPuzzle
{
class Program
{
/// <summary>
/// Num Puzzle
/// ^^**
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string numPazzle = string.Empty;
string numLength = string.Empty;
int count = 0;
int countMax = 0;
Console.WriteLine("How long do you want?[0<n<11] \"Suggestion : 4\"");
while (true)
{
numLength = Console.ReadLine();
if (IsNum(numLength))
{
countMax = Convert.ToInt32(numLength);
if (countMax > 10)
{
Console.WriteLine("Re-inpt due to n>10");
continue;
}
break;
}
else
{
Console.WriteLine("Re-inpt, input is not a num:");
continue;
}
}
while (count < countMax)
{
string strA = GetNum();
if (numPazzle.IndexOf(strA) != -1)
{
continue;
}
numPazzle += strA;
count++;
}
while (true)
{
string input = string.Empty;
string results = string.Empty;
Console.WriteLine("Input what you guess:");
input = Console.ReadLine();
if (!IsNum(input))
{
Console.WriteLine("Re-inpt, input is not a num:");
continue;
}
if (input.Length != countMax)
{
Console.WriteLine("The length of input is error");
continue;
}
if (IsDup(input))
{
Console.WriteLine("Input is a dup num");
continue;
}
results = CompareNum(input, numPazzle);
if (results.Split('-')[0].Equals(numPazzle.Length.ToString()))
break;
Console.WriteLine("Results: A-{0} B-{1}", results.Split('-')[0], results.Split('-')[1]);
}
Console.WriteLine("Win! The num is {0}", numPazzle);
Console.ReadKey();
}
public static string GetNum()
{
Random sSeed = new Random();
Random seed = new Random(sSeed.Next());
return seed.Next(10).ToString();
}
public static string CompareNum(string actualStr, string expectedStr)
{
int a = 0;
int b = 0;
string results = string.Empty;
for (int i = 0; i < actualStr.Length; i++)
{
if (expectedStr.IndexOf(actualStr[i]) != -1)
{
b++;
}
if (expectedStr[i].Equals(actualStr[i]))
{
a++;
b--;
}
}
results = a.ToString() + "-" + b.ToString();
return results;
}
public static bool IsDup(string input)
{
bool result = false;
foreach (char aStr in input)
{
if (input.IndexOf(aStr) != input.LastIndexOf(aStr))
{
result = true;
break;
}
}
return result;
}
public static bool IsNum(string numInput)
{
bool result = false;
Regex reg = new Regex(@"^-?\d+$");
result = reg.IsMatch(numInput);
return result;
}
}
}
CMD 猜数字
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace NumberPuzzle
{
class Program
{
/// <summary>
/// Num Puzzle
/// ^^**
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string numPazzle = string.Empty;
string numLength = string.Empty;
int count = 0;
int countMax = 0;
Console.WriteLine("How long do you want?[0<n<11] \"Suggestion : 4\"");
while (true)
{
numLength = Console.ReadLine();
if (IsNum(numLength))
{
countMax = Convert.ToInt32(numLength);
if (countMax > 10)
{
Console.WriteLine("Re-inpt due to n>10");
continue;
}
break;
}
else
{
Console.WriteLine("Re-inpt, input is not a num:");
continue;
}
}
while (count < countMax)
{
string strA = GetNum();
if (numPazzle.IndexOf(strA) != -1)
{
continue;
}
numPazzle += strA;
count++;
}
while (true)
{
string input = string.Empty;
string results = string.Empty;
Console.WriteLine("Input what you guess:");
input = Console.ReadLine();
if (!IsNum(input))
{
Console.WriteLine("Re-inpt, input is not a num:");
continue;
}
if (input.Length != countMax)
{
Console.WriteLine("The length of input is error");
continue;
}
if (IsDup(input))
{
Console.WriteLine("Input is a dup num");
continue;
}
results = CompareNum(input, numPazzle);
if (results.Split('-')[0].Equals(numPazzle.Length.ToString()))
break;
Console.WriteLine("Results: A-{0} B-{1}", results.Split('-')[0], results.Split('-')[1]);
}
Console.WriteLine("Win! The num is {0}", numPazzle);
Console.ReadKey();
}
public static string GetNum()
{
Random sSeed = new Random();
Random seed = new Random(sSeed.Next());
return seed.Next(10).ToString();
}
public static string CompareNum(string actualStr, string expectedStr)
{
int a = 0;
int b = 0;
string results = string.Empty;
for (int i = 0; i < actualStr.Length; i++)
{
if (expectedStr.IndexOf(actualStr[i]) != -1)
{
b++;
}
if (expectedStr[i].Equals(actualStr[i]))
{
a++;
b--;
}
}
results = a.ToString() + "-" + b.ToString();
return results;
}
public static bool IsDup(string input)
{
bool result = false;
foreach (char aStr in input)
{
if (input.IndexOf(aStr) != input.LastIndexOf(aStr))
{
result = true;
break;
}
}
return result;
}
public static bool IsNum(string numInput)
{
bool result = false;
Regex reg = new Regex(@"^-?\d+$");
result = reg.IsMatch(numInput);
return result;
}
}
}
CMD 猜数字
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询