|
Hi all,
It’s Friday and it is time for a weekly update.
As said in the post before this, this week the Game Programming course started. My first week has been more or less an introduction. We have gone through a lot of basic c++.
For example we have been through:
- Input/Output: Since we only work in the console so for this is really easy, in my opinion. This just demand a couple of lines of code. The easiest and, probably, the most famous kind of output example is the “Hello World” example. But I am going to show you another example, which is just a bit more advanced. I’ll show you both parts of input and output a bit further down the post.
- Fundamental Data types: We have also talked about data types. There are a bunch of different data types. The big difference between these data types is the size. Some is the size of 1 byte, 2 bytes, 4 bytes and 8 bytes. Some examples of data types are: Integers, Characters, Boolean and Floating Points. For this week I have been using the integers most. The integer is exactly what it is. The value is an integer, so if you use this data type you won’t be able to get another value than an integer. (You wont be able to get a value of e.g. 1.2 or 15.78). To get a value of that kind you will need to use the Floating Point.
They are all defined in each way, but as said, the main difference is the size in byte. The floating point is declared by writing “float a = 1.5f;”. The ‘f’ at the end is to really make sure that the value will be a float. This is, what I have understood, the only time in C++ that you do this. The program won’t broke if you forget to write it, but it should be there.
The integer is declared by writing “int a = 10;”. The semicolon ‘;‘ is almost the most important thing to remember when programming in C++. Without this nothing will work in the program, really. DO NOT FORGET IT!
- Variables: Variables is another thing we have gone through. As you saw in the previous text about ‘Fundamental Data Types’ I wrote 2 examples of how to declare a float and an integer. The ‘a‘ after float and int is the identifier. That is your variable. You can call the variable in your function. The variable is an identifier which holds a value that is given to it.

Now I feel that I have gone through the most important things for you to understand these two pictures. The first picture you can see the function with the codeblock, and the code in it. At the top you can see it is written ‘#include ‘. This is a C++ Standard Library which contain the cout, cin and cerr tools.
The first thing I do in the function main is to declare 2 variables. int userInput1 and userInput2. I give them the value of 0. After that I use the namespace std to first ask the user to write integers. I have not learned so much about the namespace std yet, but we will so I can’t explain that yet.
However, the line 10, under the line where I ask the user to write the first integer. In this line the user is able to write an integer which will be saved in userInput1.
After the user have written both his integers they will be displayed as in picture 2. I gave the first integer the value of 10 and the second 15.
Now, at the end of the first school week I have pushed myself to do something I did not know how to do before. The thing I got most proud of is my more advanced version of the Guessing what Number it is – game. It looks like this:
So, at the top of the first picture you see one of the random number generator. I have not fully understood how this work yet. But the teacher know that if we got a random number generator we could create this game, but I worked more on it.
In the main function I am starting of to call the random number generator function. I then us the boolean data type to get my While-loop to work correctly. I use the while-loop because it is infinite if it is not interrupted and since I wanted the player to be able to have a rematch if he/she wanted to, I had to do this.
In the outside While-loop I start of declaring a bunch of variables. The green text next to the variables is comments, where I explain for myself and others what the variable is for. You write a comment by writing two slashes, like: “//Blablabla”. This kind of comment will only be there for 1 line. If you want to write a comment that is over multiple lines you will have to write it like this: “/* Bablbablal
blaball */”.
To decide who won I have used two variables, that each count the amount of guesses both the player and the computer uses. I have then let first 1 start guessing the number and after that it is the computer start. The one who guesses the number in the least amount of guesses win the game. As you see in the second picture, the player have already guessed correctly and I got it in 7 guesses. The computer is not done yet.
I check who had the least by comparing the two variables after the two do/while-loops using an If/Else statement. I chose to do it this way because I felt that I couldn’t make it work doing a turn based game.
This was an easier way of doing it, but the result is the same.
In the next post I will try to mention and talk about different kinds of loops.
I hope you found this interesting! We’ll see next Friday, I will then write about making PONG.
See you all soon,
Alex

About Alex Henningsson
2014 Programming
|