c#實現簡單金山打字小游戲(源碼)


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();
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM