Hello guys :) What a great and usefull forum m8s :)

I have made a program in snake, but i have remade it, because c++ is quite new for me, but on tursday i have to do a presentation(examination) for my teacher explaining the code, i understand 80-90 % of the code. But i would like to have, a pro explaining each section function if possible :)

The code:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")

int iSleepTime=125;


void screen(char main[][75], int score);
void reset(char main[][75]);
bool exitgame(int score);
void dot(int &dots, char main[][75]);
void control(int &direction);
void move(char main[][75], int &pieces, int pastCounter, int past[][2], int &dots, int &score, int &exit);
void directionn(int direction, int &pastCounter, int past[][2]);

int main()
{
    int past[1000][2];
    int pieces = 3;
    char main[25][75];
    int pastCounter = 6;  
    int direction = 0;
    int dots = 0;
    int score = 0;
    int exit = 0;
    int playAgain = 1;
    
    
    
    
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),31); // Farve
   
    
        for (int x = 0; x < 1000; x ++) 
        	{
        		for (int y = 0; y < 2; y ++)
        		{
        			past [x][y] = 0;
        		}
        	}
        	past[pastCounter][0] = 1; //Væg
        	past[pastCounter][1] = 1;
        	while(exit == 0)
        {  
        		screen(main, score);
        		Sleep(iSleepTime); //For normalt hastighed
                control(direction);
        		directionn(direction, pastCounter, past);
        		reset(main);
        		move(main, pieces, pastCounter, past, dots, score, exit);
        		
        		if (dots == 0)
        		{  //Nye æbler 
        			dot(dots, main); 
        		}
        		
        	}
        	exitgame(score);
        	Sleep (10000);
        	}
        
        char *strAddChar(char *s1, char c)
        {
        char *s;
        s = s1;
        s = s + strlen(s1); // Slangens position
        *s = c; // placering  null char first
        // was.
        s++;
        *s = 0; // sætte ekstra dele til slangen, new null string

    return(s1);
} // strAddChar

void screen(char main[][75], int score)
{//Tegner skærmen
	char s[2000]="";
    int i=0;
    
    
    i=sprintf(s,"Score : %d  | Speed : %d\n", score, (int)1000/iSleepTime);
//    printf ("Score : %d\n", score);
	
    for (int x = 0; x < 23; x ++)
	{
		for (int y = 0; y < 75; y ++)
		{
			strAddChar(s,main[x][y]);
		}
		strcat(s,"\n");
	}
	
	system("cls");
	printf("%s",s);
	Sleep(10);
}
void reset(char main[][75])
{// resetter arrayen, sådan at slangen ikke får dele af slangen steder, hvor der ikke brude værer dele efter slangens bevægelse.
 	for (int x = 0; x < 23; x++)
		{
			for (int y = 0; y < 75; y++)
			{
				if (main[x][y] == '@')
				{  //resetter ikke æble
					main[x][y] == '@';
				}
				else
				{
					if (x == 0 || x == 22 || y == 0 || y == 74)
					{//resetter ikke rammen
						main[x][y] = 178;
					}
					else
					{
						main[x][y] = ' ';
					}
				}
          }
      }
}
//
void move(char main[][75], int &pieces, int pastCounter, int past[][2], int &dots, int &score, int &exit)
{//Bevæger slangens hovede, og dens krop følger efter
	if (past[pastCounter][0] == 22 || past[pastCounter][0] == 0)
	{  //Når slangen rammer rammen, så dør slangen
	PlaySound("start.wav", NULL, SND_FILENAME | SND_ASYNC);
		exit = 1;
	}
	if (past[pastCounter][1] == 74 || past[pastCounter][1] == 0)
	{
    PlaySound("start.wav", NULL, SND_FILENAME | SND_ASYNC);
		exit = 1;
	}

	for (int x = 0; x < pieces; x++)
	{
		if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == '@')
		{  //Når slangen spiset et æbel
			dots--;
			pieces++;
			score += 10;
			PlaySound("eat.wav", NULL, SND_FILENAME | SND_ASYNC);
			iSleepTime=max(iSleepTime-5,25);
			
		}
		if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == 'o')
		{  //Hvis slangen spiser sig selv.
		PlaySound("dead.wav", NULL, SND_FILENAME | SND_ASYNC);
			exit = 1;
			
		}
		else
		{  //Slangens position bestemmes
		main[past[pastCounter - x][0]][past[pastCounter - x][1]] = 'o';
		}
	}
}
void control (int &direction)
{//kontrollere brugeres indput
	int key = 0;
	if (kbhit())
	{
		
		switch (getch())
		{
		case 72:
			direction = 2;
			break;  //up
		case 77:
			direction = 0;
			break; //right
		case 80:
			direction = 3;
			break; //down
		case 75:
			direction = 1;
			break; //left
		}
	}
		
}
void directionn(int direction, int &pastCounter, int past[][2])
{ //Gælder spillerens bevægelse, som bestemmer slangens retning
	int right;//adder til array
	int down;
	right = past[pastCounter][1];
	down = past[pastCounter][0];
	switch (direction)
	{  //Slangens retning
	case 0:
		right ++;
		break;
	case 1:
		right --;
		break;
	case 2:
		down --;
		break;
	case 3:
		down ++;
	}

	pastCounter ++;
	past[pastCounter][0] = down;
	past[pastCounter][1] = right;
}
void dot (int &dots, char main[][75])
{//Placere æblerne
	int up = 0;
	int left = 0;
	dots = 1;
	

	for (int x = 0; x < dots; x++)
	{ //Placerer kun æbler, når hverken rammen eller slangens krop allerede er på dem
		up = (rand() % 23)+1;
		left = (rand() % 73)+1;

		if (main[up][left] != ' ')
		{
			dot(dots, main);
		}
//		if (main[up][left] == '*//')
//		{
//			dot(dots, main);
//		}
		else
		{
			main[up][left] = '@';
		}
	}
}




