Jump to content

Need a brute forcer


MagnusTheRed90

Recommended Posts

So, I have been thinking about applying brute forcing to other things in computer programming, like for example brute forcing the most effective LRM's on a battlemech (Mechwarrior online) such that I get maximum damage potential for given tonnage and slot space. I actually have some convincing code, but I am in need of a simple hello world of brute forcing in order to exhaust all possibilities. I remember a few years ago someone did a brute forcer in java and posted the code on stack overflow. It was great, but the post has since been garbage collected or something. I can't wrap my head around the recursion required. I need a simple brute force algorithim, which can use characters, since I am wholely capable of translating the algorithim once I get it. The brute force will be contained only to my local desktop, and will not execute elsewhere. Any help would be appreciated. The following is what I have so far;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LrmBruteForcer.Models;

namespace LrmBruteForcer {
    public delegate void NotifyLrm(LRM[] values);

    /// <summary>
    /// Brute forcer for the lrm script. 
    /// </summary>
    public class BruteForcer {

        public event NotifyLrm LrmComboFound;

        /// <summary>
        /// Number of slots available. 
        /// </summary>
        public int Slots {
            get; set;
        }

        /// <summary>
        /// Number of tonnes available. 
        /// </summary>
        public int Tonnage {
            get; set;
        }

        public BruteForcer(int slots, int tonnage) {
            this.Slots = slots;
            this.Tonnage = tonnage;
        }

        //the lrms available
        private LRM[] LrmsAvailable = new ConcreteLrmFactory().GetLrms();

        private int[] valueIndexes = null;

        /// <summary>
        /// Run the brute force program. 
        /// </summary>
        public void Run() {
            //aaaa
            FillArray(Slots);

            LRM[] currentValue = null;
            decimal tonnage = -1;
            //loop backwards aaaa, aaab
            for (int i = Slots - 1; i > 0; i--) {
                //loop over lrms available
                for (int j = 0; j < LrmsAvailable.Count(); j++) {
                    currentValue = GetCurrentValue(Slots);
                    tonnage = currentValue
                        .Sum(val => val.Tonnage);
                    //if (tonnage <= Tonnage) {
                        //if (LrmComboFound != null)
                    LrmComboFound.Invoke(currentValue);
                    //}
                    Iterate(i);
                }//end loop
                Iterate(i);
                //Iterate(i);
            }//end loop

            //Run(valueIndexes);
        }//end method

        ///// <summary>
        ///// Run the brute force program. 
        ///// </summary>
        //private void Run(int[] valueIndexes) {
        //    this.valueIndexes = valueIndexes;

        //    //loop backwards aaaa, aaab
        //    for (int i = Slots - 1; i > 0; i--) {

        //        //loop over lrms available
        //        for (int j = 0; j < LrmsAvailable.Count(); j++) {
        //            var currentValue = GetCurrentValue(Slots);
        //            decimal tonnage = currentValue
        //                .Sum(val => val.Tonnage);
        //            if (tonnage <= Tonnage) {
        //                if (LrmComboFound != null)
        //                    LrmComboFound.Invoke(currentValue);
        //            }

        //            Iterate(i);
        //        }//end loop

        //        //Iterate(i);
        //    }//end loop

        //}//end method

        /// <summary>
        /// Iterate the value passed to the method. 
        /// </summary>
        private void Iterate(int index) {
            if (index > valueIndexes.Count() - 1)
                throw new ArgumentException("Argument outside of range of array bounds. Brute Force Exception. ");

            //if we have to start over
            if (valueIndexes[index] + 1 > LrmsAvailable.Count() - 1) {
                valueIndexes[index] = 0;
                return;
            }

            valueIndexes[index]++;
        }//end method

        /// <summary>
        /// Fill the current value at the beginning of the program. 
        /// </summary>
        private void FillArray(int numSlots) {
            valueIndexes = new int[numSlots];

            //loop over values fill entry
            for (int i = 0; i < valueIndexes.Length; i++) {
                valueIndexes[i] = 0;
            }//end loop

        }//end method

        /// <summary>
        /// Gets the current value of the brute forcer. 
        /// </summary>
        private LRM[] GetCurrentValue(int numSlots) {
            LRM[] lrms = new LRM[numSlots];
            int i = 0;
            foreach (var index in valueIndexes) {
                lrms[i] = LrmsAvailable[index];
                i++;
            }//end loop

            return lrms;
        }//end method

    }//end class

}//end namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LrmBruteForcer.Models;

namespace LrmBruteForcer {

    class Program {

        static void Main(string[] args) {
            BruteForcer brute = new BruteForcer(5, 30);
            brute.LrmComboFound += ComboFound;
            brute.Run();

            Console.Write("Press any key to continue... ");
            Console.ReadKey();
        }//end method

        public static LRM[] mostDamaging = null;

        /// <summary>
        /// An lrm combo has been found. 
        /// </summary>
        /// <param name="combo"></param>
        public static void ComboFound(LRM[] combo) {
            decimal damageA = combo.Sum(entry => entry.Damage);

            decimal? damageB = null;
            if (mostDamaging != null)
                damageB = mostDamaging.Sum(entry => entry.Damage);

            if (damageA > (damageB == null ? 0 : damageB.Value)) {
                mostDamaging = combo;

                foreach (var lrm in combo) {
                    Console.Write(lrm.ToString() + ", ");
                }//end loop
                Console.WriteLine();
            }
        }//end method

    }//end class

}//end namespace

The whole code will not be present, but you should be able to get the picture.

Part of the problem is I can't remember how the character values should go; 'aaaa', 'aaab', 'aaac', 'aaba' (???).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...