[Code隨手寫] 樂透對獎程式兼新年恭賀

適逢虎年除夕,即將邁入兔年的前一刻,台彩宣布百萬中獎的100個號碼,但因為一張一張對真的會眼花,索性花了十來分鐘撰寫了一支小程式,由電腦去做這件事,雖然對的結果 ... 沒中 Orz,但其實寫下去也是蠻有趣的,但會不會再改成真的可以對獎的程式,那就看我想不想做了。順道藉此文恭祝大家新年開運旺旺來,心想事成,萬事如意。

適逢虎年除夕,即將邁入兔年的前一刻,台彩宣布百萬中獎的100個號碼,但因為一張一張對真的會眼花,索性花了十來分鐘撰寫了一支小程式,由電腦去做這件事,雖然對的結果 ... 沒中 Orz,但其實寫下去也是蠻有趣的,但會不會再改成真的可以對獎的程式,那就看我想不想做了。

這支小程式是用 C# 開發,對獎程式的主體是 LotteryNumber 類別,並顯露 Parse 方法 (可讀入字串格式為以空白分隔的號碼清單),透過 Match 方法來檢查是否有中獎,以及對中的號碼數量等,以下為所有的 code,如果你也閒來無事的話可以 view 一下 :)

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace LotteryMatch
{
public class LotteryNumber
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public int D { get; set; }
public int E { get; set; }
public int F { get; set; }
public int S { get; set; }

public LotteryNumber(int A, int B, int C, int D, int E, int F)
{
List<int> numberList = new List<int>();

numberList.Add(A);
numberList.Add(B);
numberList.Add(C);
numberList.Add(D);
numberList.Add(E);
numberList.Add(F);
numberList.Sort();

this.A = numberList[0];
this.B = numberList[1];
this.C = numberList[2];
this.D = numberList[3];
this.E = numberList[4];
this.F = numberList[5];
}

public static LotteryNumber Parse(string LotteryNumberString)
{
if (string.IsNullOrEmpty(LotteryNumberString))
throw new ArgumentException("樂透號碼字串不可為空白。", LotteryNumberString);

string[] numberParts = LotteryNumberString.Split(' ');

if (numberParts.Length < 6)
throw new ArgumentException("樂透號碼字串不足六個數字。", LotteryNumberString);

return new LotteryNumber(
Convert.ToInt32(numberParts[0]), Convert.ToInt32(numberParts[1]), Convert.ToInt32(numberParts[2]),
Convert.ToInt32(numberParts[3]), Convert.ToInt32(numberParts[4]), Convert.ToInt32(numberParts[5]));
}

public int Match(LotteryNumber WinLotteryNumber)
{
return this.Match(WinLotteryNumber.A, WinLotteryNumber.B, WinLotteryNumber.C, WinLotteryNumber.D, WinLotteryNumber.E, WinLotteryNumber.F);
}

public int MatchWithSpecialNumber(LotteryNumber WinLotteryNumber)
{
return this.Match(WinLotteryNumber.A, WinLotteryNumber.B, WinLotteryNumber.C, WinLotteryNumber.D, WinLotteryNumber.E, WinLotteryNumber.F, WinLotteryNumber.S);
}

public int Match(int A, int B, int C, int D, int E, int F)
{
int matchCount = 0;

if (A == this.A)
matchCount++;
if (B == this.B)
matchCount++;
if (C == this.C)
matchCount++;
if (D == this.D)
matchCount++;
if (E == this.E)
matchCount++;
if (F == this.F)
matchCount++;

return matchCount;
}

public int Match(int A, int B, int C, int D, int E, int F, int S)
{
int matchCount = 0;

if (A == this.A)
matchCount++;
if (B == this.B)
matchCount++;
if (C == this.C)
matchCount++;
if (D == this.D)
matchCount++;
if (E == this.E)
matchCount++;
if (F == this.F)
matchCount++;

return matchCount;
}

public override string ToString()
{
return string.Format("{0} {1} {2} {3} {4} {5}, S: {6}",
this.A.ToString("00"), this.B.ToString("00"), this.C.ToString("00"), this.D.ToString("00"), this.E.ToString("00"), this.F.ToString("00"), this.S.ToString("00"));
}
}

public class LotteryNumberCollection : List<LotteryNumber> { }

public class Program
{
static void Main(string[] args)
{
// read win number.
FileStream fsWinNumber = new FileStream(Environment.CurrentDirectory + @"\WinNumber.txt", FileMode.Open, FileAccess.Read);
// read match number.
FileStream fsMatchNumber = new FileStream(Environment.CurrentDirectory + @"\MatchNumber.txt", FileMode.Open, FileAccess.Read);

LotteryNumberCollection lotteryWinNumbers = new LotteryNumberCollection();
LotteryNumberCollection lotteryMatchNumbers = new LotteryNumberCollection();

StreamReader srWinNumber = new StreamReader(fsWinNumber);
StreamReader srMatchNumber = new StreamReader(fsMatchNumber);

while (!srMatchNumber.EndOfStream)
{
string number = srMatchNumber.ReadLine();

if (!string.IsNullOrEmpty(number))
lotteryMatchNumbers.Add(LotteryNumber.Parse(number));
}

while (!srWinNumber.EndOfStream)
{
string number = srWinNumber.ReadLine();

if (!string.IsNullOrEmpty(number))
lotteryWinNumbers.Add(LotteryNumber.Parse(number));
}

srWinNumber.Close();
srMatchNumber.Close();

Console.WriteLine("對獎中...");

foreach (LotteryNumber matchNumber in lotteryMatchNumbers)
{
int num = 0;

// scan win number.
foreach (LotteryNumber winNumber in lotteryWinNumbers)
{
int matchCount = matchNumber.Match(winNumber);

if (matchCount == 6)
Console.WriteLine("中獎了!!! 輸入號: {0}, 對中獎號: {1}, 組數: {2}", matchNumber.ToString(), winNumber.ToString(), num.ToString("00"));
else
Console.WriteLine("未中獎。輸入號: {0}, 獎號: {1}, 組數: {2}, 對中號碼: {3}", matchNumber.ToString(), winNumber.ToString(), num.ToString("00"), matchCount);

num++;
}
}

Console.WriteLine("對獎完畢。請按 ENTER 結束程式。");
Console.ReadLine();
}
}
}

順道藉此文恭祝大家新年開運旺旺來,心想事成,萬事如意。