Friday, 21 March 2014

No longer posting on this blog.

I have made a final decision not to post on the blogger platform anymore. As previosly posted before, I have ported this blog to Wordpress and I have many new posts there. Also, it makes for better SEO methods and better displayment of source code, media, and other aspects.

So again, if you continue to read this blog, thats fine and all, but be aware that I no longer post on here and many articles on here have been updated on the new site.

Bits and Pieces of Code: http://simpledevcode.wordpress.com

Thursday, 6 March 2014

Ported to wordpress

I've recently ported this blog to wordpress.com, I feel that it would a somewhat better platform, please check it out: http://simpledevcode.wordpress.com/

Thank you.

Monday, 3 March 2014

A good C++ book for all levels

I've learned C++ on my spare time and I will soon post more content on C++, and I've been doing so using a great book by Alex Allain called "Jumping Into C++". Its great for both beginners and experts as it provides clear and simple examples of C++ code and each chapter builds upon concepts learned. Give it a try:

 http://www.cprogramming.com/c++book/?inl=sb



http://imgv2-2.scribdassets.com/img/word_document/106127644/164x212/1b95d91b97/1387593515

Saturday, 1 March 2014

Cards library for creating card games in C#/.Net

I found a really good library to use if you wanted to create a card game. Its a cards library and its meant for use in C#.Net, this download also includes a sample game that uses the library, which I found really helpful:  http://playingcards.codeplex.com/


Saturday, 22 February 2014

Using Stacks to evaluate prefix notation expressions (Polish notation)

Prefix notation (for those that do not know), is a way of writing a mathematical expression without the use of parenthesis or brackets. Also known as "Polish notation" (which was created by Jan Ɓukasiewicz to simplify sentential logic), it provides an easy way for computers to evaluate order of operations expressions without the use of brackets.

To start, a prefix notation example is “+34″, which would evaluate to 7 because the expression is 3+4, just in polish notation. Accordingly, there are a lot more examples of polish notation, and for the sample code posted, the algorithm will evaluate the prefix notation from a string array. In pseudo-code, the algorithm uses a stack to push and pop values in the expression and then evaluate according to the operator in the expression:

Code in C#:

class Program
    {
        static void Main(string[] args)
        {
            Stack<int> polishNotationStack = new Stack<int>();//a stack of type int
            //the expression will be a string array
            string[] expression = { "/", "*", "5", "+", "4", "3", "2" };//first, a prefix expression
            for (int i = 0; i < expression.Length; i++)//we write the prefix expression
            {
                Console.Write(expression[i]);
            }
            int result;//a result to push onto the stack after an operation was done
           
            Console.WriteLine();//newline space
            Array.Reverse(expression);
           //we reverse the expression to read in the characters from right to left
           
            int n;
            foreach (string c in expression)//for each string characcter in the array
            {
                if (int.TryParse(c, out n))//if the character can be converted to a number (operand)
                {
                    polishNotationStack.Push(n);//push the number onto the stack
                }
                if (c == "+")//handling of operators
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x + y;//evaluate the values popped from the stack
                    polishNotationStack.Push(result);//push current result onto the stack
                }
                if (c == "-")
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x - y;
                    polishNotationStack.Push(result);
                }
                if (c == "*")
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x * y;
                    polishNotationStack.Push(result);
                }
                if (c == "/")
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x / y;
                    polishNotationStack.Push(result);
                }
               
            }
            /*write the final result of the expression,
             * which is at the top of the stack, so we use Peek()*/
            Console.WriteLine("result of expression: {0}", polishNotationStack.Peek());

            //Function();
            Console.ReadLine();
        }

Sunday, 9 February 2014

Learn C, Then Learn Computer Sci...

This was a really good article written by a CS student. In it he talks about how crucial it is to learn the meaning behind the code, then learning theory or concepts. Have a look, its a short read: http://qrohlf.com/posts/learn-to-code-then-learn-cs/http://qrohlf.com/posts/learn-to-code-then-learn-cs/

Thursday, 6 February 2014

Drawing by mouse on a PictureBox (Freehand drawing)

Brief overview of GDI+: Graphics Device Interface + (GDI+) is a graphical subsystem of Windows that consists of an application programming interface (API) to display graphics and formatted text on both video display and printer.
GDI+ acts as an intermediate layer between applications and device drivers for rendering two-dimensional graphics, images and text.



Suppose we wanted to create a sort of painting/freehand writing program that uses the mouse to draw. We can do this in .Net by first creating a Windows Forms application and then making a picturebox like so (adding a button to clear the picturebox is optional):
The code (in C#) is relatively simple to write. Theres many ways to accomplish freehand drawing by mouse, so this is just one way. The algorithm goes like this:
  1. First, create the event handlers for the picture box: MouseDown, MouseMove, & MouseUp
  2. Create a previous Point object to store the current mouse position so that the any previous point will equal whatever position the mouse is on the picturebox (x,y) coordinates
  3. Create a boolean to detect whether the mouse button is currently pressed
  4. Create a Bitmap so that you have something to write/paint on if there is no existing Image/Bitmap on the picturebox
  5. Once you've created a bitmap, create a Graphics object to draw with, we will use the DrawLine() function, it takes a Pen object, a starting Point, and an ending Point
The sample code in C#:

public partial class Form1 : Form

    {

        Point lastPoint = Point.Empty;//Point.Empty represents null for a Point object

        bool isMouseDown = new Boolean();

        public Form1()

        {

            InitializeComponent();

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

        {

            lastPoint = e.Location;//we assign the lastPoint to the current mouse position

            isMouseDown = true;//we set to true because our mouse button is down (clicked)

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

        {

            if (isMouseDown == true)//check to see if the mouse button is down

            {

                if (lastPoint != null)//if our last point is not null, which in this case we have assigned above

                {

                    if (pictureBox1.Image == null)

                    {

                        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

                        pictureBox1.Image = bmp;

                    }

                    using (Graphics g = Graphics.FromImage(pictureBox1.Image))

                    {//we need to create a Graphics object to draw on the picture box, its our main tool

                 //when making a Pen object, you can just give it color only or give it color and pen size

                        g.DrawLine(new Pen(Color.Black, 2), lastPoint, e.Location);
                        g.SmoothingMode = SmoothingMode.AntiAliasing;
                   //this is to give the drawing a more smoother, less sharper look

                    }

                    pictureBox1.Invalidate();//refreshes the picturebox

                    lastPoint = e.Location;//keep assigning the lastPoint to the current mouse position

                }

            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)

        {

            isMouseDown = false;

            lastPoint = Point.Empty;

           //set the previous point back to null if the user lets go of the mouse button

        }

        private void clearButton_Click(object sender, EventArgs e)//our clearing button

        {

            if (pictureBox1.Image != null)

            {

                pictureBox1.Image = null;

                Invalidate();

            }

        }

    }