Help concerning a basic simple snake game

So, I was trying to create a simple snake game the other day.
The code has been written, but a few problems popped out and I don't quite understand why.

1. when the snake eats at least one fruit, the 'y' for replay option doesn't work (SOLVED)

2. the snake occasionally goes into wall even though it is supposed to be on the edge

3.The limitations of the grid don't seem to correspond to the (x,y) coordinates that I assigned. Meaning that in order to make the snake "die" when touching the grid, I couldn't when if( x == 0 || x == width -1 || y == 0 || y == height -1) The conditions that I have currently in the codes doesn't work.

4. after precisely 33 fruits, there is no more generation of fruit in the board

Also, if I wanted to make the game bigger (like, to make every character appear bigger), how should I do that?

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <conio.h>
#include <string>
#include <Windows.h>
using namespace std;

char head;
char tail = 'o';
bool GameOver, pause;
bool paint = false;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int ntail;
enum Direction {Stop=0, Left, Right, Up, Down};
Direction dir;


void SetUp() {
	GameOver = false;
	dir = Stop;
	x = width / 2;
	y = height / 2;
	fruitX = rand() % width;
	fruitY = rand() % height;
	score = 0;
	head = '@';
	pause = false;
}

void Draw() {
	system("cls");
	for (int i = 0; i < width; i++)
	{
		cout << '#';
	}

	cout << "W(5), A(1), S(2), and D(3) to move, X to end and P to pause";
	cout << endl;
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			if ((j == 0) || j == (width - 1))
			{
				cout << '#';
			}
			else if ((j == x) && (i == y))
			{
				cout << head;
			}
			else if ((j == fruitX) && (i == fruitY))
			{
				cout << 'F';
			}
			else
			{
				for (int k = 0; k < ntail; k++)
				{
					if (tailX[k] == j && tailY[k] == i)
					{
						cout << tail;
						paint = true;
					}
				}
				if (!paint){
					cout << ' ';
				}
				paint = false;
			}
		}
		cout << endl;
	}
	for (int i = 0; i < width; i++)
	{
		cout << '#';
	}
	cout << endl << "Score: " << score;
	if (pause)
	{
		cout << "        PAUSED";
	}
}

void Input() {
	if (_kbhit())
	{
		switch (cin.get())
		{
		case 'w': 
			dir = Up;
			break;
		case 'a':
			dir = Left;
			break;
		case 's':
			dir = Down;
			break;
		case 'd':
			dir = Right;
			break;
		case '5':
			dir = Up;
			break;
		case '1':
			dir = Left;
			break;
		case '2':
			dir = Down;
			break;
		case '3':
			dir = Right;
			break;
		case 'p':
			if (!pause)
			{
				pause = true;
			}
			else
			{
				pause = false;
			}
			break;
		case 'x':
			GameOver = true;
			break;
		}
	}
}

void Logic() {

	int prevX = tailX[0];
	int prevY = tailY[0];
	int prev2X, prev2Y;
	tailX[0] = x;
	tailY[0] = y;

	for (int i = 1; i < ntail; i++)
	{
		prev2X = tailX[i];
		prev2Y = tailY[i];
		tailX[i] = prevX;
		tailY[i] = prevY;
		prevX = prev2X;
		prevY = prev2Y;
	}
	
	switch (dir)
	{
	case Left:
		x--;
		head = '<';
		break;
	case Right:
		x++;
		head = '>';
		break;
	case Up:
		y--;
		head = '^';
		break;
	case Down:
		y++;
		head = 'v';
		break;
	default:
		break;
	}
	if (x > width || x < 0 || y > height || y < 0)
	{
		GameOver = true;
	}
	if (x == fruitX && y == fruitY){
		fruitX = rand() % width;
		fruitY = rand() % height;
		score += 10;
		ntail++;
	}
	for (int k = 0; k < ntail; k++)
	{
		if (tailX[k] == x && tailY[k] == y)
		{
			GameOver = true;
		}
	}
}

int main(){
	char YESorNo;
	
	do
	{
		SetUp();
		while (!GameOver)
		{
			Sleep(200);
			Draw();
			Input();
			if (!pause){
				Logic();
			}
		}

		system("cls");
		cout << "GAME OVER" << endl;
		cout << "Score: " << score << endl;
		cout << "Do you want to replay? (Respond with y or n)" << endl;
		cin.get(YESorNo);
		cin.get();
		if (YESorNo == 'y' || YESorNo == 'Y')
		{
			GameOver = false;
		}
		if (YESorNo == 'n' || YESorNo == 'N')
		{
			GameOver = true;
		}
	} while (!GameOver);
	
	return 0;
}
Last edited on
1. when the snake eats at least one fruit, the 'y' for replay option doesn't work

You need to reset ntail to 0 in SetUp().

1
2
3
4
5
switch (cin.get()) {
  case 'w':
    dir = Up;
    break;
...

Why don't you use _getch() instead of cin.get().
That way you don't need to press enter every time.
Thanks for that answer! Also it was _getch() before. I was just trying to understand the difference between the two commands so I swapped it in. Guess I forgot to switch it back.
Topic archived. No new replies allowed.