rand() function

I'm coding for a basic snake game I've commented the line of error. What I want to do is if the 'S' and the '*' are in the same place, move the '*' to a different random location inside the Map array.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
	srand(time(NULL));
	int pellet=rand()%21;
	
	
	  for (int pellet = rand(); pellet % Map[0][0]; pellet++)
	{
		  if (Map[y][x] = '*' == Map[y][x] = 'S')//line of error
		  {
			  pellet = rand() % 21;
			  cout << pellet;
		  }
	}
 
What exactly are you trying to do on that line? What are the values of y and x?
<on second thought, the code is more broken than I thought/hoped... so I've removed my naive guess/suggestion.>
Last edited on
this is my whole code is basically a very simple snake game the code above is to randomly place a * inside the array when the S touches it





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
 #include<iostream>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
using namespace std;

char Map[11][22] =
{
	"---------------------",
	"|S                  |",
	"|                   |",
	"|                   |",
	"|       *           |",
	"|                   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"---------------------"
};
int x = 1, y = 1;
bool GameRunning = true;
int main()
{
	srand(time(NULL));
	int pellet = 0;
	int pellet=rand()%21;
	
	
	  for (int pellet = rand(); pellet % Map[0][0]; pellet++)
	{
		  if (Map[y][x] = '*' == Map[y][x] = 'S')
		  {
			  pellet = rand() % 21;
			  cout << pellet;
		  }
	}

	 
	while (GameRunning == true)
	{
		
		system("cls");
		for (int display = 0; display < 11; display++)
		{
			cout << Map[display] << endl;
		}
		system("pause>nul");

	

		if (GetAsyncKeyState(VK_DOWN))
		{
			int y2 = y + 1;
			if (Map[y2][x] == ' ')
			{
				Map[y][x] = ' ';
				y++;
				Map[y][x] = 'S';
			}
		}
		if (GetAsyncKeyState(VK_UP))
		{
			int y2 = y - 1;
			if (Map[y2][x] == ' ')
			{
				Map[y][x] = ' ';
				y--;
				Map[y][x] = 'S';
			}
		}
		if (GetAsyncKeyState(VK_RIGHT))
		{
			int x2 = x+1;
			if (Map[y][x2] == ' ')
			{
				Map[y][x] = ' ';
				x++;
				Map[y][x] = 'S';
			}
		}
		if (GetAsyncKeyState(VK_LEFT))
		{
			int x2 = x - 1;
			if (Map[y][x2] == ' ')
			{
				Map[y][x] = ' ';
				x--;
				Map[y][x] = 'S';
			}
	     }

	}
	
	
}  
Topic archived. No new replies allowed.