bool exitgame (int score) // returnere enten sandt eller falskt
{//Går ud af spillet, og viser scoren

	int exit = 0;
	system ("cls");
	printf ("Game Over\n\n");
	printf ("You got a score of %d\n", score);
	
	return (getch());
}

Hey, i dont pretend to program, i have had 1 month of lessons (around 30 hours). Our teachers dont expect us to make a program, but to build in a program and make some addition and fixes.

Added sleeptime, sound, fixed main problem, fixed apple generator, fixed main lagging.


Why do u have to be so negative?

mate if u dont want to help, then dont answer thanks.

i dont understand this part very good:

char *strAddChar(char *s1, char c)
        {
        char *s;
        s = s1;
        s = s + strlen(s1); // Slangens position
        *s = c; // placering  null char first
        // was.
        s++;
        *s = 0; // sætte ekstra dele til slangen, new null string

    return(s1);

i know its about the snakes position, and the ekstra pieces on snakes after eaten apple. But if someone, would like to explain?

Typical - first post is "Hey, I'm a programmer who needs a bit of help".

When they get "found out" for posting someone else's work, it's out with all the sob stories of "I didn't want to do this course; the teacher is an idiot; I really need to pass this course; my dog ate my mouse;" and so on.

Yours is somehow no different.

But since you understand "80 to 90%" of it, how about you begin by explaining that much of it, so we only have to do 10% of the work rather than 100% of the work.

okay, what dont u understand? What function dont u understand? shall i just tell u basic what it does for each section?

i added sleeptime, it increases the speed of the snake, everytime a apple gets eaten.

what u wanna know?


i dont understand this one tho

char *strAddChar(char *s1, char c)
        {
        char *s;
        s = s1;
        s = s + strlen(s1); // Slangens position
        *s = c; // placering  null char first
        // was.
        s++;
        *s = 0; // sætte ekstra dele til slangen, new null string
 
    return(s1);

i admittet in the start that it wasent mine, just my english "but i have remade it, because c++ is quite new for me".

For a C++ program, there is precious little C++ in the code at all.

Apart from a few reference parameters (some for no good reason I can see), and the for loop declarations, this could so easily pass for being a C program.

If you want it to be more like C++, then do these things:
1. Replace all printf with cout
2. Replace all char arrays with std::string; for one thing, it reduces strAddChar() to one line of code.
3. Replace all other arrays with std::vector

Somewhere in the code there should be at least one 'class' with suitable member variables and methods.

But then again, you've had only 30 hours, so perhaps making classes is beyond you.

If you've been taught all these things (OK, assuming a level of competence in the teacher here, but what the heck, it might be true), then your tutor is going to be expecting to see some real C++.

So you took someone else's old C and made it make a couple of noises - whoop-ee-doo.

Actually, I don't really care, since we still can't tell what it is you're ACTUALLY capable of doing yourself.

What is for sure is that no one here is interested in helping some wannabe get a grade they don't deserve, which might lead them to getting a job which they are incapable of doing. Unlike student homework, the answers to job assignments CANNOT be found just by using google.

Member Avatar for iamthwee

The function strAddChar looks like what a std::string could probably do much more elegantly. That's all I'm saying though.

thanks salem and iamthwee, its a great help, i understand salem and i am sorry if u feel used or something like that, i thank u for the great help, i haver never used programming before, soo.. in order for me to learn the c++, i needed help from u guys, because of the limited learning time. Our teachers does infact expect us to get help explaining, because when i get it explained i will understand the programming better.


Thanks! and to u too iamthwee :)

Your teacher expects you to copy and paste the code on cboard.cprogramming.com and have us explain it to you? I think a monkey just flew right out... nevermind.


You know I always had a problem with copying & pasting code, I still do. I never did it, I typed it myself even when it's free to anyone to use for anything. I just didn't do it, maybe you don't have what it takes? You know, that "ethics" thing?

Somehow stealing the idea and providing your own implementation may help, idk. I hate to call it stealing but I feel that way, even when it's free to anyone.

What have you accomplished by copying and pasting? In my opinion there is further benefit to writing it yourself.

commented: Nicely put +17

i have accomplished a lot i think so, 1 week ago i did know how to program. I only know that int measn that and char means that... basic stuff. But i couldn't put it together, i was confused, by studying this program carefully and adding/removing stuff, i know understand muccccccch more than before.

But guys, i have to go make preparations for examination.

And m8 yeah my teacher said, that we should ask on forums, if there where stuff that i didnt understand. Under the examination the 2 teachers, can tell if i have understood the code, they will ask me to change it, or to implement something else.

but thx guys :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.