söndag 23 november 2014

Week 2 gameprogramming I

The second week of the course and I am starting to understand the C++ programming structure. I have a long way to go but this week I got one step closer to creating a game!

The focus on my side is to learn "for" loops and how to get functions outside main to work and in what order i should code the functions. I have also learned how to get random values and sort them, how to make a small random map and much more.

I have not done all the program exercises but I will get to it later this evening.

So a little bit of code then.
__________________________________________________________
#include <iostream>
#include <time.h>

// My random function below named random.

int random(int min, int max)
{
return min + (rand() % (max - min + 1)); //int min and int max will be used later
}

int main(int argv, char* argc[])
{

int array1[5];
int swap = -1;
int end = 4;
int count = 5;
srand((unsigned int)time(0));

for (int i = 0; i < 5; i++)
{
array1[i] = random(0, 100); // 0 = int min and 100 = int max.
std::cout << array1[i] << " "; // print out the array
}

std::cout << std::endl; // just some space :D
std::cout << std::endl;
std::cout << "sortera" << std::endl;
std::cout << std::endl;

// run the "for" loop until we have cheeked all the array values.
for (int counter = count - 1; counter > 0; counter--)
{
.
for (int i = 0; i < end; i++)
{
// look what value is bigger and swap them.
if (array1[i] > array1[i + 1])
{
swap = array1[i + 1];
array1[i + 1] = array1[i];
array1[i] = swap;
}
}

end--;
}

for (int i = 0; i < 5; i++)
{
std::cout << array1[i] << " "; // print out the array again but this time it is sorted.
}

std::cin.ignore(1024, '\n');
std::cin.get();
return 0;
}

lördag 15 november 2014

Week 1 gameprogramming I

Starting a new course that i think will be fun.

I am programming C++ in Visual studios and started to learn the coding language that resembles flash actionscript and Csharp. It was some years ago i coded so a introduction to coding in C++ is good for my learning process. This week we tested to build simpler programs to get started, some of them was challenging for me.

One of the programs that was challenging for me was this program that I show below. I am not the best at coding and before this course I usually made small applications and nothing fancy.

Program 24
-Write a program that calculates the N!

All of the explanations in the code is in Swedish.
_________________________________________________________________________________

#include <iostream>

int main(int argv, char* argc[])
{

// deklarera variabel a,b och initierar b.

int a;
int b = 1;

// skriv ut sträng och ta emot variabel "a" av användaren.

std::cout << "program24: räkna ut fakultet av ett positivt värde." << std::endl;
std::cin >> a;

// om "a" är större eller lika med 1 kör koden. Koden körs inte om ett negativt värde är inmatat.

if (a >= 1)
{

// kör koden till a=1.

while (a >= 1)
{

// b *=a , b=b*a. b gånger a och tilldelar det värdet tillbaka till b.

b *= a;

// a--, a=a-1. gör värdet mindre varje gång delen körs.

a--;
}

// skriv ut svaret "b".

std::cout << "Svar: " << b << std::endl;
}

// unik regel då fakulteten av 0 blir 1.

else if (a == 0)
{
std::cout << "Svar: 1" << std::endl;
}

// om felinmatning ges går den vidare till "else".

else
{
std::cout << "värdet kan inte vara negativt." << std::endl;
}

// säg åt programmet att vänta på input.

std::cin.ignore(1024, '\n');
std::cin.get();
return 0;
}
_________________________________________________________________________________
The code without explanations.

#include <iostream>

int main(int argv, char* argc[])
{
int a;
int b = 1;

std::cout << "program24: räkna ut fakultet av ett positivt värde." << std::endl;
std::cin >> a;

if (a >= 1)
{
while (a >= 1)
{
b *= a;
a--;
}
std::cout << "Svar: " << b << std::endl;
}
else if (a == 0)
{
std::cout << "Svar: 1" << std::endl;
}
else
{
std::cout << "värdet kan inte vara negativt." << std::endl;
}

std::cin.ignore(1024, '\n');
std::cin.get();
return 0;
}