扑克洗牌程序

本文介绍了一种使用完全剩余系表达式生成器实现的扑克牌随机算法。该算法通过线性同余生成器创建随机索引,并利用这些索引来重新排序扑克牌数组。此外,还提供了一种备选洗牌方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Text;

namespace RandomOut
{
    
public interface IRandom
    
{
        
void BringToWorld();
        
void MixedRandomized();
    }


    
public abstract class TPoker : IRandom
    
{
        
public virtual void BringToWorld() { }
        
public abstract void MixedRandomized();
    }


    
public class Pokers : TPoker
    
{
        
private List<int> _poker;
        
private Random random;
        
private int baseNum;
        
private int extra;
        
/// <summary>
        
/// Summary description for Pokers
        
/// </summary>

        public Pokers()
        
{
            random 
= new Random(DateTime.Now.Millisecond);
            Random rdm 
= new Random();
            baseNum 
= rdm.Next(11000);
            extra 
= rdm.Next(11000);
        }

        
/// <summary>
        
/// Complete System of Residues Expression Generator
        
/// </summary>
        
/// <param name="index">An index to generate an index using Linear Congruence Generator</param>
        
/// <returns></returns>

        private int EquationVendor(int index)
        
{
            
return (baseNum * index + extra);
        }

        
/// <summary>
        
/// Randomized the Order of the Poker
        
/// </summary>
        
/// <param name="count">The total pieces of Poker</param>
        
/// <returns></returns>

        private int[] Randomized(int count)
        
{
            
int[] indexes = new int[count];
            
for (int i = 0; i < count; i++)
            
{
                indexes[i] 
= EquationVendor(i) % count;
            }


            
return indexes;
        }

        
/// <summary>
        
/// Generate a set of Poker
        
/// </summary>

        public override void BringToWorld()
        
{
            _poker 
= new List<int>();
            _poker.Add(
0);
            _poker.Add(
0);
            
for (int i = 1; i < 14; i++)
            
{
                
for (int j = 0; j < 4; j++)
                
{
                    _poker.Add(i);
                }

            }

        }

        
/// <summary>
        
/// Randomized the Order of the Poker, using Complete System of Residues
        
/// </summary>

        public override void MixedRandomized()
        
{
            
int[] indexes = Randomized(_poker.Count);
            
            List
<int> tmp = new List<int>();
            
foreach (int index in indexes)
            
{
                tmp.Add(_poker[index]);
            }


            _poker 
= tmp;
        }

        
/// <summary>
        
/// Shuffle the Poker, just another method different from MixedRandomized
        
/// </summary>

        public void Shuffle()
        
{
            
int tmp = 0;
            
int card = 0;

            
for (int i = 0; i < _poker.Count; i++)
            
{
                tmp 
= random.Next(0, _poker.Count);
                card 
= _poker[tmp];
                _poker[tmp] 
= _poker[i];
                _poker[i] 
= card;
            }

        }

        
/// <summary>
        
/// A hand to generate the parameters for the Complete System of Residues Expression Generator
        
/// </summary>

        public void Hand()
        
{
            Random rdm 
= new Random();
            baseNum 
= rdm.Next(11000);
            extra 
= rdm.Next(11000);
            
while (!IsCorrectBaseNum(baseNum - 1))
            
{
                baseNum 
= rdm.Next(11000);
            }

            
while (!IsCorrectExtra(extra))
            
{
                extra 
= rdm.Next(11000);
            }

        }

        
/// <summary>
        
/// Check if the BaseNum for Complete System of Residues Expression Generator is Correct
        
/// </summary>
        
/// <param name="num">BaseNum for Complete System of Residues</param>
        
/// <returns></returns>

        private bool IsCorrectBaseNum(int num)
        
{
            
if ((num % 3 == 0&& (num % 2== 0)
                
return true;

            
return false;
        }

        
/// <summary>
        
/// Check if the Extra for Complete System of Residues Expression Generator is Correct
        
/// </summary>
        
/// <param name="ex">Extra for Complete System of Residues</param>
        
/// <returns></returns>

        private bool IsCorrectExtra(int ex)
        
{
            
if (GreatCommonDivisor(ex, _poker.Count) == 1)
                
return true;
            
return false;
        }

        
/// <summary>
        
/// Gets the Great Common Divisor
        
/// </summary>
        
/// <param name="a">First parameter</param>
        
/// <param name="b">Second parameter</param>
        
/// <returns>Great Common Divisor</returns>

        private int GreatCommonDivisor(int a, int b)
        
{
            
int dividend, divisor, tmp;

            dividend 
= a > b ? a : b;
            divisor 
= a < b ? a : b;

            
while (dividend % divisor != 0)
            
{
                tmp 
= divisor;
                divisor 
= dividend % divisor;
                dividend 
= tmp;
            }


            
return divisor;
        }

        
/// <summary>
        
/// Gets the Least Common Multiple
        
/// </summary>
        
/// <param name="a">First parameter</param>
        
/// <param name="b">Second parameter</param>
        
/// <returns>Least Common Multiple</returns>

        private int LeastCommonMultiple(int a, int b)
        
{
            
int gcd, lcm;

            gcd 
= GreatCommonDivisor(a, b);
            lcm 
= (a / gcd) * (b / gcd) * gcd;

            
return lcm;
        }

        
/// <summary>
        
/// Override ToString to a specified Format Style only for Pokers
        
/// </summary>
        
/// <returns></returns>

        public override string ToString()
        
{
            
string ret = string.Empty;

            
for (int i = 0; i < _poker.Count; i++)
            
{
                
if (i == _poker.Count - 1)
                    ret 
+= _poker[i].ToString();
                
else
                    ret 
+= _poker[i].ToString() + "->";
            }


            
return ret;
        }

        
/// <summary>
        
/// Gets a Poker
        
/// </summary>

        public List<int> Poker
        
{
            
get return _poker; }
        }

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值