Monday, November 30, 2009

Tokenizing Example: Square

Well, I did it. I created a tokenizing program, which tokeninzes a square. I had a reference file, but the thing was coded so poorly that I didn't even bother reading or understanding the darn thing. Here is the source code to the terribly written file: http://cplusplus.com/files/fraction.zip


This program took me about 30 minutes to write, and 2 hours to debug. I had multiple bugs, and wrote the loop that creates the "middle" of the square about 4 times before I finally created something clean looking and easy to read. A common error I made, was during the draw function, I would often reset the value of a or b to the incorrect corresponding variables width and height. If I were to label my variables as something extremely intuitive, I could have saved myself a lot of trouble :P


Here is the code for my square tokenizing program, it should look nothing like my reference (which I didn't ever reference), and be easier to read:



/*Tokenizing Square*/

#include<iostream>
using namespace std;

#define n '\n';

void InputData (int &height, int &width)
{
//Stores data input by the user.
    cout << "Enter in the height of your square: ";
    cin >> height;
    cout << n;
    cout << "Enter in the width of your square: ";
    cin >> width;
    cout << n;
}

void Draw (int &height, int &width)
{
/*Draws the square. This function operates using the c variable to control wich
of the loops fires.*/
    int a, b, c = 0;
  
    a = height;
    b = width;
    c = 3;
  
/*The following two if statements check to make sure the dimensions aren't too
high.*/
  
    if(height > 30)
    {
        cout << "Too large of a height, enter in something smaller!";
        cout << n;
        cout << n;
        c = 0;
    }
  
    if(width > 30)
    {
        cout << "Too large of a width, enter in something smaller!";
        cout << n;
        cout << b;
        c = 0;
    }
  
    while (b > 0, c == 3)
    {
        if (b == width)
        {
            cout << " ";
        }
      
        cout << "__";
        b--;
      
        if (b == 0)
        {
            cout << n;
            c = 2;
            b = width;
        }
    }
  
    while (a > 0, c == 2)
    {
        cout << "|";
      
        for (b = width; b > 0; b--)
        {
            cout << "__";
        }
      
        cout << "|";
        cout << n;
        a--;
        b = width;
      
        if (a == 0)
        {
            c = 1;
            a = height;
        }
    }
  
    while (b > 0, c == 1)
    {
        if (b == width)
        {
            cout << "|";
        }
      
        cout << "__";
        b--;
      
        if (b == 0)
        {
            c = 0;
            b = width;
            cout << "|";
            cout << n;
            cout << n;
        }
    }
}

int main()
{
//This is the main loop, which is a do while loop.
    char answer;
  
    do
    {
        int height, width, a, b;
      
        InputData(height, width);
        Draw(height, width);
  
        cout << "Would you like to draw another square (y/n) ";
        cin >> answer;
        cout << n;
  
        }while ((answer == 'y') || (answer == 'Y'));
      
    return 0;
}


I also read quite a few different blogs and featured articles at http://www.gamasutra.com/, none of which are directly relevant to basic programming, such as what I am currently doing.

However, I want to repost this link: https://www.digipen.edu/uploads/media/digipen_podclass_issue_37.mp3

That is a podclass made by DigiPen. It is an interview of two programmers, and is extremely interesting and relevant to anyone aspiring to become a professional programmer for the games industry. Right click on the link, and select "Save target as", or "Save link as".

Tomorrow, I will be going back to school. I plan to still do a little coding, so expect to have another blog post to read.

Sunday, November 29, 2009

Fraction Addition

Today, I created a program that adds two fractions together, and then reduces them. This took me about 2 hours to complete, and about 25 minutes to write. Yep, that means I sat there and debugged the program for over an hour and a half. Sheesh, I hope this isn't what programming is always like ;)


The way this program works, is I set up the main function to call all of the other functions. However, in function Addition, I call the reduce function. This is because I needed to pass on the data from the addition function directly into the reduce function. If I were to call the reduce fraction from within the main function, the private data of function addition wouldn't have been passed onto being reduced. Here is my code:



#include<iostream.h>

void InputData (int &Num,int &Denom,int &Num2,int &Denom2)
{

//This function takes data from the user.

    cout << "Enter the numerator of the first fraction." << '\n';
    cin >> Num;
    cout << "Enter the denominator of the first fraction." << '\n';
    cin >> Denom;
    cout << "Enter the numerator of the second fraction." << '\n';
    cin >> Num2;
    cout << "Enter the denominator of the second fraction." << '\n';
    cin >> Denom2;
    cout << '\n';
}

void Reduce (int &Num,int &Denom,int &Num2,int &Denom2)
{

//This function reduces both fractions, before adding them.

    int a, b, c, d, i, j = 0;
    
    a = Num;
    b = Denom;
    c = Num2;
    d = Denom2;
    
    for (i = a * b; i > 1; i--)
    {
        if ((a % i == 0) && (b % i == 0))
        {
            a /= i;
            b /= i;
        }
    }
    
    for (j = c * d; j > 1; j--)
    {
        if ((c % j == 0) && (d % j == 0))
        {
            c /= j;
            d /= j;
        }
    }
    Num = a;
    Denom = b;
    Num2 = c;
    Denom2 = d;
}

void Reduce (int &Num,int &Denom)
{

//This function reduces the added fractions.

    int a = 0;
    int b = 0;
    int i = 0;
    
    a = Num;
    b = Denom;
    
    for (i = a * b; i > 1; i--)
    {
        if ((a % i == 0) && (b % i == 0))
        {
            a /= i;
            b /= i;
        }
    }
    Num = a;
    Denom = b;
}

void Addition (int &Num,int &Denom,int &Num2,int &Denom2)
{

//This adds both of the raw fractions. In order to do this, you need to make sure both the numerator and denominator are equal.

    if (Denom != Denom2)
    {
        Num = Num * Denom2;
        Num2 = Num2 * Denom;
        Denom = Denom * Denom2;
        Denom2 = Denom2 * Denom;
        Num = Num + Num2;
    }
    else
    {
        Num = Num + Num2;
    }
    Reduce (Num, Denom);
}

void OutputData (int &Num,int &Denom)
{

//This function simply displays the final fraction.

    cout << Num << "/" << Denom << '\n';
}

int main ()
{

//This is the main loop, it will keep firing as long as char a is equal to y or Y.

    char a;
    
    do{
        int Num, Denom, Num2, Denom2 = 0;
    
        InputData (Num, Denom, Num2, Denom2);
        Reduce (Num, Denom, Num2, Denom2);
        Addition (Num, Denom, Num2, Denom2);
        OutputData (Num, Denom);
    
        cout << "Would you like to add two more fractions (y/n)?" << '\n';
        cin >> a;
    }while ((a == 'y') || (a == 'Y'));
    
    return 0;
}


And there she is! The program should work without the & symbol in the functions' parameters, my reference code used it, so so did I :)

While creating this program, I used this source as a reference, although, I made sure to only look at it when I completely forgot how to do something, and throughout when I was debugging. Here is the link to my reference source code: http://cplusplus.com/files/fraction.zip

Speaking of that bug, take a look at what I had wrong! Look up in my source code, you see that red code? That is what I had mixed up, here is what I had when my code was bugged:





    Num = a;
    Denom = b;
    Num2 = a;
    Denom2 = b;

Pretty ridiculous eh? That simple, tiny, infinitesimal error cost me over an hour and a half of debugging!

That is what I completed for the day. While creating that, I gained understanding of the % (modulo) symbol, and one of it's practical uses. I also gained an understanding of a very useful loop; the do while loop. The do while loop, which is what my entire main function was constructed of, will complete it's task, and then complete a comparison. So basically, the while loop makes the comparison before it executes code, and the do while loop executes its code before the comparison.

Tomorrow, I shall work on this source code: http://cplusplus.com/files/rectangle.zip

That code demonstrates creating a square with tokenizing. His code looks pretty ugly, hopefully I can come up with something much nicer, using his code as a reference.


Here's an interesting read I'll hopefully finish up before the end of tonight! It's about game engines. I doubt I'll actually put any of this into practice any time soon, but it's nice to read things like this: http://www.gamasutra.com/view/feature/4199/book_excerpt_game_engine_.php

Saturday, November 28, 2009

Gaining Familiarity with C++

Today I decided to solidify my familiarity of C++ by creating a couple rather useless, but working programs from scratch.

The first one I created solves for the hypotenuse vector of a right triangle, using the Pythagorean Theorem. This program is rather straightforward, and took little to no time to write.


// Hypotenuse

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int main()
{
    int x;   //position of x-axis
    int y;   //position of y-axis
    int A;   //square A + square B
    float V;   //resultant vector
  
    cout << "Enter in both adjecants." << '\n';
    cin >> x;
    cin >> y;
    A = (x * x) + (y * y);
    V = sqrt ((double) A );
  
    cout<< V << "\n";   //output of resultant vector
    cin.get();
    cin.get();
    return 0;
}

After creating this program in a few minutes, I went on to try something a bit more challenging. This program below took me about two hours to write; a lot of debugging. At first, I tried to write the entire program within the main function, although, that didn't really challenge me or enhance my abilities in any way, and it was extremely buggy. I decided to seperate each step in the process of matrix multiplication into seperate functions, and create my main function as a do while loop. It turned out producing correct results! Here is the code, keep in mind this only works with 2x2 matrices:


#include<iostream>
using namespace std;

/*The functions in this program are fired in the order of top to bottom for readability*/

void Readmatrix(int a, int b, int c, int d, int e, int f, intg, int h)
{

/*This function records the integers placed into each matrices' variable*/

        cout << "Enter in your first matrix (4 integers): ";
        cin >> a;
        cin >> b;
        cin >> c;
        cin >> d;
        cout << "Enter in your second matrix (4 integers): ";
        cin >> e;
        cin >> f;
        cin >> g;
        cin >> h;
      
}

void multiply(int a, int b, int c, int d, int e, int f, intg, int h)
{

/*This is the algorithm for multiplying the matrices together. It is only compatible with 2x2 matrices*/

        int a1, b1, c1, d1 = 0;
      
        a1 = ((a * e) + (b * g));
        b1 = ((a * f) + (b * h));
        c1 = ((c * e) + (d * g));
        d1 = ((c * f) + (d * h));
      
        a = a1;
        b = b1;
        c = c1;
        d = d1;

}

void Displaymatrix(int a, int b, int c, int d)
{

/*The functions simply displays the 2x2 matrix product*/

        cout << a << " " << b << '\n' << c << " " << d;
}

int main()
{

/*The functions is my main loop, which calls all the above functions*/

        char rawr;
      
        do
        {
                int a, b, c, d, e, f, g, h = 0;

                Readmatrix(a, b, c, d, e, f, g, h);
                multiply(a, b, c, d, e, f, g, h);
                Displaymatrix(a, b, c, d);
                cout << endl;
              
                cout <<"Would you like to do another matrix (y/n)? ";
                cin >> rawr;
                cout << endl;
        } while ((rawr == 'y') || (rawr == 'Y'));


        return(0);
}

I feel like I'm getting a little better each day I do this! Tomorrow, I plan to write out a program that will add, subtract, and reduce fractions. I will be using a reference source code, found here: http://cplusplus.com/files/fraction.zip

I can't wait to start moving onto programs using windows coding! I plan to become accustomed to using arrays, functions, structures, pointers, and dynamic memory before I move onto more advanced concepts though. If I can write simple programs using the tools I just listed, I'll have some fundamental skills to use for those more advanced concepts.

Decided to keep a blog.

A friend of mine mentioned to me today that I should keep a blog of my journeys on becoming a video game programmer; I have been accepted to my top choice college: DigiPen! I'm ridiculously excited to start school there next August! I'll be starting in a Bachelor's Degree of CS in Real Time Interactive Simulation (RTIS). Here is a link to DigiPen's site: https://www.digipen.edu/



The goal of this blog is to keep a detailed and prolific log of what it is like to start from no programming experience, to becoming a professional programmer.


Sadly, I already have a small amount of programming experience. My first language I ever built a program in was AS3, which is the language behind Flash. This was the ever popular Hello World program. From this point, I ventured into forums and online tutorials, and within a couple weeks of study I was able to create a simple game (which I currently still have on my flash drive), and then continue additions to the game once the tutorial was over.


I couldn't, even if I wanted to, recode this game from scratch -I only had a deep enough understanding of AS3 to modify pre-existing code from the tutorial. So, the goal of this blog is still attainable!


After this little flash game, I stopped coding altogether.


Now, two years later, I am accepted to a great college, am learning C++ from the ground up on my spare time, and finishing up my senior year of highschool.


The reason I have chose to spend my time learning C++, is because the language that 90% of video games are made with is C and C++, not to mention C and C++ are taught extensively at DigiPen, so anything I learn now will be very valuable during college.


I've spent two days reading through the tutorial found on this website http://cplusplus.com/, and I've gained a working knowledge of all categories up to Classes(1). I've learned to use variables, constants, basic input/output via cin and cout, control structures (like for and while loops, if and else statements) , arrays, pointers, dynamic memory, and data structures.


While progressing through this tutorial, I take effort into not only understanding the concepts taught by the examples, but I go and create my own programs similar to the examples from scratch best I can.


Over these two days, I have written two console programs. Here is the code for the my first program created during this tutorial, it's function is to have the user guess what number the number input by the user is being muliplied by. It only works with integers:






#include <iostream>
using namespace std;

int subtraction (int a)
{
    int r;
    r = a*5;
    return (r);
}

int main ()
{
    int x, y, z;
    y = 5;
    loop:
    cout << "Enter a number into the secret equation." << '\n';
    cin >> x;
    cout << "Now I will modify the number you entered; it is now:" << '\n';
    cin.get();
    cout << subtraction (x) << " - Press enter to continue" << '\n';
    cin.get();
    cout << "Can you guess by what number I multiplied yours by?" << '\n';
    cin >> z;
  
    while (z != 5)
    {
          cout << "You guessed the wrong number!" << '\n';
          goto loop;
          }
        
    return 0;
}






Here is the code for my second program created while running through this tutorial. This program stores strings entered by the user, and then spits them back out:





#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct animals_t {
    string danger;
    string size;
}dog, mouse, alligator;

