A simple snake game

Hi, i am making a simple snake game but i cant seem to be getting it right. firstly, i want the game to terminate when it hits the wall, second once the snake hits the target it should randomly appear in another place inside the box. Please help me.

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
120
121
122
123
124
125
126
127
128
129
 #include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
bool end;
const int width = 30;
const int length = 20;
int head, tail, target1, target2;
enum ecommand {stop = 0, l,r,u,down};
ecommand  comm;

void Setup(){
	end = false;
	comm = stop;
	head = width / 2;
	tail = length/ 2;

	target1 = rand() % width;
	target2 = rand() % length;
}
void Layout(){
	system ("cls");
	for (int i=0; i<width; i++)
	cout<<"$";
	cout<<endl;
	for (int i = 0; i < length ; i++)
	{

		for (int j = 0; j < width; j++)
		{
			if (j == 0)
				cout<<"$";
			if (i==head && j==tail)
				cout<<"#";
			else if (i==target1 && j==target2)
			cout<<"@";
				cout <<" ";

			if (j == width - 1)
				cout<<"$";
		}
		cout<<endl;
	}
	for (int i= 0; i < width; i++)
	cout<<"$";
	 	cout<<endl;



}
void Input(){
	if (_kbhit ()) {

switch (_getch ()) {

case 'w':

comm = l;

break;

case 's':

comm = r;

break;

case 'a':

comm = u;

break;

case 'd':

comm = down ;

break;

case 'x':

end = true;

break;

}

}

}
void Thinking(){
	
switch(comm){
case l:
	head--;
break;

case down:
	tail++;
break;

case u:
	tail--;
break;

case r:
	head++;
break;
default:
	break;

if (head > width || head < 0 || tail > length || tail < 0 ){
	end = true;}
if (head == target1 && tail == target2)
{
	target1 = rand() % width;
	target2 = rand() % length;
}
}
}
int main(){
Setup();
while (!end) {
Layout();
Input();
Thinking();
}
	return 0;
}
To make the starting position random, you could look into randomly generating a number to place a character within an array, This can be done using C-style random generation as you have used here or you can look into C++11's <random> header and generate a random number within a certain range.

Once you have generated that random number, you can insert a snake character (whatever you are using, it is not very clear from your code) at that position and mark that position as the head for your snake.

For wall detection, it is simple. Just check whether the next character in the array where the snake is supposed to move is a wall character. If it is, you can have a return value for your Layout function (I assume this function is generating every "frame" of your game) that specifies whether the player has hit a wall, and then you can use this return value to break out of the while loop in main() and terminate the program.

Also, for future reference, you should ask specific questions regarding parts of your code. This is helpful for you and for people reading your code. Nobody is going to have time to sift through hundreds of lines of unformatted and uncommented code to figure out what it does.
Line 4. Avoid using namespace std;. It brings all of the standard library names into scope, which increases the changes of a conflict.

Line 5. And BAM! Here 's a conflict! "end" is also part of the standard namespace.

Line 119. This closing brace is out of place. This is why it's so important to indent your code to reflect the actual block structure.
Topic archived. No new replies allowed.