练习之彩票三 添加号码相关代码
源代码已放入我的开源网址中,希望大家踊跃拍砖,或者直接优化代码。共同进步 谢谢
基础工具
排列组合算法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TG.Package.Lottery.Calculate { public class Discrete { /// <summary> /// Permutation ( ex: P(8,6) or P(6,6) ) /// </summary> public static Int32 P(Int32 n, Int32 m) { if (m > n && m <= 0 && n <= 0) { throw new FormatException("error:m is Larger than n"); } Int32 result = 1; for (var i = 0; i < m; i++) { try { checked { result = result * (n - i); } } catch { throw new FormatException("error:result is Larger than Int32.MaxValue"); } } return result; } /// <summary> /// Combination ( ex: C(8,6) or C(6,6) ) /// </summary> public static Int32 C(Int32 n, Int32 m) { if (n == m) { return 1; } return P(n, m) / P(m, m); } } }
号码验证:
工厂:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using TG.Package.Lottery.Base; namespace TG.Package.Lottery.ValidHelper { abstract class ValidLotteryNumber { public static void ValidNumbers(WelfareLottery pWelfareLottery, LotteryType pLotteryType) { IValidNumber valid = null; switch (pLotteryType) { case LotteryType.TwoColor: valid = new TwoColor(); break; case LotteryType.FuCai3D: valid = new FuCai3D(); break; default: break; } if (null != valid) { valid.Valid(pWelfareLottery); } } } }
接口:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using TG.Package.Lottery.Base; namespace TG.Package.Lottery.ValidHelper { interface IValidNumber { /// <summary> /// ValidNumbers /// </summary> /// <param name="numbers">ex:"01,02"or "1,2"</param> /// <returns></returns> void Valid(WelfareLottery pWelfareLottery); } }
双色球:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using TG.Package.Lottery.Base; namespace TG.Package.Lottery.ValidHelper { public class TwoColor : IValidNumber { private const Int32 mBit = 2; private const Int32 mRedMax = 33; private const Int32 mRedMin = 1; private const Int32 mBlueMax = 16; private const Int32 mBlueMin = 1; private const Char SplitStr = ','; public void Valid(WelfareLottery pWelfareLottery) { PlayType playtype = pWelfareLottery.PlayType; String red = pWelfareLottery.Lottery[NumericType.Red]; String blue = pWelfareLottery.Lottery[NumericType.Blue]; String dan = String.Empty; ValidNumbers(red, NumericType.Red); ValidNumbers(blue, NumericType.Blue); if (playtype == PlayType.DanTuo) { dan = pWelfareLottery.Lottery[NumericType.Dan]; ValidNumbers(dan, NumericType.Dan); if (NumberHelper.IsRepeat(red.Split(SplitStr), dan.Split(SplitStr))) { throw new FormatException("error:dan number is repeat"); } } } public void ValidNumbers(String pNumbers, NumericType pNumericType) { String[] res = pNumbers.Split(SplitStr); if (NumberHelper.IsRepeat(res)) { throw new FormatException("error:number is repeat"); } for (var i = 0; i < res.Length; i++) { if (!ValidHelper.NumberHelper.IsBitLength(res[i], mBit)) { throw new FormatException("error:number bit is error"); } if (!ValidNumber(res[i], pNumericType)) { throw new FormatException("error:number is error"); } } } public bool ValidNumber(string pNumber, NumericType pNumericType) { switch (pNumericType) { case NumericType.Dan: case NumericType.Red: return ValidHelper.NumberHelper.ValidNumber(pNumber, mRedMax, mRedMin); case NumericType.Blue: return ValidHelper.NumberHelper.ValidNumber(pNumber, mBlueMax, mBlueMin); default: return false; } } } }
福彩3D:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using TG.Package.Lottery.Base; namespace TG.Package.Lottery.ValidHelper { public class FuCai3D : IValidNumber { private const Int32 mBit = 1; private const Int32 mMax = 9; private const Int32 mMin = 0; private const Int32 mZ3MinSize = 2; private const Int32 mMinSize = 3; private const Char SplitStr = ','; public void Valid(WelfareLottery pWelfareLottery) { switch (pWelfareLottery.PlayType) { case (PlayType.Zu6 | PlayType.DanTuo): case (PlayType.Normal | PlayType.DanTuo): ValidNormalDan(pWelfareLottery); break; case (PlayType.Zu3 | PlayType.DanTuo): ValidZu3Dan(pWelfareLottery); break; case PlayType.Normal: ValidNormal(pWelfareLottery); break; case PlayType.Zu3: ValidZu3(pWelfareLottery); break; case PlayType.Zu6: ValidZu6(pWelfareLottery); break; default: throw new FormatException("error:playtype is wrong"); } } private static void ValidNumbers(String pNumbers) { String[] res = pNumbers.Split(SplitStr); if (NumberHelper.IsRepeat(res)) { throw new FormatException("error:number is repeat"); } for (var i = 0; i < res.Length; i++) { if (!ValidHelper.NumberHelper.IsBitLength(res[i], mBit)) { throw new FormatException("error:number bit is error"); } if (!ValidNumber(res[i])) { throw new FormatException("error:number is error"); } } } private static bool ValidNumber(String pNumber) { return ValidHelper.NumberHelper.ValidNumber(pNumber, mMax, mMin); } private static void ValidNormal(WelfareLottery pWelfareLottery) { String one = pWelfareLottery.Lottery[NumericType.Ones]; String ten = pWelfareLottery.Lottery[NumericType.Tens]; String hundred = pWelfareLottery.Lottery[NumericType.Hundreds]; ValidNumbers(one); ValidNumbers(ten); ValidNumbers(hundred); } private static void ValidZu3(WelfareLottery pWelfareLottery) { String zu3 = pWelfareLottery.Lottery[NumericType.Zu3]; if ((zu3.Length / 2 + 1) < 2) { throw new FormatException("error:z3 length large than 1"); } ValidNumbers(zu3); } private static void ValidZu6(WelfareLottery pWelfareLottery) { String zu6 = pWelfareLottery.Lottery[NumericType.Zu6]; if ((zu6.Length / 2 + 1) < 3) { throw new FormatException("error:z3 length less than 3"); } ValidNumbers(zu6); } private static void ValidZu3Dan(WelfareLottery pWelfareLottery) { String dan = pWelfareLottery.Lottery[NumericType.Dan]; String tuo = pWelfareLottery.Lottery[NumericType.Tuo]; Int32 tuocount = tuo.Length / 2 + 1; if (dan.Length != 1) { throw new FormatException("error:dan length must be 1"); } if (tuocount < 2) { throw new FormatException("error:dan length less than 2"); } if (NumberHelper.IsRepeat(dan.Split(SplitStr), tuo.Split(SplitStr))) {throw new FormatException("error:tuo number is repeat with dan"); } ValidNumbers(dan); ValidNumbers(tuo); } private static void ValidNormalDan(WelfareLottery pWelfareLottery) { String dan = pWelfareLottery.Lottery[NumericType.Dan]; String tuo = pWelfareLottery.Lottery[NumericType.Tuo]; Int32 dancount=dan.Length/2+1; Int32 tuocount = tuo.Length / 2 + 1; if (dancount != 1 && dancount != 2) { throw new FormatException("error:dan numbers must be 1-2"); } if (tuocount < 2) { throw new FormatException("error:dan length less than 2"); } if ((dancount + tuocount)<3) { throw new FormatException("error:numbers is not enough"); } if (NumberHelper.IsRepeat(dan.Split(SplitStr), tuo.Split(SplitStr))) { throw new FormatException("error:tuo number is repeat with dan"); } ValidNumbers(dan); ValidNumbers(tuo); } } }
彩票接口:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; namespace TG.Package.Lottery.Base { public interface ILottery { Int32 Count{ get; } Int32 TotalBet { get; } /// <summary> /// 期号 /// </summary> string LotteryIssue{ get; } WelfareLottery this[Int32 i]{ get; } void Valid(WelfareLottery pWelfareLottery); void Remove(Int32 i); void Clear(); }//end ILottery }//end namespace System
双色球:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using TG.Package.Lottery.Base; using System.Collections; namespace TG.Package.Lottery { public class TwoColor : ILottery,IDisposable { public TwoColor(String pLotteryIssue) { mLotteryIssue = pLotteryIssue; mWelfareLotterys = new List<WelfareLottery>(); } #region member private List<WelfareLottery> mWelfareLotterys; public WelfareLottery this[Int32 i] { get { return this.mWelfareLotterys[i]; } } private Int32 mTotalBet = 0; public Int32 TotalBet { get { return mTotalBet; } } public Int32 Count { get { return this.mWelfareLotterys.Count; } } private String mLotteryIssue = String.Empty; /// <summary> /// 期号 /// </summary> public string LotteryIssue { get { return mLotteryIssue; } } #endregion #region function #region ADD /// <summary> /// Add Lottery Numbers /// </summary> /// <param name="pRed">red number ex:"01,02"</param> /// <param name="pBlue">blue number ex:"01,02"</param> /// <param name="pDan">dan number ex:"01,02"</param> public void Add(String pRed, String pBlue, String pDan) { WelfareLottery lottery = new WelfareLottery(); lottery.Lottery.Add(NumericType.Red, pRed); lottery.Lottery.Add(NumericType.Blue, pBlue); lottery.Lottery.Add(NumericType.Dan, pDan); lottery.PlayType = PlayType.DanTuo; Valid(lottery); lottery.BetCount = CalculateBet(pRed, pBlue, pDan); mTotalBet += lottery.BetCount; mWelfareLotterys.Add(lottery); } /// <summary> /// Add Lottery Numbers /// </summary> /// <param name="pRed">red number ex:"01,02"</param> /// <param name="pBlue">blue number ex:"01,02"</param> public void Add(String pRed, String pBlue) { WelfareLottery lottery = new WelfareLottery(); lottery.Lottery.Add(NumericType.Red, pRed); lottery.Lottery.Add(NumericType.Blue, pBlue); lottery.PlayType = PlayType.Normal; Valid(lottery); lottery.BetCount = CalculateBet(pRed, pBlue, String.Empty); mTotalBet += lottery.BetCount; mWelfareLotterys.Add(lottery); } /// <summary> /// Add Number Collections /// </summary> public void AddRange(IList<String> pRed, IList<String> pBlue) { if (pRed.Count == pBlue.Count) { for (var i = 0; i < pRed.Count; i++) { if (!String.IsNullOrEmpty(pRed[i]) && !String.IsNullOrEmpty(pBlue[i])) { Add(pRed[i], pBlue[i]); } else { throw new FormatException("red or blue is Empty"); } } } else { throw new FormatException("red and blue Length is not equal"); } } #endregion /// <summary> /// Calculate one Lottery`s bet /// </summary> /// <param name="pRed">Red number</param> /// <param name="pBlue">blue number</param> /// <param name="pDan">dan number</param> /// <returns></returns> private static Int32 CalculateBet(String pRed, String pBlue, String pDan) { Int32 redcount = (pRed.Length / 3) + 1; Int32 bluecount = (pBlue.Length / 3) + 1; Int32 dancount = 0; if (!String.IsNullOrEmpty(pDan)) { dancount = (pDan.Length / 3) + 1; } return Calculate.Discrete.C(redcount, 6 - dancount) * bluecount; } /// <summary> /// Valid number /// </summary> public void Valid(WelfareLottery pWelfareLottery) { ValidHelper.ValidLotteryNumber.ValidNumbers(pWelfareLottery, LotteryType.TwoColor); } /// <summary> /// Remove WelfareLotterys with i /// </summary> /// <param name="i">index</param> public void Remove(int i) { mTotalBet -= mWelfareLotterys[i].BetCount; this.mWelfareLotterys.RemoveAt(i); } /// <summary> /// Clear All WelfareLotterys /// </summary> public void Clear() { mTotalBet = 0; this.mWelfareLotterys.Clear(); } #endregion public void Dispose() { mWelfareLotterys = null; mLotteryIssue = null; } }//end TwoColor }//end namespace System
福彩3D:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using TG.Package.Lottery.Base; namespace TG.Package.Lottery { public sealed class FuCai3D : ILottery,IDisposable { public FuCai3D(String pLotteryIssue) { mWelfareLotterys = new List<WelfareLottery>(); mLotteryIssue = pLotteryIssue; } #region member private Int32 mTotalBet = 0; public int TotalBet { get { return mTotalBet; } } public Int32 Count{ get{ return this.mWelfareLotterys.Count; } } private string mLotteryIssue=String.Empty; /// <summary> /// 期号 /// </summary> public string LotteryIssue{ get{ return mLotteryIssue; } } public WelfareLottery this[Int32 i]{ get{ return this.mWelfareLotterys[i]; } } private List<WelfareLottery> mWelfareLotterys; #endregion #region function public void Add(String pOne, String pTen, String pHundred) { WelfareLottery lottery = new WelfareLottery(); lottery.PlayType = PlayType.Normal; lottery.Lottery.Add(NumericType.Ones, pOne); lottery.Lottery.Add(NumericType.Tens, pTen); lottery.Lottery.Add(NumericType.Hundreds, pHundred); Valid(lottery); lottery.BetCount = CalculateBet(lottery); mTotalBet += lottery.BetCount; mWelfareLotterys.Add(lottery); } public void Add(String pDan, String pTuo, PlayType pPlayType) { WelfareLottery lottery = new WelfareLottery(); lottery.PlayType = pPlayType; lottery.Lottery.Add(NumericType.Dan, pDan); lottery.Lottery.Add(NumericType.Tuo, pTuo); Valid(lottery); lottery.BetCount = CalculateBet(lottery); mTotalBet += lottery.BetCount; mWelfareLotterys.Add(lottery); } public void Add(String pNumbers, PlayType pPlayType) { WelfareLottery lottery = new WelfareLottery(); lottery.PlayType = pPlayType; switch(pPlayType) { case PlayType.Zu3: lottery.Lottery.Add(NumericType.Zu3, pNumbers); break; case PlayType.Zu6: lottery.Lottery.Add(NumericType.Zu6, pNumbers); break; default: throw new FormatException("error:PlayType must be zu3 or zu6"); } Valid(lottery); lottery.BetCount = CalculateBet(lottery); mTotalBet += lottery.BetCount; mWelfareLotterys.Add(lottery); } public void Valid(WelfareLottery pWelfareLottery) { ValidHelper.ValidLotteryNumber.ValidNumbers(pWelfareLottery,LotteryType.FuCai3D); } #region CalculateBet private Int32 CalculateBet(WelfareLottery pWelfareLottery) { switch (pWelfareLottery.PlayType) { case PlayType.Normal: return NormalCalculateBet(pWelfareLottery); case PlayType.Zu3: return Zu3CalculateBet(pWelfareLottery); case PlayType.Zu6: return Zu6CalculateBet(pWelfareLottery); ; case PlayType.Normal | PlayType.DanTuo: return NormalDanCalculateBet(pWelfareLottery); case PlayType.Zu3 | PlayType.DanTuo: return Zu3DanCalculateBet(pWelfareLottery); case PlayType.Zu6 | PlayType.DanTuo: return Zu6DanCalculateBet(pWelfareLottery); default: throw new FormatException("error:PlayType is error"); } } private static Int32 NormalCalculateBet(WelfareLottery pWelfareLottery) { Int32 onecount=(pWelfareLottery.Lottery[NumericType.Ones].Length/2)+1; Int32 tencount = (pWelfareLottery.Lottery[NumericType.Tens].Length/2)+1; Int32 hundredcount = (pWelfareLottery.Lottery[NumericType.Hundreds].Length/2)+1; return onecount * tencount * hundredcount; } private static Int32 Zu3CalculateBet(WelfareLottery pWelfareLottery) { Int32 zu3count=(pWelfareLottery.Lottery[NumericType.Zu3].Length/2)+1; return Calculate.Discrete.P(zu3count, 2); } private static Int32 Zu6CalculateBet(WelfareLottery pWelfareLottery) { Int32 zu6count=(pWelfareLottery.Lottery[NumericType.Zu6].Length/2)+1; return Calculate.Discrete.C(zu6count, 3); } private static Int32 NormalDanCalculateBet(WelfareLottery pWelfareLottery) { Int32 dancount = (pWelfareLottery.Lottery[NumericType.Dan].Length / 2) + 1; Int32 tuocount = (pWelfareLottery.Lottery[NumericType.Tuo].Length / 2) + 1; return 3 * dancount * tuocount; } private static Int32 Zu3DanCalculateBet(WelfareLottery pWelfareLottery) { Int32 tuocount = (pWelfareLottery.Lottery[NumericType.Tuo].Length / 2) + 1; return 2 * tuocount; } private static Int32 Zu6DanCalculateBet(WelfareLottery pWelfareLottery) { Int32 dancount = (pWelfareLottery.Lottery[NumericType.Dan].Length / 2) + 1; Int32 tuocount = (pWelfareLottery.Lottery[NumericType.Tuo].Length / 2) + 1; return Calculate.Discrete.C(tuocount, 3 - dancount); } #endregion public void Remove(int i) { mTotalBet -= mWelfareLotterys[i].BetCount; this.mWelfareLotterys.RemoveAt(i); } public void Clear() { mTotalBet = 0; this.mWelfareLotterys.Clear(); } #endregion public void Dispose() { mWelfareLotterys = null; mLotteryIssue = null; } }//end FuCai3D }//end namespace System
我的开源的地址https://github.com/tianjing/
大家踊跃拍砖共同成长,拍砖的同时记得附上意见或者建议。!!谢谢 谢谢