snake game in C++ with header files

Hi I just finished a code for a snake game hope you like it an hope you can improve it but please if you improve post your improvement here. please tell me if it's good!
thanks.
p.s.: it's a bit long, but I think you hard-workers out there will figure it out... :)
p.p.s: if you find any small insignificant errors it's just because this isn't the final edition... I cant find the better version... sorry guys...

the simplest file which contanes all the simple functions and basic definitions(.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef SYSTEM_H
#define SYSTEM_H
#include<iostream>
#include<ctime>
using namespace std;
#define XLEN 20
#define YLEN 5
#define EMPTY ' '
#define MAX_SCORE 5
int sec=1;
int delay=0;
int score=0;
bool beep=false;
bool show=false;
void l(int num)
{
	cout<<" ";
	for(int i=0;i<num;++i)     cout<<"-";
	cout<<     endl;
}
void print(char arr[YLEN][XLEN])
{
	l(XLEN);
	for(int i=0;i<YLEN;++i,     cout<<"|"<<     endl)
	{
		cout<<"|";
		for(int j=0;j<XLEN;++j)
			cout<<arr[i][j];
	}
	l(XLEN);
	cout<<"score: "<<score<<     endl;
}
void printd(int d)
{
	cout<<"direction: ";
	switch (d)
	{
	case LEFT:
		cout<<"left\n";
		break;
	case UP:
		cout<<"up\n";
		break;
	case RIGHT:
		cout<<"right\n";
		break;
	case DOWN:
		cout<<"down\n";
		break;
	}
}
void print_error()
{
	cout<<"I don't understand you. Enter correctly please!\n";
	Sleep(800);
}
void clear(char arr[YLEN][XLEN])
{
	for(int i=0;i<YLEN;++i)
		for(int j=0;j<XLEN;++j)
			arr[i][j]=EMPTY;
}
void b(bool beep)
{
	if(beep)     cout<<"\a";
}
void p(){system("pause");}
void c(){system("cls");}
void settings()
{
	int set;
	do
	{
		c();
		cout<<"settings:\n"
			<<"***********\n"
			<<"\n"
			<<"1.delay: "<<sec<<".\n"
			<<"2.sound: ";
		if(beep)cout<<"on\n";
		else cout<<"off\n";
		cout<<"3.show details: ";
		if(show)cout<<"on\n";
		else cout<<"off\n";
		cout<<"0.exit\n";
		cin>>set;
		if(set==1)cin>>sec;
		else if(set==2)cin>>beep;
		else if(set==3)cin>>show;
		else if(!set);
		else print_error();
	}
	while(set);
}
void setdelay(){delay=sec*1000;}
int xran(){return rand()%XLEN;}
int yran(){return rand()%YLEN;}
#endif 


the file in which the movement manager is defined(.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef MOVMENT_H
#define MOVMENT_H
#include<iostream>
#include<Windows.h>
#include"system.h"
#define NOTE 'X'
#define APPLE '@'
#define LEFT 'a'
#define UP 'w'
#define RIGHT 'd'
#define DOWN 's'
void place(char arr[YLEN][XLEN],int &x,int &y,int d)
{
	arr[y][x]=EMPTY;
	if(d==LEFT)arr[y][--x]=NOTE;
	else if(d==UP)arr[--y][x]=NOTE;
	else if(d==RIGHT)arr[y][++x]=NOTE;
	else arr[++y][x]=NOTE;
}
bool eat(char arr[YLEN][XLEN],int x,int y,int d)
{
	if(d==LEFT&&arr[y][x-1]==APPLE)return true;
	if(d==UP&&arr[y-1][x]==APPLE)return true;
	if(d==RIGHT&&arr[y][x+1]==APPLE)return true;
	if(d==DOWN&&arr[y+1][x]==APPLE)return true;
	return false;
}
char randomize()
{
	int num=rand()%4;
	switch(num)
	{
	case 1:
		return LEFT;
	case 2:
		return UP;
	case 3:
		return RIGHT;
	default:
		return DOWN;
	}
}
bool endboard(int x,int y)
{
	if(x==-1||y==-1||x==XLEN||y==YLEN)return true;
	return false;
}
bool direction_is_good(int x,int y,int d)
{
	if(d==LEFT&&x==0)return false;
	if(d==UP&&y==0)return false;
	if(d==RIGHT&&x==XLEN-1)return false;
	if(d==DOWN&&y==YLEN-1)return false;
	if(d!=LEFT&&d!=UP&&d!=RIGHT&&d!=DOWN)return false;
	return true;
}
#endif 


the basic file in which all the game modes an stuff are declared(.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef GAME_H
#define GAME_H
#include<iostream>
#include<conio.h>
#include"movment.h"
#include"system.h"
using namespace std;
void rand_apple(char arr[YLEN][XLEN])
{
	int x=xran(),y=yran();
	arr[y][x]=APPLE;
}
void classic_mode()
{
	char board[YLEN][XLEN];
	int x=xran(),y=yran();
	char d;
	setdelay();
	clear(board);
	do
	{
		d=randomize();
	}
	while(!direction_is_good(x,y,d));
	rand_apple(board);
	board[y][x]=NOTE;
	do
	{
		do
		{
			c();
			if(eat(board,x,y,d))
			{
				++score;
				rand_apple(board);
			}
			place(board,x,y,d);
			b(beep);
			print(board);
			if(show)
			{
				cout<<"x="<<x<<" y="<<y<<endl;
				printd(d);
			}
			Sleep(delay);
		}
		while(!_kbhit()&&!endboard(x,y));
		if(!endboard(x,y))
			d=_getch();
	}
	while(!endboard(x,y)&&score<MAX_SCORE);
	cout<<endl;
	if(score>=MAX_SCORE)cout<<"you win!!! \1\n";
	else cout<<"you lose and your score is "<<score<<".\n";
	p();
}
void endless_mode()
{
	char board[YLEN][XLEN];
	int x=xran(),y=yran(),score=0;
	char d;
	setdelay();
	clear(board);
	do
	{
		d=randomize();
	}
	while(!direction_is_good(x,y,d));
	rand_apple(board);
	while(direction_is_good(x,y,d)&&score<MAX_SCORE)
	{
		c();
		while(!_kbhit())
		{
			place(board,x,y,d);
			Sleep(delay);
		}
		d=_getch();
		if(eat(board,x,y,d))
		{
			++score;
			rand_apple(board);
		}
	}
	c();
	print(board);
	if(score>=MAX_SCORE)cout<<"you win!!! \1\n";
	else cout<<"you lose and your score is "<<score<<".\n";
	p();
}
void instruc(){}
void run_simulator()
{
	char arr[YLEN][XLEN],d;
	clear(arr);
	int x=xran(),y=yran();
	rand_apple(arr);
	while(!_kbhit())
	{	
		do
		{
			d=randomize();
		}
		while(!direction_is_good(x,y,d));
		if(eat(arr,x,y,d))
		{
			++score;
			rand_apple(arr);
		}
		place(arr,x,y,d);
		c();
		cout<<"\t\t\tthe moving thing!!!(simulator)\n";
		print(arr);
		cout<<"Press any key to continue...";
		Sleep(700);
	}
	_getch();
}
#endif 


the main file:(.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<ctime>
#include<conio.h>
#include<Windows.h>
#include"game.h"
using namespace std;
typedef unsigned short directions;
void run_simulator();
bool direc_is_good(directions,int,int);
void print(char[YLEN][XLEN]);
directions random_direction(int,int);
void move(char[YLEN][XLEN],directions,int&,int&);
int getint();
int main()
{
	srand((unsigned int)time(NULL));
	int menu;
	cout<<"An Aviad Levine programing product...\n";
	Sleep(1800);
	c();
	cout<<"presenting the new extreme game...\n";
	Sleep(2000);
	c();
	cout<<"the most dramatic game ever to be played by man...\n";
	Sleep(2000);
	run_simulator();
	do
	{
		c();
		cout<<"menu:\n"
			<<"************\n"
			<<"\n"
			<<"1.play the extremely fun game\n"
                        <<"2.play endless mode\n"
			<<"3.read instructions\n"
			<<"4.continue watching the incredable simulator\n"
			<<"0.exit\n";
		cin>>menu;
		if(menu==1)classic_mode();
                else if(menu==2)endless_mode();
		else if(menu==2)instruc();
		else if(menu==3)run_simulator();
		else if(!menu);
		else print_error();
	}
	while(menu);
	return 0;
}
void instruc()
{/*not finished*/}
void run_simulator()
{
	char arr[YLEN][XLEN];
	for(int i=0;i<YLEN;++i)
		for(int j=0;j<XLEN;++j)
			arr[i][j]=EMPTY;
	directions d;
	int x=rand()%XLEN,y=rand()%YLEN;
	while(!_kbhit())
	{
		d=random_direction(x,y);
		move(arr,d,x,y);
		c();
		cout<<"\t\t\tthe moving thing!!!(simulator)\n";
		print(arr);
		cout<<"Press any key to continue...";
		Sleep(700);
	}
}
void print(char arr[YLEN][XLEN])
{
	for(int i=0;i<YLEN;++i,cout<<endl)
	{
		for(int j=0;j<XLEN;++j,cout<<" ")
		{
			cout<<arr[i][j];
		}
	}
}
bool direc_is_good(directions d,int x,int y)
{
	if(d==UP&&y==0)return false;
	if(d==LEFT&&x==0)return false;
	if(d==DOWN&&y==YLEN-1)return false;
	if(d==RIGHT&&x==XLEN-1)return false;
	return true;
}
int getint()
{
	char c;
	do
	{
		c=_getch();
		if(c<'0'||c>'9')
			print_error();
	}
	while(c<'0'||c>'9');
	return (int)(c-48);
}
directions random_direction(int x,int y)
{
	directions d;
	do
	{
		d=(directions)(rand()%4);
	}
	while(!direc_is_good(d,x,y));
	return d;
}
void move(char arr[YLEN][XLEN],directions d,int &x,int &y)
{
		arr[y][x]=EMPTY;
		if(d==UP)arr[--y][x]=NOTE;
		else if(d==LEFT)arr[y][--x]=NOTE;
		else if(d==DOWN)arr[++y][x]=NOTE;
		else arr[y][++x]=NOTE;
}
Last edited on
Topic archived. No new replies allowed.