Friday, April 29, 2016

Stack Class in C#

So this week there is no more homework for my C# class since the semester is coming to an end and we have a final exam next week.

Since I was bored at work, I started thinking that we never created a stack for one of the class assignments. So I decided to create my own, which wasn't that hard since its essentially just a wrapper around an array.

I started to work on it yesterday for a couple hours and finished it this morning.

Here is my code:


Thanks for reading!

Wednesday, April 27, 2016

CS 2420 - Assignment 9 - Max Heap

This week's assignment is not as hard as the last one, we just have to create a heap data structure. We had the choice of either creating a min heap or max heap, so I went with Max since it made the most sense to me.

While the assignment its self wasn't too hard, it took me a while to do all the conditional logic behind the Sink() function.

Here is my code:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Assignment9
{
    class MaxHeap<T> : IEnumerable<T> where T : IComparable
    {
        private T[] UnderlyingArray;
        private int ElementCount = 0;
        private int OldCount = 0;
        private bool IsHeap = true;

        public MaxHeap(int starting_size)
        {
            UnderlyingArray = new T[starting_size];
        }

        public MaxHeap()
        {
            UnderlyingArray = new T[5];
        }

        public int Count
        {
            get
            {
                return ElementCount;
            }
        }

        public void Clear()
        {
            UnderlyingArray = new T[UnderlyingArray.Length];
        }

        public T this[int index]
        {
            get
            {
                return UnderlyingArray[index];
            }
        }

        public void Add(T new_value)
        {
            if (!IsHeap)
                BuildHeap();

            if (ElementCount == UnderlyingArray.Length)
                Resize();

            UnderlyingArray[ElementCount] = new_value;
            Swim();
            ElementCount++;
        }

        public T PopTop()
        {
            if (!IsHeap)
                BuildHeap();

            T max = UnderlyingArray[0];

            Swap(0, ElementCount - 1);
            UnderlyingArray[ElementCount - 1] = default(T);
            ElementCount--;
            Sink();
            
            return max;
        }

        public void Sort()
        {
            if (!IsHeap)
                BuildHeap();

            OldCount = ElementCount;

            for (int i = 0; i < OldCount; i++)
            {
                Swap(0, ElementCount - 1);
                ElementCount--;
                Sink();
            }

            IsHeap = false;
            ElementCount = OldCount;
        }

        public void BuildHeap()
        {
            if (IsHeap)
                return;

            int last_item = OldCount - 1;

            for (int index = 0; index < OldCount / 2; index++)
            {
                Swap(index, last_item);
                last_item--;
            }

            ElementCount = OldCount;
            IsHeap = true;
        }

        private void Swim()
        {
            int current_index = ElementCount;

            while (current_index != 0)
            {
                int parent_index = FindParent(current_index);

                if (UnderlyingArray[current_index].CompareTo(UnderlyingArray[parent_index]) == 0)
                    break;

                else if (UnderlyingArray[current_index].CompareTo(UnderlyingArray[parent_index]) > 0)
                    Swap(current_index, parent_index);

                current_index = parent_index;
            }
        }

        private void Swap(int first, int second)
        {
            T temp = UnderlyingArray[first];
            UnderlyingArray[first] = UnderlyingArray[second];
            UnderlyingArray[second] = temp;
        }

        private void Sink()
        {
            int current_index = 0;

            while (current_index < ElementCount)
            {
                int max_child_index = current_index;
                int left_child_index = FindLeft(current_index);
                int right_child_index = FindRight(current_index);

                if (left_child_index >= ElementCount)
                    break;

                if (right_child_index >= ElementCount)
                    max_child_index = left_child_index;

                if (right_child_index < ElementCount &&
                    UnderlyingArray[left_child_index].CompareTo(UnderlyingArray[right_child_index]) == 0)
                    max_child_index = left_child_index;

                if (right_child_index < ElementCount && 
                    UnderlyingArray[left_child_index].CompareTo(UnderlyingArray[right_child_index]) > 0)
                    max_child_index = left_child_index;

                if (right_child_index < ElementCount && 
                    UnderlyingArray[left_child_index].CompareTo(UnderlyingArray[right_child_index]) < 0)
                    max_child_index = right_child_index;

                if (UnderlyingArray[max_child_index].CompareTo(UnderlyingArray[current_index]) == 0)
                    break;

                if (UnderlyingArray[max_child_index].CompareTo(UnderlyingArray[current_index]) > 0)
                    Swap(current_index, max_child_index);

                current_index = max_child_index;
            }
        }

        private int FindParent(int current_index)
        {
            return (current_index - 1) / 2;
        }

        private int FindLeft(int current_index)
        {
            return (current_index * 2) + 1;
        }

        private int FindRight(int current_index)
        {
            return (current_index * 2) + 2;
        }

        private void Resize()
        {
            T[] new_array = new T[UnderlyingArray.Length * 2];

            for (int index = 0; index < UnderlyingArray.Length; index++)
                new_array[index] = UnderlyingArray[index];

            UnderlyingArray = new_array;
        }

        public IEnumerator<T> GetEnumerator()
        {
            for (int index = 0; index < ElementCount; index++)
            {
                T temp = UnderlyingArray[index];
                //Console.WriteLine("Current Value: {0}", temp);
                yield return temp;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}

That's all for now, thanks for reading!

Wednesday, April 20, 2016

CS 2420 - Assignment 8 - Components

This week's assignment is a bit more fun that usual since we are got to build a "game."

The point of this week's assignment to learn how to use components to build a game, instead of hard coding everything like we usually do. We also had a chance to review inheritance and understand how it works.

This time around there is too much code involved for me to put it here like I usually do, but if you are interested in looking at the code you can look here:

https://github.com/fushinoryuu/EAE2420/tree/master/DataStructuresAndSorts/Assignment8

I created these main class files: Component, Entity, Grid, Point, and Power Up.

The Grid was essentially a 2D array and everything ran through this file, it represented the map the player was going to play on. It also updated itself on the position of each enemy and the player and then displayed itself on the console.

The player and the enemies in the game all inherited from the Entity class and used a bunch of components to make each unique. For example, enemies had a component that allowed them to wrap around the Grid but the player was bound and couldn't go past the edges of the grid. To do this there was two components and I just had to make sure to give one to the player and the other to the enemies.

Power Ups are a type of component, but they don't inherit from the component class. I made it so they would modify the player's action. For example I had a Teleport Power Up. When they stepped on it, they would get randomly taken to an empty spot in the map.

I had a lot of fun with this assignment, but it took a little longer than I anticipated.

Anyways, that's all for now. Thanks for reading!

Tuesday, April 12, 2016

CS 2420 - Assignment 7 - Hash Table

This week we had to write a hash table, also known as a dictionary. Hash tables are really nice because you can index with more than just integers, and they are faster running times than a regular array or a linked list.

Here is my code:


That's all for now, thanks for reading!

Wednesday, April 6, 2016

CS 2420 - Assignment 6 - A Star

So this week we had to create a node graph and write our version of the A* (A Star) algorithm to find the shortest path. Here is the map we were given:



The map is not to scale, here are the coordinates of each node: 

A: -19, 11        B: -13, 13      C: 4, 14          D: -4, 12      E: -8, 3           F: -18, 1
G: -12, -8        H: 12, -9         I: -18, -11       J: -4, -11     K: -12, -14     L: 2, -18
M: 18, -13       N: 4, -9          O: 22, 11        P: 18, 3

I have seen multiple implementations of A* around the web, but they all use a matrix and it can get really complicated really quickly. I like this implementation better because its simple, but it works.

Here is my code:


That's all for now, thanks for reading!