Jump to content

Tutorial-the basics of C++ and game making


Recommended Posts

Integer is not the only type of variable. Here are few other:

 

char ch='t';
cout<<ch;

The output will be "t". Note that the 't' is inside single commas. chars are inside single commas, strings(text) are inside inverted commas.

char is basicly the ascii number of that sign, so you can use it as regular number like this:

char ch='b';
ch++;
cout<<ch;

the output will be "c", because 'b'+1='c'.

 

You can get char input like this:

char ch;
cin>>ch;

Just like integers.

 

 

Other type of variable is the float. Integers are for complete numbers. floats are for real numbers, so they can contain fractions. Otherwise, floats are used just like integers, so you can write:

float f1=2.3,f2=4.6;
f1+=f2;
cout<<f1+f2;

ect.

 

Run this program:

#include<iostream.h>
void main()
{
 float f;
 f=5/2;
 cout<<endl<<f<<endl;
}

the output is "2"!!!

The problem is, that when dealing with integers, the result is integer. 5/2 is int/int, so the result is int. to get corrent answer, write:

#include<iostream.h>
void main()
{
 float f;
 f=5.0/2;
 cout<<endl<<f<<endl;
}

5.0 is float, so the result is float.

 

if you do it with variables, just multiple the first number by 1.0, like this:

#include<iostream.h>
void main()
{
 int a;
 cout<<"\n enter a number ";
 cin>>a;
 float f=1.0*a/2;
 //or
 float f=a/2.0;
 cout<<endl<<f<<endl;
}

If a is not even, you will get a fraction.

 

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

One of the most powerfull tools in programing is the arrays. Array is basicly a bunch of variables of the same type, that each one of them has an index.

index|value
-----+-----
 0   | 35
-----+-----
 1   | 48
-----+-----
 2   | 1
-----+-----
 3   | 119
-----+-----
 4   | -48

This is an integer array with 5 cells. The index in C++ is allways starting by 0.

 

This is how it is done in C++:

int a[5];

5 is the number of cells, so the index of the last cell is 5-1=4, since it's starts from 0.

If you want to use the arrays cells, you do it like this:

a[2]=5;
cout<<a[3];

Just like normal variables.

 

So why to use arrays?

Inside the [], you can put variables!!!

so:

int a[5],i=3;
a[i]=10;//same as a[3]=10;

 

You can also use arrays with the counter of for loops. This is the most common use for arrays:

int a[70];
for(int i=0;i<70;i++)
 cin>>a[i];

In few rows we got input for 70 variables.

 

Now I will show you a complicated program compared to what we have done so far:

#include<iostream.h>
void main()
{
 int a[7],counter=0;
 float avg=0;
 for(int i=0;i<7;i++)
  {
   cout<<"\ngive me a number ";
   cin>>a[i];
   avg+=a[i];
  }
 avg/=7;
 for(i=0;i<7;i++)//note that I didn't declare i, since its already declared
   if(a[i]>avg)
     counter++;
 cout<<endl<<counter<<" numbers are bigger than the avagreage\n";
}

We store the values in array, because we don't know what the avagrage is before we get all the values.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

Great work someboddy. I love programming in C++ it is my favourite programming language.

 

I am currently taking a Multimedia elective class and we have to make a videogame. Once I have finished I shall upload it somewhere for everyone to play :D

Link to comment
Share on other sites

The string is a speciel type of array. It's an array of chars. Therefor, you can use it as array or as single variable.

char s[20];
cin>>s;
cout<<s;

This code will get a string, and then print it.

You can give a value to the intire string only when you declare it:

char s[15]="hello world"; //Right
s="whats up";                 //Wrong

 

If you want to give a value to a string after you declare it, do it like this:

strcpy(s,"whats up");

strcpy is a function. Inside the (), there are the functions parameters. Even if The function doesn't have any parameters, you still need to put the (), like this: "function()". Inside the (), the parameters are seperated by commas.

 

The strcpy function is inside "string.h", so you must put the line "#include<string.h>" at the top of the program, like this:

#include<iostream.h>
#include<string.h>
void main()
{
 char s[15]="hello world";
 cout<<s;
 strcpy(s,"whats up");
 cout<<s;
}

 

You can also use the string like an array, like this:

char s[10]="hello";
cout<<s[2];

the output will be "l", because that's the char at the index 2 (remember, the indexes begin from 0).

 

The last char of the string is '\0', or 0 in ascii. This is the char that tells the computer where is the string's end. You can also use it.

#include<iostream.h>
void main()
{
 char s[20];
 cout<<"\ngive me a string: ";
 for(int i=0;s[i];i++)
   cout<<s[i]<<endl;
}

