C# Spinner [Random spin] - [Uniqueness] - [Combinations] - EXAMPLES

MysteryGuest

Registered Member
Joined
Mar 7, 2013
Messages
64
Reaction score
18
Well working on a spinner so i thought i release a part of my old source, This is doing what it suppose to do! however this is the basic stuff.

So for the random spin
Code:
public void StartRandomSpin()
        {
            if (_form.rtbMain.Text.Split('{').Length != _form.rtbMain.Text.Split('}').Length) return;
            var text = _form.rtbMain.Text;
            var md = Regex.Matches(text, "\\{[\\s\\S]*\\}*");
            var rnd = new Random();
            foreach (Match d in md)
            {
                var dstr = d.Value;
                var carrier = RandomSpinner(dstr, rnd);
                text = _form.rtbComplete.Text = Regex.Replace(text, Regex.Escape(d.Value), carrier);
            }
            try
            {
                var unique = Uniqueness(_form.rtbMain.Text);
                _form.lblUniqueness.Text = unique != "" ? unique + @"%" : @"0.00%";
                _form.lblCombinations.Text = Combinations(_form.rtbMain.Text) > 1000000 ? "1,000,000+" : Combinations(_form.rtbMain.Text).ToString();
            }
            catch
            {
                _form.pControls.Visible = false;
            }
            _form.lblCharCount.Text = _form.rtbComplete.Text.Length.ToString(CultureInfo.InvariantCulture);
        }


private static string RandomSpinner(string mstr, Random rdm)
        {
            var cnt = 0;
            while (Regex.IsMatch(mstr, "\\{"))
            {
                var innerspin = mstr.Split('{')[(mstr.Split('{').Length) - 1];
                var tospin = Regex.Match(innerspin, "[\\s\\S]*?(?=\\})").Value;

                var arrSplit = tospin.Split('|');
                var returnword = arrSplit[rdm.Next(0, arrSplit.Length)];
                cnt++;
                Console.WriteLine(cnt);

                mstr = Regex.Replace(mstr, "\\{" + Regex.Escape(tospin) + "\\}", returnword);
            }
            return mstr;
        }


And for the uniqueness.
Code:
private static string Uniqueness(string text)
        {
            var notspun = Regex.Matches(text, "\\{[\\s\\S]*?\\}");
            var spincount = notspun.Cast<Match>().Sum(m => m.Value.Length);
            var all = Regex.Matches(text, "[\\S]*");
            var allcount = all.Cast<Match>().Sum(m => m.Value.Length);
            var nonspun = allcount - spincount;

            var mc = Regex.Matches(text, "(?<=\\{)[\\s\\S]*?(?=\\})");
            var arrDoubles = new decimal[mc.Count];
            var x = 0;
            foreach (Match m in mc)
            {
                var arrMatch = m.Value.Split('|');
                var averagelengthcnt = arrMatch.Sum(s => s.Length);
                arrDoubles[x] = Decimal.Divide(averagelengthcnt, arrMatch.Length);
                x++;
            }
            return text = (Decimal.Divide(arrDoubles.Sum(), (arrDoubles.Sum() + nonspun)) * 100).ToString("#.##");
        }

and for the combinations!
Code:
private static int Combinations(string text)
        {
            var mc = Regex.Matches(text, "\\{[\\s\\S]*?\\}");
            var combo = 1;
            foreach (var arrMatch in from Match m in mc select m.Value.Split('|'))
            {
                combo *= arrMatch.Length;
                if (combo <= 1000000) continue;
                break;
            }
            return combo;
        }

I'm not going to explain how everything works! but i can say if you want to use this as a class. You need to hook the class to your from first. How i did this is like this, this has to be placed in your class..
Code:
private FrmMain _form;

public void HookMainForm(FrmMain mainForm)
{
_form = mainForm;
}

So now go to your main form double click it and go to your constructor, if you don't understand this please don't bother continuing this. add this line. Almost forgot declare your class first


Code:
readonly WordSpin _wordSpin = new WordSpin();

//Name of my constructor
public FrmMain()
{
InitializeComponent();
_wordSpin.HookMainForm(this);
}

EDIT: almost forgot to call the functions. just go to your main form under a button.
Code:
_wordSpin.StartRandomSpin();

And your done! just use 1 button, and 2 richtextboxes.
 
Last edited:
Back
Top