void printdetails (animals_t details)
{
    cout << details.danger << '\n';
    cout << "And this large: ";
    cout << details.size;
}

int main ()
{
    string input;
  
    cout << "How large is a dog!? ";
    getline (cin,dog.size);
    cout << "How dangerous is a dog!? ";
    getline (cin,dog.danger);
    cout << "How large is a mouse!? ";
    getline (cin,mouse.size);
    cout << "How dangerous is a mouse!? ";
    getline (cin,mouse.danger);
    cout << "How large is an alligator!? ";
    getline (cin,alligator.size);
    cout << "How dangerous is an alligator!? ";
    getline (cin,alligator.danger);
    cout << '\n';
  
    cout << "A dog is this dangerous: ";
    printdetails (dog);
    cout << '\n' << '\n';
  
    cout << "A mouse is this dangerous: ";
    printdetails (mouse);
    cout << '\n' << '\n';
  
    cout << "An alligator is this dangerous: ";
    printdetails (alligator);
    cout << '\n' << '\n';
  
    cin.get();
  
    return 0;
}



I'm finding that coding in C++ is actually extremely rewarding! Just creating these two programs, from scratch, without any help has revitalized me and peaked my interest of programming even more so than it was before!


I am now currently reading an article about 2d physics engines here: http://www.gamedev.net/reference/programming/features/verletPhys/ and should finish reading it before I finally get to sleep. The article is fairly simple in terms of concepts, but the actual code is way over my head.. Ugh. I can understand the math and the concepts, but I can't wrap my head around every bit of code presented.


Here is an extremely useful podcast about becoming a game programmer (right click save target as): https://www.digipen.edu/uploads/media/digipen_podclass_issue_37.mp3


My goal is to submit a daily entry here. Once college starts, that may change.