Tag Archives: C (Sharp)

Functional C#

Starting to like C# a bit more.  This SO post is a must read, the top rated answer has some amazing info and links.  Going to have some fun playing around with expression trees.

C# Lambda Expression,Why Should I Use This?

This YouTube video is also extremely well done.
[youtube:http://www.youtube.com/watch?v=8o0O-vBS8W0&feature=related%5D

Also an another good video.
[youtube:http://www.youtube.com/watch?v=HG48kVzpb7U&feature=related%5D

/R/Programming Reposts

These were intersting reads today.

Exploring Code Canvas

http://blogs.msdn.com/b/kaelr/archive/2012/04/14/exploring-code-canvas.aspx

 

Why Erlang?

http://smyck.net/2012/04/22/why-erlang/

 

Node.js Is Bad Ass Rock Star Technology

COM Structured Storage from .NET – Tutorial by Harry Fairhead

This was an interesting read.  I took some time looking into working with COM interfaces this morning and found this article.

 

COM Structure Storage

http://www.developerfusion.com/article/84406/com-structured-storage-from-net/

Working With Directories in C#

I need to do a directory walk and decided to try coding it in C#.
Continue reading

A Moore Machine

I think someone found my blog today by Googling for a Moore machine example in C#, lol.  I will admit a Moore machine is the best kind of machine, no offense Turing and f%*k Mealy.  As for explanation of what’s a Moore machine your SOL, even I don’t have the time or energy to try present this one but here are few resources to get you going.

Continue reading

SlimDX Resources

I just took a quick look at SlimDX.  Here are two blogs about on using SlimDX in C#

Continue reading

Tactics Sharp Quick Grid Redesign

I spent about a hour redesigning the grid class and here is what I got so far. I didn’t even try compiling it yet so reader be ware.
Continue reading

Lambda Expressions && Dictionaries && Arrays

I’ve worked on the grid class of Tactics Sharp a little bit. Here is the source, it’s still incomplete. Also here is a good MSDN article on Lambda Expressions in C#. http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx

Grid class source

using System;
using System.Collections.Generic;
/*
    Note this is still under development, contains errors and missing functions.
 */
namespace Tactics_Sharp {
    internal class Grid {
        // Grid pointer object
        public class Point{
            private Int32 _row, _col; 
            public Point()                       { this._row = -1; this._col = -1; }      
            public Point(Int32 _row, Int32 _col) { this._row = _row; this._col = _col; }  
            public void setRow(Int32 _row)       { this._row = _row; }  
            public void setCol(Int32 _col)       { this._col = _col; }
            public Int32 getRow()                { return this._row; }
            public Int32 getCol()                { return this._col; }
        }

        private Int32   MAX_ROW, MAX_COL;
        private Int32[] _arr;
        private Point _ptr;

        public Grid() { this._arr = null; this.MAX_ROW = -1; this.MAX_COL = -1; }
        public virtual void init(Int32 _row, Int32 _col) {
            this.MAX_ROW = _row; this.MAX_COL = _col;
            this._arr = new Int32[this.MAX_ROW * this.MAX_COL];
            this._ptr = new Point();
        }
        public virtual Int32 getMAX_ROW() { return this.MAX_ROW; }
        public virtual Int32 getMAX_COL() { return this.MAX_COL; }

        //ToDo: Make tenary check
        public virtual void movePointer(Int32 row, Int32 col) { this._ptr.setRow(row); this._ptr.setCol(col); }
        public virtual Int32 getValue() {
            if( (this._ptr.getRow() >= 0 && this._ptr.getCol() >= 0) || (this._ptr.getRow() < this.MAX_ROW && this._ptr.getCol() < this.MAX_COL) )
                return this._arr[this._ptr.getRow() * this.MAX_COL + this._ptr.getCol()]; 
            else
                return -1; // !Error
        }
        public virtual void setValue(Int32 _val) {
            if ( (this._ptr.getRow() >= 0 && this._ptr.getCol() >= 0) || (this._ptr.getRow() < this.MAX_ROW && this._ptr.getCol() < this.MAX_COL) )
                if ( _val >= 0 && _val < this.MAX_ROW && _val < this.MAX_ROW )
                    this._arr[this._ptr.getRow() * this.MAX_COL + this._ptr.getCol()] = _val;            
        }
        public virtual void print() {
            Console.WriteLine("Multi-Array Contents");
            for(int _row = 0; _row < this.MAX_ROW; _row++) {
                Console.WriteLine();
                for(int _col = 0; _col < this.MAX_COL; _col++)
                    Console.Write(this._arr[_row * this.MAX_COL + _col]);
            }
            Console.WriteLine();
        }
    }

    internal sealed class GameBoard : Grid {
        /*
            Some of you might have a few questions about the dictionary object occupants, like why need array if 
            you can have a dictionary of characters and points.  Couldn't you just use the point objects to keep  
            track and manipulate the character's position.  Yeah, probably but where's the point in that.
         */
        public Dictionary<Character, Point> occupants = new Dictionary<Character,Point>();
        public GameBoard(Int32 _row, Int32 _col) { base.init(_row, _col); }

        public override void movePointer(Int32 _row, Int32 _col) { base.movePointer(_row, _col); }
        public override void setValue(int _val)                  { base.setValue(_val); }
        public override Int32 getValue()                         { return base.getValue(); }
        public void add(Character _char)                         { this.occupants.Add( _char, new Point() ); }
        public void add(Character _char, Int32 _row, Int32 _col) {
            if ((_row >= 0 && _col >= 0) || (_row < base.getMAX_ROW() && _col < base.getMAX_COL()))
                this.occupants.Add(_char, new Point(_row, _col));
            else
                this.occupants.Add(_char, new Point(_row, _col)); 
        }
       
        public void populate() {
            foreach (Character _char in this.occupants.Keys){
                base.movePointer(this.occupants[_char].getRow(), this.occupants[_char].getCol() );
                base.setValue(_char._ID);
            }
        }

        public void test() {
            base.print();
            foreach( Character var in this.occupants.Keys )
                Console.WriteLine(var.getName() +" @ [" + occupants[var].getRow() + "," + occupants[var].getCol() +"]" );
        }
             
    }
}

Character class, only added an id value.

using System;
using System.Collections.Generic;

namespace Tactics_Sharp {
    internal class Character : IComparable {

        [Flags] //-State of character.
        public enum CharState { Normal = 0x0, Death = 0x1, Asleep = 0x2, Burn = 0x4, Frozen = 0x8, Paralyze = 0x10, Poison = 0x20, Confuse = 0x40 }

        // - Character's Vitals && Skills
        private Dictionary<String, Int32> _skills;
        private Dictionary<String, Int32> _vitals;
        private CharState _state;
        public String _name;
        public Int32 _ID;

        public Character() {
            this._state = CharState.Normal;
            this._skills = new Dictionary<String, Int32>() { 
                { "Punch", 0 }, { "Weapon", 0 }, { "Evade", 0}, { "Steal", 0 }, 
                { "Block", 0 }, { "Fire", 0 }, { "Lighting", 0 }, { "Heal", 0 },  
                { "Bite", 0 }, { "Claw", 0 } };
            this._vitals = new Dictionary<String, Int32>() { { "HP", 0 }, { "MP", 0 }, { "LVL", 0 } };
            this._ID = -1;
        }

        // - Virtual Derived Classes
        public virtual Int32                     CompareTo(Object _obj) { return this.CompareTo(_obj); }
        public virtual String                    getName() { return this._name;  }
        public virtual void                      setName(String _name) { this._name = _name; }
        public virtual Dictionary<String, Int32> getVitals() { return this._vitals; }
        public virtual Dictionary<String, Int32> getSkills() { return this._skills; }
        public virtual void                      incrementSkill(String key) { this._skills[key]++; }
        public virtual void                      incrementVitals(String key) { this._vitals[key]++; }
        public virtual Int32                     indexSkill(String key) { return this._skills[key]; }
        public virtual Int32                     indexVital(String key) { return this._vitals[key]; }
        public virtual CharState                 getState() { return this._state; }

        public virtual void setSkill(String key, Int32 val) {
            if (val >= 0 && val <= 100)
                this._skills[key] = val;
        }
        public virtual void setVital(String key, Int32 val) {
            if (val >= 0)
                this._vitals[key] = val;
        }
        public virtual void setState(CharState _state) {
            if (_state != (this._state & _state))  // - Checks if class is deactive  
                this._state = this._state | _state;  // - Adds state
        }
        public virtual void removeState(CharState _state) {
            if (_state == (this._state & _state))   // - Checks if class is active
                this._state = this._state ^ _state; // - Removes state
        }

        // Human Character Class
        internal sealed class Human : Character, IComparable {
            [Flags]
            public enum CharClass { Squire = 0x0, Knight = 0x1, Monk = 0x2, Mage = 0x4, Archer = 0x8, Theif = 0x10 }
            public CharClass _class;
            public Human(String _name, Int32 _ID) { base._name = _name; base._ID = _ID; this._class = new CharClass(); }

            // Locals Functions
            public void promote(CharClass _class) {
                if (_class != (this._class & _class))    // - Checks if class is deactive 
                    this._class = this._class | _class;  // - Adds class
            }
            public void demote(CharClass _class) {
                if (_class == (this._class & _class))   // - Checks if class is active
                    this._class = this._class ^ _class; // - Removes class
            }

            // Base Class Functions
            public override Int32 CompareTo(Object _obj) { return base.CompareTo(_obj); }
            public override string getName() { return base.getName(); }
            public override Dictionary<String, Int32> getSkills() { return base.getSkills(); }
            public override Dictionary<String, Int32> getVitals() { return base.getVitals(); }
            public override void incrementSkill(String key) { base.incrementSkill(key); }
            public override void incrementVitals(String key) { base.incrementVitals(key); }
            public override Int32 indexSkill(String key) { return base.indexSkill(key); }
            public override Int32 indexVital(String key) { return base.indexVital(key); }
            public override void setSkill(String key, Int32 val) { base.setSkill(key, val); }
            public override void setVital(String key, Int32 val) { base.setVital(key, val); }
        }

        // Dragon Character Class
        internal sealed class Dragon : Character, IComparable {
            [Flags]
            public enum CharClass { Brainfuck = 0x0, Packrat = 0x1, ANTLR = 0x2, PLY = 0x4, SWIG = 0x8 }
            public CharClass _class;

            public Dragon(String _name, Int32 _ID) { base._name = _name; base._ID = _ID; this._class = new CharClass(); }

            // Locals Functions
            public void promote(CharClass _class) {
                if (_class != (this._class & _class))      // - Checks if class is deactive 
                    this._class = this._class | _class; }  // - Adds class
            
            public void demote(CharClass _class) {
                if (_class == (this._class & _class))      // - Checks if class is active
                    this._class = this._class ^ _class; }  // - Removes class
            
            // Base Class Functions
            public override Int32                     CompareTo(Object _obj) { return base.CompareTo(_obj); }
            public override string                    getName() { return base.getName(); }
            public override Dictionary<String, Int32> getSkills() { return base.getSkills(); }
            public override Dictionary<String, Int32> getVitals() { return base.getVitals(); }
            public override void                      incrementSkill(String key) { base.incrementSkill(key); }
            public override void                      incrementVitals(String key) { base.incrementVitals(key); }
            public override Int32                     indexSkill(String key) { return base.indexSkill(key); }
            public override Int32                     indexVital(String key) { return base.indexVital(key); }
            public override void                      setSkill(String key, Int32 val) { base.setSkill(key, val); }
            public override void                      setVital(String key, Int32 val) { base.setVital(key, val); }
        }
    }
}

Test class with a lambda expression:

using System;
using System.Collections.Generic;

namespace Tactics_Sharp {
    class Run {
        public delegate void StandardOutput();

        static void Main(string[] args) {
            GameBoard board = new GameBoard(5, 5);
            Character.Human Bit = new Character.Human("Bit Diddler", 1);
            Character.Dragon Aho = new Character.Dragon("Aho", 9);

            StandardOutput SO = () => {
                board.add(Bit, 0, 0);
                board.add(Aho, 0, 1);
                board.populate();
                board.test();
                Console.ReadLine();
            };
            SO();
        }
    }
}

Tactics Sharp – Character Design Update

Still haven’t figured out how to get dictionary iteration working smoothly yet but here is the source code. I added back the character classes and state enums. I’m skipping the dictionary issues for now and moving on. Next I’m either going back to the grid to track and move around the characters or start working out attacking events. Also I’ll try to throw in a lambda function where I can.
Continue reading

C# – Derived Classes && !Dictionaries

Spent some time tonight cleaning up the character class from Tactics Sharp. When I have some time later I’ll try to push it everything I have out to github. As for new additions I, finally implemented the character class as a base class and made to two derived classes, human and dragon. Also, I added two dictionaries but I’m not getting the iteration process right. I’m sure the Python gods are looking down at me now.

I left out my bitwise operations on bit flags. Check one of my older posts if you want to see how to do that.
Continue reading

Tactics Sharp – Quick Redesign

Alright I feel kinda stupid now. I didn’t realize I could use an enum like this. Here is what my bitwise operation functions look like now.

        public void promote(CharClass _class)
        {
            if (_class != (this._class & _class))  // - Checks if class is deactive  
                this._class = this._class | _class;  // - Adds state
        }

        public void demote(CharClass _class)
        {
            if (_class == (this._class & _class))   // - Checks if class is active
                this._class = this._class ^ _class; // - Removes state
           
        }

Continue reading

Tactics Sharp – Game Board Design

I quickly design the game board, here is what I got so far.

Continue reading

Tactics Sharp – Character Design

Just spent around 45 minutes playing around with some code.  Here is what I came up with, Bit Diddler would be proud.

Continue reading

C# State Machine Example

I found this example trolling over at StackOverflow. It’s a basic state machine but I really like how it’s designed, plus the author Juliet Rosenthal added a diagram which is always nice.
http://stackoverflow.com/questions/5923767/simple-state-machine-example-in-c
Continue reading

C# XML Serialization

Here is a simple example on how to serialized objects to XML in C#.
Continue reading