Tag Archives: Android

Helpers and Extension Methods for Games

Typically, extension methods and helpers are small methods that allow the same functional code to be re-used. An excellent (and useful) example is this:

        public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
        {
           foreach (T item in enumeration) action(item);
        }

The above extension method provides a `ForEach` method for `IEnumerable`.

Are games different?

Yes.

Because a method for a game may involve a random algorithm. Below are a collection of methods that I’ve found useful.

Helper Methods

I’ve put all the following methods in a class called Dice. Obviously, for the first method, the name only makes sense in the context of a Dice class.

        /// <summary>
        /// Returns a boolean denoting the result of a 1 in chance dice roll
        /// </summary>
        /// <param name="chance"></param>
        /// <returns></returns>
        public static bool Roll(int chance)
        {
            Random rnd = new Random();

            return rnd.Next(chance) == 1;
        }

I use this everywhere in my games:

If (Dice.Roll(10)) // character has a 1 in 10 chance of dying if they drink the potion

The next method is basically just a wrapper for Random.Next.

Tip: You may find the random algorithm works best if you maintain the `rnd` variable for the life of the game. The reason for this being the the default random seed is taken from the system clock. Consequently, calling the same method in rapid succession may result in the same number being returned.

        /// <summary>
        /// Select a random number
        /// </summary>
        /// <param name="max"></param>
        /// <returns></returns>
        public static int PickRandomNumber(int max)
        {
            if (max <= 0) return 0;

            Random rnd = new Random();
            return rnd.Next(max);
        }

    }
&#91;/sourcecode&#93;

The context for this would be to allocate a number, for example:

&#91;sourcecode langiage="csharp"&#93;
Character.Health = Dice.RickRandomNumber(10); // allocate character's initial health
&#91;/sourcecode&#93;


<strong>Extension Methods</strong>

The following methods allow you to return a random element from a collection; the second allows for a predicate before the element is selected.


    static class EnumerableExtensions
    {
        public static T SelectRandomElement<T>(this IEnumerable<T> enumeration)
        {
            Random rnd = new Random();
            int idx = rnd.Next(enumeration.Count());

            return enumeration.ElementAt(idx);
        }

        public static T SelectRandomElement<T>(this IEnumerable<T> enumeration, Func<T, bool> predicate)
        {
            var filteredList = new List<T>();
            enumeration.ForEach((item) => { if (predicate(item)) filteredList.Add(item); });

            Random rnd = new Random();
            int idx = rnd.Next(filteredList.Count());

            return filteredList.ElementAt(idx);
        }
    }

A typical usage for these would be:

var enemy = AllCharacters.SelectRandomElement(e => e.CharacterType == CharacterTypes.Enemy);

Unit Tests

The biggest problem with code like this, is that it’s difficult to unit test. For example:

[TestMethod]
public void TestSelectRandomElementPredicate()
{
List testCollection = new List();

testCollection.Add(1);
testCollection.Add(3);
testCollection.Add(5);
testCollection.Add(7);
testCollection.Add(9);

int e = testCollection.SelectRandomElement(n => n < 2); Assert.AreNotEqual(e, 1); e = testCollection.SelectRandomElement(n => n < 2); Assert.AreNotEqual(e, 1); [/sourcecode] Certainly, if the method is bug free, then this will always pass; however, there may be a bug that only ever occurs when the first or last element is chosen. In the next post I'll cover how to unit test methods with random elements.

Windows 8 Shortcuts

Windows 8 is a great operating system, but sometimes, without knowing the shortcuts and apps available, it can be difficult to achieve even mundane tasks. Here’s some of the tools and tricks that I use.

Disclaimer

I am the author of one of the apps mentioned below. I have no financial interest in Console OS.

Mail

Windows 8 mail app is definitely not as good as outlook. However, what people forget about when making this comparison is price:

Outlook is a commercial product and sells for just over £90 (that’s around $170 at the time of writing). Windows mail app is part of Windows 8. So it’s free.

If you want to beef it up a bit you can find some useful apps in the store. This one allows to to configure a mail template and associate that with a windows tile.

Multi-screen

Many people these days run more than one monitor; especially with tablets. If you haven’t come across it yet, the magic key-combination here is Windows Key + Shift + Cursor Left or Windows Key + Shift + Cursor Right to move the current window to the next monitor.

Search

Windows Key + S will allow you to search from anywhere; however, if you just press the Windows Key you’ll get taken to the start menu. From there you can just type and Windows should quickly find what you’re looking for.

When you’re done with an app, remember that moving away from the app, or even dragging it to close (finger at the top and the screen and swipe down) may not close the app. To do this, swipe down, but keep the app held at the bottom of the screen until the tile rotates: now it’s gone.

Copy and Paste

Admittedly, the store apps have a way to go with this. Complex text manipulation is difficult without a mouse, and whichever platform you choose, you’ll be swearing at the screen as you try to select the area between a comma and the preceding text. Windows big let down here is that some of the time, press and hold doesn’t give you a cut/copy/paste context menu. However, it does give you a keyboard (all textboxes do). So, simply select Ctrl and X, C or V from there. Not perfect, but it works.

Killer Apps

By far my favourite app these days is One Note. Unfortunately (or maybe fortunately, depending on your point of view) for Microsoft, they’ve made it available on other platforms for free.

The Facebook and Twitter apps are quite good; however, I now just use internet explorer. Remember that a Surface Pro or a Surface Pro 2
tablet, or even something like this Asus) is effectively running a full OS, so you’re no longer restricted to what you can find apps for.

So, what’s missing

While writing this, one of my big annoyances was that there isn’t a WordPress app. Admittedly there are less games available for Windows, but I suppose there’s only so many permutations of Angry Birds you can write. If you want more, why not consider contributing to these guys who are trying to bridge the gap?

Finally, if you would like to see a particular application on WinRT, leave a comment, or contact me directly.