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();
}
}
}