This program is a little complicated, so I will explain. The program get an input string, and put all the chars in the sting in a column. The i goes from the begining of the string - index 0 - untill s is false. If you remember, false in C++ is 0, so '\0', the end of the string, is false. so the loop will stop when it reach to the end of the string.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

You can make an array with more then one dimention. A 2D array is called matrix. You do it like this:

int mat[5][5];
mat[1][3]=7;
cout<<mat[2][4];

 

You can make more then two dimention arrays:

int a[2][5][7],b[4][3][10][6];

There is a limit, but it is very high, so you will probebly never reach it.

 

A matrix of chars, is basicly an array of strings:

char s[5][20];
cin>>s[2];

The firest number is the number of string in the array, and the second number is the maximum chars in each string.

 

If you want to fill a matrix with for loops, you need to make a loop inside loop. The indexes of the loops must be different, so the loops will work right. You use i for the outer loop, and j for the inner loop:

#include<iostream.h>
void main()
{
 int mat[5][5];
 for(int i=0;i<5;i++)
   for(int j=0;j<5;j++)
    {
     cout<<"enter number "<<i<<","<<j<<":";
     cin>>mat[i][j];
    }
 for(int i=0;i<5;i++)
  {
   cout<<endl;
   for(int j=0;j<5;j++)
     cout<<mat[i][j];
  }
}

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

Just to note a coupla things you missed:

 

You got down the standard operators alright (+, -, *, /) and their shorthand forms (++,+=, etc) but you missed a very essential and important set of operators - the bitwise operators.

 

Here they are:

 

& - Bitwise AND, performs a logical AND bit by between the two given variables

Returns 1 ONLY if both inputs are 1

AND Table:

a b Out

1 1 1

1 0 0

0 1 0

0 0 0

example

9 & 11 = 1001&1011=1001 = 9

| - Bitwise OR, performs a logical OR bit by between the two given variables

Returns 1 if either one, or both inputs are 1

OR Table:

a b Out

1 1 1

1 0 1

0 1 1

0 0 0

example

9 | 11 = 1001|1011=1011 = 11

^ - Bitwise XOR, performs a logical XOR bit by between the two given variables

Returns 1 if the inputs are different, also called a diffrential amplifier

XOR Table:

a b Out

1 1 0

1 0 1

0 1 1

0 0 0

example

9 ^ 11 = 1001&1011=10 = 2

~ - NOT, complements (inverts) every bit in the variable

Returns 1 if input is 0, 0 if input is 1

NOT Table:

a Out

1 0

0 1

example

~11 = ~1011 = 0100 = 4

 

Shorthand style follows the same rules for these operators, so you can do things like &=, |=, ^= etc

Note these are NOT the comparison operators, which are differentiated by doubling up the operators (&& for comparison and, || for comparison or, ! for comparison not)

 

