Here is an experiment I did for creating my own in game sprite. The proportions are slightly off as it was just an experiment. I want to make a hand drawn/watercolour painted game, to give it a delicate handmade feel. This was the result of my first experiment.
When following a tutorial, I made the value of x, 7 and the value of y, 42.
I then set the pointer value to assign the address of x, so for the first paragraph *ip=7
In the second paragraph x now has the value 73. So x = 73, y remains 42 and the *ip has changed in accordance to addressing x and is now 73.
In the third paragraph ip is assigned to the address of y.
So x = 73, y remains 42 and ip has changed in accordance to addressing y and is now 42.
This tutorial taught me about the following:
Are tokens that provide readable names for variables, function, labels and defined types, made up of letters and numbers within a set of constraints.
Can be identified as:
i = 7;
int: i = 7;
variable i has a value of 7.
- C++ uses qualifiers, sometimes called modifiers, that change the behaviour variables in various ways.
- Eg. const qualifier = read only, prevents value being changed. Eg. const int = 7;
- A variable is a typed and named location in memory
- Memory is allocated for the size of an integer associated with the name x, integer value 1 is copied into the memory location associated with the integer variable x.
x = 1;
int y = x; is both a definition and an assignment
int *ip; is a pointer to a value, ip is pointer to int
& = reference operator or address operator.
ip = &x this assigns address of x to integer pointer ip
y = *ip this copies the pointed value by ip which currently points to integer variable x which currently points to integer variable y. In this case the * is called the pointer dereference operator.
A really common mistake in while loops is infinite loops, so it’s important to remember to increment i below console.WriteLine so that it doesn’t continue displaying 1 forever.
i += 1; is another way of writing i++;
Problem Solving With While Loops
Here I’ve coded a While Loop for the number of attempts it takes to roll a six on a dice given that there is a one in six chance.
I defaulted the numberOfAttempts to 0 as no attempts had been made yet.
The int attempt stores the single attempt and as long as the number does not equal six, != 6 the code will loop the code setting the attempt to a new random value.
To generate random I referenced the random class, similarly to in a previous post.
I’ve set the parameters of attempt = numberGen.Next(1, 7); because the dice roll limits the numbers 1 to 6. Again 1 being inclusive and 7 being exclusive. This could also be written as (1, 6 + 1);
I also experimented with a do while loop, which required you to be sure the code would perform at least one iteration.
This is a way to iterate a way to do something a number of times.
defining int variable to i and setting it equal to 1, in this case 1 is our starting number.
As long as i is <=10 we want to execute the line below.
i++ is to add a value of 1 to the value of i on each loop.
The (i) is the parameter telling the console to write the value of i, upon completion it loops until it reaches the number 10.
Displaying Even Numbers
To experiment with if statements, I’ve attempted getting the console to display only even numbers to the user between 0-100 when using a for loop. It appears to have been a success!
Here I took the user answer and subtract the correct answer from it in the parenthesis to apply it to the closeness of the user’s answer. For example, if the user was asked what is 2*8 and the user said 15, the application would perform the subtraction 15 – 16 = -1.
The Math.Abs statement converts any negative number to a positive.
So when seeing 15-16= -1 and the Math.Abs statement converts this to 1, the application can read that the user was 1 away from the correct answer.
Here if else statements can be used to inform the user how close they were to the correct answer, by saying if diff == 1 (being the closest), else if diff <= 10 (being fairly close) and else (being not close at all).
Expanding from the previous post of referencing a random class to create a random number generator I also added the goto Start, so that the user can continuously answer random math questions.
Next I added a switch statement to vary the responses for, I could continue using if else statements but the switch statement is easier to read.
Here I made a new int variable responseIndex which offers 3 responses to the user.
The switch statement uses no semi colon, and in the parenthesis it references the responseIndex.
I then created cases with individual responses and then added a break to end the switch statement and move onto the next one. Then similarly to the else statement in if else, I added a default case, so if none of the other cases are met the computer will resort to the default.
Writing a multiplier question for the user with two different responses.
Here I have written a similar application to the cat question one in my earlier post. Except now I am asking for the application to ensure the user’s answer is correct as well as using if else statements.
Here the int variables have been defined with their own values with implicit variable declaration so that the user has two numbers to multiply.
The if statement asks the computer to check that the user’s answer matches the multiplication of int variables num01 and num02.
Again, if the user’s answer is equal to num01*num02 then they receive a response of correct, if the answer is anything else, they receive the response of incorrect, try again.
Adding Random Numbers (Referencing Random Class)
I learned of the C# language class called random, which has all kinds of methods for random numbers etc. In order to use it I had to reference the class by creating an instance of the random class.
Random numberGenerator = new Random(); references the class.
numberGenerator.Next returns a non-negative number.
However, by just adding (); on the end of each function there are no limitations for the numbers, which could make the questions difficult for the user. So I’ve set limitations for the random numbers.
For choosing between numbers 1-10 I have writting (1,11) as the main value is inclusive, in this case number 1, will be included. However, the max value is excluded, in this case 11 which means the random numbers will only be offered up to 10 as the number 11 is excluded.