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

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