using GameDemo.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GameDemo
{
class Program
{
static void Main(string[] args)
{
int total=0;//计时
Console.WriteLine("开始游戏");
Console.WriteLine("准备好开始游戏吗?y/n?");
if (Console.ReadLine().Equals("n")) {
Console.WriteLine("游戏已退出!");
return;
}
Console.WriteLine("请输入关卡数量");
int gk = Int32.Parse(Console.ReadLine());
Console.WriteLine("请输入每个关卡输入的次数");
int count = Int32.Parse(Console.ReadLine());
Console.WriteLine("请输入闯关输入的字数的个数");
int size = Int32.Parse(Console.ReadLine());
for (int i = 0; i <gk; i++)
{
for (int j = 0; j <count; j++)
{
Console.WriteLine("这是第"+(i+1)+"关"+"第"+(j+1)+"次");
//产生随机字母
string str = new RandomUtils().CreateRandomWord(size);
Console.WriteLine("你要输入的内容为:"+str);
//时间计算
DateTime start = DateTime.Now;
//等待用户输入
string userinput=Console.ReadLine();
DateTime end = DateTime.Now;
int t= (int)(end.Ticks - start.Ticks)/10000000;//单次计时
total += t;//总计时
//检查用户输入是否正确
if (userinput.Equals(str))
{
Console.WriteLine("恭喜,你输入对了!用时"+t+"秒");
}
else {
Console.WriteLine("抱歉,你输入错了,游戏结束!");
return;
}
}
if (i == gk-1) {//闯完所有关卡
Console.WriteLine("恭喜你全部过关,总用时为"+total+"秒");
return;
}
Console.WriteLine("准备好进入下一关了吗 y/n");
string comd = Console.ReadLine();
if (comd.Equals("n")) {
Console.WriteLine("游戏已退出!");
return;
}
}
}
}
}
//生产字符串的工具类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GameDemo.Utils
{
class RandomUtils
{
/// <summary>
/// 用来装载字符的数组
/// </summary>
private char[] chars = new char[50];
/// <summary>
/// 初始化数组数据
/// </summary>
public RandomUtils() {
//得到a-z的字符
int idx=0;
for (int i = 'a'; i <'z'+1; i++)
{
if (i == 'o') {//去掉o字母
continue;
}
chars[idx] += (char)i;
idx++;
}
//得到1-9的字符
int idx2=idx;
for (int j ='0'; j <'9'+1; j++)
{
chars[idx2++] = (char)j;
}
//重新组装数据
char[] newchars = new char[idx2];
for (int m = 0; m <idx2; m++)
{
if (chars[m] == 'l') {//将小写的l换成L
chars[m] = 'L';
}
newchars[m] = chars[m];
}
//将重组后的新数组赋值给原来的数组便于给其他方法访问数组数据
chars = newchars;
}
/// <summary>
/// 随机产生字符串
/// </summary>
/// <param name="size">产生的字符串个数</param>
/// <returns></returns>
public string CreateRandomWord(int size) {
StringBuilder builder = new StringBuilder();
Random r = new Random();
for (int i = 0; i <size; i++)
{
char c = chars[r.Next(chars.Length)];
if (builder.ToString().Contains(c)) {//处理字符串重复出现
i--;
continue;
}
builder.Append(c);
}
return builder.ToString();
}
}
}