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.

/* This is still very much untested pseudocode.
    ToDo's: 
        - Make tenary one liners
        - Test
        - Profit
 */

using System;
using System.Collections.Generic;

namespace Tactics_Sharp {
    internal class Grid {
        /* Grid: Fake multi-array       
        */
        private Int32 MAX_ROW; // Only useful for if statements
        private Int32 MAX_COL; // Useful for indexing && if statements
        private Point[] _arr;  // Fake multi-array of point objects
        private Point _ptr;    // Pointer var

        public class Point {
            /* Point: Container class             
            */
            [Flags]
            public enum PointState { Suspended = 0x0, Active = 0x1 }
            private Int32 _row, _col;
            private Object _val;
            private PointState _state;

            // Default constructor values:
            //  - _row = -1
            //  - _col = -1
            //  - _val = null
            //  - _state = new PointState()
            public Point() { this._row = -1; this._col = -1; this._val = null; this._state = new PointState(); }
            public Point(Object _val) { this._row = -1; this._col = -1; this._val = _val; this._state = new PointState(); }
            public Point(PointState _state) { this._row = -1; this._col = -1; this._val = null; this._state = _state; }
            public Point(Int32 _row, Int32 _col) { this._row = _row; this._col = _col; this._val = null; this._state = new PointState(); }
            public Point(Int32 _row, Int32 _col, Object _val) { this._row = _row; this._col = _col; this._val = _val; this._state = new PointState(); }
            public Point(Int32 _row, Int32 _col, PointState _state) { this._row = _row; this._col = _col; this._val = null; this._state = _state; }
            public Point(Int32 _row, Int32 _col, Object _val, PointState _state) { this._row = _row; this._col = _col; this._val = _val; this._state = _state; }

            // Setters-------------------------------------------------------
            public void setValue(Object _val)          { this._val = _val; }
            public void setRow(Int32 _row)             { this._row = _row; }
            public void setCol(Int32 _col)             { this._col = _col; }
            public void setPos(Int32 _row, Int32 _col) { this._row = _row; this._col = _col; }
            public void delValue()                     { this._val = null; }
            public void activateState() {
                if (PointState.Active != (this._state & PointState.Active))
                    this._state = this._state | PointState.Active;
            }
            public void suspendState() {
                if (PointState.Active == (this._state & PointState.Active))
                    this._state = this._state ^ PointState.Active;
            }
            // Getters------------------------------------------------------------------------
            public Object      getValue() { return this._val; }
            public PointState  getState() { return this._state; }
            public Int32       getRow()   { return this._row; }
            public Int32       getCol()   { return this._col; }
            public List<Int32> getPos()   { return new List<Int32> { this._row, this._col }; }

        }// !Point

        public Grid() { this._arr = null; this.MAX_ROW = -1; this.MAX_COL = -1; this._ptr = null;}
        public Grid(Int32 _row, Int32 _col) {
            this.MAX_ROW = _row; this.MAX_COL = _col;
            this._arr = new Point[this.MAX_ROW * this.MAX_COL];
            //ToDo: Update point row/col values
            /* Untested
             for(Int32 x = 0; x < this.MAX_ROW; x++)
                for(Int32 y = 0; x < this.MAX_COL; y++)
                    this._arr[ x * this.MAX_COL + y ].setPos(x,y);
             */
        }
        public virtual void _init(Int32 _row, Int32 _col) {
            this.MAX_ROW = _row; this.MAX_COL = _col;
            this._arr = new Point[this.MAX_ROW * this.MAX_COL];
            //ToDo: Update point row/col values
            /* Untested
             for(Int32 x = 0; x < this.MAX_ROW; x++)
                for(Int32 y = 0; x < this.MAX_COL; y++)
                    this._arr[ x * this.MAX_COL + y ].setPos(x,y);
             */
        }
        // Getters------------------------------------------------------------------------------
        public virtual Int32  getMAX_ROW()       { return this.MAX_ROW; }
        public virtual Int32  getMAX_COL()       { return this.MAX_COL; }
        public virtual Point  getPointer()       { return this._ptr; }
        public virtual Object getPointerValue()  { return this._ptr.getValue(); }
        public virtual Object getPointerState()  { return this._ptr.getState(); }
        public virtual Object getPointValue(Int32 _row, Int32 _col) {
            if ((_row > 0 && _row < this.MAX_ROW) || (_col > 0 && _col < this.MAX_COL))
                return this._arr[_row * this.MAX_COL + _col];
            else
                return null;  
        }
        public virtual Object getPointState(Int32 _row, Int32 _col) {
            if ((_row > 0 && _row < this.MAX_ROW) || (_col > 0 && _col < this.MAX_COL))
                return this._arr[_row * this.MAX_COL + _col].getState();
            else
                return null;
        }
        // Setters------------------------------------------------------------------------------
        public virtual void setPointer(Point _ptr) { this._ptr = _ptr; }
        public virtual void setPointerValue(Object _val) {
            if (this._ptr != null)
                this._ptr.setValue(_val);
        }
        public virtual void setPointValue(Object _val, Int32 _row, Int32 _col) {
            if ((_row > 0 && _row < this.MAX_ROW) || (_col > 0 && _col < this.MAX_COL))
                this._arr[_row * this.MAX_COL + _col].setValue(_val);
            else
                Console.Error.WriteLine("Invaild cordinates!");
        }
        public virtual void movePointer(Int32 _row, Int32 _col) {
            if ((_row > 0 && _row < this.MAX_ROW) || (_col > 0 && _col < this.MAX_COL))
                this._ptr = this._arr[_row * this.MAX_COL + _col];
            else
                this._ptr = null; 
        }
        // Debugers------------------------------------------------------------------------------
        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();
        }
    } // !Grid

    // - This will get a little wierd and hacky.
    internal sealed class Team : Grid {
        private List<Character> teammates = new List<Character>();
        public Team() { }
        public void addTeammate(Character _char) { teammates.Add(_char); }
        public void removeTeammate(Character _char) {
            if (teammates.Contains(_char))
                teammates.Remove(_char);
            else
                Console.Error.WriteLine("Character not found!");
        }
        
        // Getters---------------------------------------------------------------------------------------------
        public override Int32 getMAX_ROW() { return base.getMAX_ROW(); }
        public override Int32 getMAX_COL() { return base.getMAX_COL(); }
        public override Point getPointer() { return base.getPointer(); }
        public override Object getPointerValue() { return base.getPointerValue(); }
        public override Object getPointerState() { return base.getPointerState(); }
        public override Object getPointValue(Int32 _row, Int32 _col) { return base.getPointValue(_row,_col); }
        public override Object getPointState(Int32 _row, Int32 _col) { return base.getPointState(_row, _col); }
        public List<Character> getTeammates()  { return this.teammates; }
        public Character getCharacter(Int32 i) { return this.teammates[i];}

        // Setters----------------------------------------------------------------------------------------------
        public override void setPointer(Point _ptr) { base.setPointer(_ptr) }
        public override void setPointerValue(Object _val) { base.setPointerValue(_val); }
        public override void setPointValue(Object _val, Int32 _row, Int32 _col) { base.setPointValue(_val, _row, _col); }
        public override void movePointer(Int32 _row, Int32 _col) { base.movePointer(_row, _col); }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s