Oh, if you feel like a brain teaser, have fun with this little piece of code :(

 

x^=y

y^=x

x^=y

 

Those three lines will actually swap the contents of x and y without needing a temporary third variable. The question is, How Does It Work? Enjoy!

Link to comment
Share on other sites

Thanks roofus.

 

Variables can change during the program, but you can also declare values that can not be changed. Why do you want to do that?

Let's say you need to use pi alot in your program. pi is the relation circuit of the circlre to it's diameter. The value of pi is about 3.141592654. Since you don't want to write that long number any time you use pi, you declare a constant.

 

The first way to declare a constant, is a macro:

#include<iostream.h>
#define PI 3.141592654 // constant are usualy writed with capital letters.
void main()
{
 cout<<"pi="<<PI;
}

#define name value

 

You can make string macroes, int macroes, char macroes ect.

 

What macro does is scaning your program from the point you declared it, and replacing each "PI" with "3.141592654".

 

Another way to decalre a constant, is a const variable:

const int PI=3.141592654;
cout<<PI;

If you add "const" before the variable decleration, the value you assigned it at the decleration is unchangeable.

 

The third way is to use the keyword "enum":

enum
{
 MINUTESISHOUR=60
};

enum can only be used for integer constants. You can make many contants in single enum command with the comma:

enum
{
 HOURSINDAY=12,
 MINUTESISHOUR=60
};

 

You can also use constants for values you will want to change during the programing.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

IDK if ill ever be able to understand that >_<

the stuff gets less intimidating as you work with it more.

 

programming isn't hard, its just tedious.

this is what it takes to calculate a score in my poker game(shrunken down to prevent page stretching):

/////////////////////////////////

int getscore(drawhand *thishand)

/////////////////////////////////

{

int score;

int lowesti=1;

int e=1;

drawhand sorted;

 

inithand(&sorted);

bool asorted[6];

for(int a=0; a<=5;a++)

{asorted[a]=false;}

 

 

 

 

for(int s=1;s<=5;s++)

{

lowesti=1;

while(asorted[lowesti]==true)

{lowesti++;}

 

for(int s0=1;s0<=5;s0++)

{

if((thishand->playercards[s0].num<thishand->playercards[lowesti].num)&&(asorted[s0]==false))

{

lowesti=s0;

}

}

 

e=1;

while(e<=5)

{

if((thishand->playercards[e].num==thishand->playercards[lowesti].num)&&(asorted[e]==false))

{asorted[e]=true;sorted.playercards=thishand->playercards[e];e=20;}

else

{e++;}

 

}

 

 

}

 

if(((sorted.playercards[1].num==10)&&(sorted.playercards[2].num==11)&&(sorted.playercards[3].num==12)&&(sorted.playercards[4].num==13)&&(sorted.playercards[5].num==14))&&((sorted.playercards[1].suit==sorted.playercards[2].suit)&&(sorted.playercards[2].suit==sorted.playercards[3].suit)&&(sorted.playercards[3].suit==sorted.playercards[4].suit)&&(sorted.playercards[4].suit==sorted.playercards[5].suit)))

{

score=600000000;

}

 

else if((((sorted.playercards[1].num==(sorted.playercards[1].num+0))&&(sorted.playercards[2].num==(sorted.playercards[1].num+1))&&(sorted.playercards[3].num==(sorted.playercards[1].num+2))&&(sorted.playercards[4].num==(sorted.playercards[1].num+3))&&(sorted.playercards[5].num==(sorted.playercards[1].num+4)))&&((sorted.playercards[1].suit==sorted.playercards[2].suit)&&(sorted.playercards[2].suit==sorted.playercards[3].suit)&&(sorted.playercards[3].suit==sorted.playercards[4].suit)&&(sorted.playercards[4].suit==sorted.playercards[5].suit))))

{

score=480000000;

score+=(sorted.playercards[5].num*pow(15,5));

 

}

 

else if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))||((sorted.playercard

s[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{

score=420000000;

if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))

{score+=(5*(sorted.playercards[1].num*pow(15,5)));}

else if((sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(5*(sorted.playercards[2].num*pow(15,5)));}

 

}

 

else if((((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))&&((sorted.playercards[4].num==sorted.playercards[5].num)))||(((sorted.playerc

ards[1].num==sorted.playercards[2].num))&&((sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))))

{

score=360000000;

if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))&&((sorted.playercards[4].num==sorted.playercards[5].num)))

{score+=(sorted.playercards[1].num*pow(15,5));score+=(sorted.playercards[4].num*

pow(15,4));}

