Click here to Skip to main content
15,894,740 members
Articles / Web Development / ASP.NET

Silverlight Alien Sokoban

Rate me:
Please Sign up or sign in to vote.
4.88/5 (59 votes)
11 Nov 200717 min read 133.9K   1K   138  
A fun Silverlight implementation of the game Sokoban. Contrasting Silverlight 1.1 and WPF, while showcasing some new features of C# 3.0, Expression Design, Expression Blend, and Visual Studio 2008.
/*
 * Stephane Tombeur
 * stephane.tombeur@gmail.com
 * 
 * source code may be used without restrictions
 * */

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Collections.Generic;

namespace Orpius.Sokoban.Controls
{
    public class KeyPressHandler
    {
        private Dictionary<int, string> keys = new Dictionary<int, string>();
        private readonly int OFFSET_SHIFT = 512;
        //private readonly int OFFSET_CTRL = 1024;

        public static readonly string BACKSPACE = "BackSpace";
        public static readonly string DELETE = "Delete";

        public KeyPressHandler()
        {
            _CreateDictionary();
        }

        public string GetKey(int key, bool shift)
        {
            int i = key;
            if (shift) i += OFFSET_SHIFT;

            if (keys.ContainsKey(i))
                return keys[i];
            else
                return "";
        }

        private void _CreateDictionary()
        {
            keys.Add(0, "KeyNone"); //
            keys.Add(1, BACKSPACE); //
            keys.Add(2, "\t"); keys.Add(2 +OFFSET_SHIFT, "\t"); //
            keys.Add(3, "\n"); keys.Add(3 +OFFSET_SHIFT, "\n"); //
            keys.Add(4, "Shift"); //
            keys.Add(5, "Ctrl"); //
            keys.Add(6, "Alt"); //
            keys.Add(7, "CapsLock"); //
            keys.Add(8, "Escape"); //
            keys.Add(9, " "); keys.Add(9 +OFFSET_SHIFT, " "); //
            keys.Add(10, "PageUp"); //
            keys.Add(11, "PageDown"); //
            keys.Add(12, "End"); //
            keys.Add(13, "Home"); //
            
            keys.Add(14, "Left"); //
            keys.Add(15, "Up"); //
            keys.Add(16, "Right"); //
            keys.Add(17, "Down"); //
            keys.Add(18, DELETE); //
            keys.Add(19, "Insert"); //

        
           //numbers
            keys.Add(20, "0"); keys.Add(20 + OFFSET_SHIFT, ")");
            keys.Add(21, "1"); keys.Add(21 + OFFSET_SHIFT, "!"); //
            keys.Add(22, "2"); keys.Add(22 + OFFSET_SHIFT, "@"); //
            keys.Add(23, "3"); keys.Add(23 + OFFSET_SHIFT, "#"); //
            keys.Add(24, "4"); keys.Add(24 + OFFSET_SHIFT, "$"); //
            keys.Add(25, "5"); keys.Add(25 + OFFSET_SHIFT, "%"); //
            keys.Add(26, "6"); keys.Add(26 + OFFSET_SHIFT, "^"); //
            keys.Add(27, "7"); keys.Add(27 + OFFSET_SHIFT, "&"); //
            keys.Add(28, "8"); keys.Add(28 + OFFSET_SHIFT, "*"); //
            keys.Add(29, "9"); keys.Add(29 + OFFSET_SHIFT, "("); //
        //letters
        for (int i=30;i<56;i++) 
        {
            keys.Add(i, Convert.ToChar(i+67).ToString());
            keys.Add(i + OFFSET_SHIFT, Convert.ToChar(i + 35).ToString());
        }//in range(30, 56): keys.Add(i] = chr(i+67), chr(i+35)
        
        //function keys
        for (int i=56;i<68;i++) 
        {
            keys.Add(i, "F" + (i - 55));
        }//for i in range(56, 68): keys.Add(i, "F"); //+str(i-55)
        
        //numpad numbers
        for (int i=68;i<78;i++) 
        {
            keys.Add(i, Convert.ToChar(i - 68).ToString());
            keys.Add(i + OFFSET_SHIFT, Convert.ToChar(i - 68).ToString());
        }//for i in range(68, 78): keys.Add(i] = str(i-68), str(i-68)
        
        keys.Add(78, "*"); keys.Add(78 +OFFSET_SHIFT, "*"); //
        keys.Add(79, "+"); keys.Add(79 +OFFSET_SHIFT, "+"); //
        keys.Add(80, "-"); keys.Add(80 +OFFSET_SHIFT, "-"); //
        keys.Add(81, "."); keys.Add(81 +OFFSET_SHIFT, "."); //
        keys.Add(82, "/"); keys.Add(82 +OFFSET_SHIFT, "/"); //

        //Treat numeric keypad as never num locked?
        //#keys.Add(69, "End"); //
        //#keys.Add(70, "Down"); //
        //#keys.Add(71, "PageDown"); //
        //#keys.Add(72, "Left"); //
        //#keys.Add(73, "Center"); //
        //#keys.Add(74, "Right"); //
        //#keys.Add(75, "Home"); //
        //#keys.Add(76, "Up"); //
        //#keys.Add(77, "PageUp"); //
        //#keys.Add(68, "Insert"); //
        //#keys.Add(81, "Delete"); //
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions