福彩有一种玩法很有意思,叫‘快乐8“;因为它五分钟就开一次奖;具体玩法是:有1-80个号码;每次摇出20个号码(1-80里的号码,不相同);你有8种玩法;最少可以选一个号码,最多选8个号码;当然每种玩法对应的奖金是不同的。(我就中拉一次8中7,只有700)。
玩过之后就想写个程序来实现它,当然只是实现它的玩法,具体它的算法我是不可能知道的(顺便也巩固一下.net的基础知识)
思路:尽量分层,不同的功能用不同的函数实现;拆分为多个类,使程序结构明了;易于以后修改和维护;
首先我要有一个配置文件来配置游戏的多长时间开奖;和每次开多少个球;(当然,快乐8是五分钟开一次,每次开20个号码;我加配置文件是为了以后能修改玩法);
App.config文件
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Interval" value="60"/>
<add key="ListCounts" value="20"/>
</appSettings>
</configuration>
还有一个GameManager来管理游戏的运行;
GameManager文件:
GameManager
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Threading;
namespace Studying
{
/// <summary>
/// a class Gamemanger
/// </summary>
public class Gamemanger
{
private static int number1 = 10000;
private static int number2 = 10000;
private static string logfile = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";
public int Number1
{
get{return number1;}
set { number1 = value; }
}
public int Number2
{
get { return number2; }
set { number2 = value; }
}
/// <summary>
/// start game in console
/// </summary>
public void GetNewGame()
{
bool isGame = true;
string count = ConfigurationManager.AppSettings["ListCounts"];
if (String.IsNullOrEmpty(count))
{
count = "20";
}
int counts = int.Parse(count);
Console.WriteLine("Game will be start. Now is:{0}", DateTime.Now);
while (isGame)
{
IList<int> list = new List<int>();
Random rd = new Random();
while (list.Count < counts)
{
int num = rd.Next(1, 81);
if (!list.Contains(num))
{
list.Add(num);
}
}
WriteDataToTxt(list);
int[] arrInt = new int[85];
int arrIndex;
foreach (int value in list)
{
arrIndex = value;
arrInt[arrIndex] = value;
}
Console.WriteLine("The Game Number is {0} Game start at {1}", number1++, DateTime.Now);
Console.WriteLine(new string('*', 80));
//Console.WriteLine("\tone\ttwo\tthree\tfour\tfive\tsix\tsevcen\teight\tnine\tten");
for (int i = 0; i < 8; i++)
{
Console.WriteLine("\t");
for (int j = 0; j < 10; j++)
{
if (arrInt[10 * i + j + 1] == (10 * i + j + 1))
{
Console.Write("{0}\t", arrInt[10 * i + j + 1]);
}
else
{
Console.Write("{0}\t", "");
}
}
}
Console.WriteLine(new string('*', 80));
Console.WriteLine("Game will start after 60 seconds Now is:{0}", DateTime.Now);
string intervalstr = ConfigurationManager.AppSettings["Interval"];
if (String.IsNullOrEmpty(intervalstr))
{
intervalstr = "60";
}
long interval = long.Parse(intervalstr);
Thread.Sleep(TimeSpan.FromSeconds(interval));
}
}
/// <summary>
/// Define when start Game and end game
/// </summary>
public void StartGame()
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day + 1, 0, 0, 0);
Thread mythread = new Thread(new ThreadStart(GetNewGame));
if (now >= startTime && now <= endTime)
{
mythread.Start();
}
else
{
mythread.Abort();
Console.WriteLine("Time is {0},Which is Not a suit time for Game!", DateTime.Now);
}
}
/// <summary>
/// Write GameData to a Text File;
/// </summary>
/// <param name="list">GameData as list</param>
public void WriteDataToTxt(IList<int> list)
{
int dataIndex = 1;
if (list.Count > 0)
{
StreamWriter sw = File.AppendText(logfile);
try
{
sw.WriteLine("Game Number {0} Game start at Time:{1}", number2++, DateTime.Now);
sw.WriteLine(new string('*', 80));
foreach (int data in list)
{
sw.WriteLine("{0}:{1}", dataIndex++, data);
}
sw.WriteLine(new string('*', 80));
sw.Close();
}
catch (IOException ex)
{
sw.WriteLine(ex.Message);
}
}
}
}
}
最后在主函数中调用GameManager类中方法就可以拉。
Main()
Gamemanger newGame = new Gamemanger();
Thread mythread = new Thread(new ThreadStart(newGame.StartGame));
mythread.Start();
Console.ReadLine();
基本上十分简单;巩固一下 .net的基础知识。
抛砖引玉,大家可以丰富一下它。