else if(((sorted.playercards[1].num==sorted.playercards[2].num))&&((sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{score+=(sorted.playercards[1].num*pow(15,4));score+=(sorted.playercards[3].num*

pow(15,5));}

 

}

 

else if((sorted.playercards[1].suit==sorted.playercards[2].suit)&&(sorted.playercards[2].suit==sorted.playercards[3].suit)&&(sorted.playercards[3].suit==sorted.playercards[4].suit)&&(sorted.playercards[4].suit==sorted.playercards[5].suit))

{

score=300000000;

score+=(sorted.playercards[1].num*pow(15,1));

score+=(sorted.playercards[2].num*pow(15,2));

score+=(sorted.playercards[3].num*pow(15,3));

score+=(sorted.playercards[4].num*pow(15,4));

score+=(sorted.playercards[5].num*pow(15,5));

 

}

 

else if(((sorted.playercards[1].num==(sorted.playercards[1].num+0))&&(sorted.playercards[2].num==(sorted.playercards[1].num+1))&&(sorted.playercards[3].num==(sorted.playercards[1].num+2))&&(sorted.playercards[4].num==(sorted.playercards[1].num+3))&&(sorted.playercards[5].num==(sorted.playercards[1].num+4))))

{

score=240000000;

score+=(sorted.playercards[5].num*pow(15,5));

}

 

else if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))||((sorted.playercard

s[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))||((sorted.playercard

s[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{

score=180000000;

if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))

{score+=(sorted.playercards[1].num*pow(15,5));}

else if((sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))

{score+=(sorted.playercards[2].num*pow(15,5));}

else if((sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(sorted.playercards[3].num*pow(15,5));}

 

}

 

else if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))||((sorted.playercard

s[2].num==sorted.playercards[3].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))||((sorted.playercard

s[1].num==sorted.playercards[2].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{

score=120000000;

if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))

{score+=(sorted.playercards[3].num*pow(15,5));score+=(sorted.playercards[1].num*

pow(15,4));score+=(sorted.playercards[5].num*pow(15,3));}

 

else if((sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(sorted.playercards[4].num*pow(15,5));score+=(sorted.playercards[2].num*

pow(15,4));score+=(sorted.playercards[1].num*pow(15,3));}

 

else if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(sorted.playercards[4].num*pow(15,5));score+=(sorted.playercards[1].num*

pow(15,4));score+=(sorted.playercards[3].num*pow(15,3));}

 

}

 

else if((sorted.playercards[1].num==sorted.playercards[2].num)||(sorted.playercards[2

].num==sorted.playercards[3].num)||(sorted.playercards[3].num==sorted.playercards

[4].num)||(sorted.playercards[4].num==sorted.playercards[5].num))

{

score=60000000;

if(sorted.playercards[1].num==sorted.playercards[2].num)

{

score+=(sorted.playercards[1].num*pow(15,5));

 

score+=(sorted.playercards[3].num*pow(15,2));

score+=(sorted.playercards[4].num*pow(15,3));

score+=(sorted.playercards[5].num*pow(15,4));

}

else if(sorted.playercards[2].num==sorted.playercards[3].num)

{

score+=(sorted.playercards[2].num*pow(15,5));

 

score+=(sorted.playercards[1].num*pow(15,2));

score+=(sorted.playercards[4].num*pow(15,3));

score+=(sorted.playercards[5].num*pow(15,4));

 

}

else if(sorted.playercards[3].num==sorted.playercards[4].num)

{

score+=(sorted.playercards[3].num*pow(15,5));

 

score+=(sorted.playercards[1].num*pow(15,2));

score+=(sorted.playercards[2].num*pow(15,3));

score+=(sorted.playercards[5].num*pow(15,4));

 

}

else if(sorted.playercards[4].num==sorted.playercards[5].num)

{

score+=(sorted.playercards[4].num*pow(15,5));

 

score+=(sorted.playercards[1].num*pow(15,2));

score+=(sorted.playercards[2].num*pow(15,3));

score+=(sorted.playercards[3].num*pow(15,4));

}

 

}

 

else

{

score=0;

score+=(sorted.playercards[1].num*pow(15,1));

score+=(sorted.playercards[2].num*pow(15,2));

score+=(sorted.playercards[3].num*pow(15,3));

score+=(sorted.playercards[4].num*pow(15,4));

score+=(sorted.playercards[5].num*pow(15,5));

}

 

thishand->score=score;

 

return score;

}

 

it may take a while (this took me 2 hours), but it not really that hard.

You just have to learn to think like a computer. and since computers are stupid, it isn't hard.

Link to comment
Share on other sites

Functions are code parts that you can call. Functions are used to split the program into simple parts, making it easyer to write and to read, and/or to save codes: you can call a function any time you want.

 

You declare a function like this:

#include<iostream.h>

void func1()
{
 //bla bla bla
}

void main()
{
 //yada yada yada
 func1();//calling the function
}

 

First, the program will run "yada yada yada". Then, by calling func1, the program will run "bla bla bla". So:

#include<iostream.h>
void func1()
{
 cout<<"bla";
}
void main()
{
 cout<"yada";
 func1();
}

Will output "yadabla"

 

functions ca allso have parameters between the ():

void f(int a,int b,char c,char s[])

Then you can use those variables in the function.

Few notes:

You can't put two parameters after the same type(int, char ect.)

In arrays, you don't put the number of cells inside the []. In matrixes, you don't put the number in the first [], but you put it in the others. I will explain the reasons later.

 

If you change the parameter inside the function, it wont effact whatever you put there when you call the function. So:

#include<iostream.h>
void changenum(int a)
{
 a++;
}
voin main()
{
 int a=5;
 changenum(a);
 cout<<a;
}

will output "5".

The "a" inside the function is not the "a" inside the main program. You can call it any other legal name.

if you put "&" before the name of the parameter, The cangews you do to it will affact the outside:

#include<iostream.h>
void changenum(int &a)
{
 a++;
}
voin main()
{
 int a=5;
 changenum(a);
 cout<<a;
}

will output "6".

 

Functions can allso return values:

#include<iostream.h>
int multipile(int a,int b)
{
 return a*b;
}
voin main()
{
 cout<<multipile(4,5);
}

will output "20". Nonvoid functions must have a return command. The return can allso be put in a void function. The function will end itself when it reach to a return command, so:

#include<iostream.h>
void func()
{
 cout<<"first";
 return;
 cout<<"second";
}
void main()
{
 func();
}

will return "first"

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...