Random function inside of a character array

I'm working on a very basic snake game, and am in need of some help. In my loop I am and trying to repeatedly display my "pellet" in the Char Map array(can post full code if needed) the Map[y][x] are what I used in my get key sync to be able to use the arrow keys. That's why I tried them again. Originally I had Map[0][0] and that did not work, as well as Map[][]. What I want to achieve is that if the asterisk and the S are exactly equal IE in the same place then cout another randomly on the board. The error that I receive is left operend must be l-value, and also I am getting a red line under the asterisk(I'm using Visual Studios to compile)




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
 #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=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;
      }
}

	 
	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';
			}
	     }

	}
	
	
}   
Last edited on
I'm not entirely sure what you are trying to do on line 30 but here is a possible solution:

Check if Map[y][x] is equal to '*' or 'S'

if (Map[y][x] == '*' || Map[y][x] == 'S')
I'm trying to use a sort of collision detection if the S which is my character comes in contact with the asterisk which is my snake pellet then move the pellet to some random place inside the character array sort of like he's eaten it
it gives me a red line under the asterisk when I do it like that
I see.

How bout doing somthing like this after lines 58, 68, 78, and 88:

1
2
3
else if(Map[y][x2] == '*'){
 //collision!!!
}
Topic archived. No new replies allowed.