Please help with program using arrays and random numbers

Here is my code. We have to create a snake game. However I am really struggling to get ONE asterick to appear randomly on my snake array. I do not need anything else right now just the one random appearing asterick. I have created an a1 and a2 int to make them = to random numbers. Then I could do a snake[a1][a2] ='*'; and then cout << snake[a1][a2]; however I've tried and my problems are usually that the asterick is in the same place every time and or it appears outside of the 2d array screen. Should I use a for loop? can someone please help. All I need is one asterick to appear at a random place on my 2d snake array screen.


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
  #include <iostream>
#include <conio.h>


using namespace std;


void Cursor(int, int);


int main() {

	char snake[100][100];

	char move;
	

	int x, y, Xmax, Ymax, S1, S2, a1, a2, a;




	//Imax = width
	Xmax = 50;
	//Jmax= height
	Ymax = 22;

	S1 = 10;
	S2 = 10;




	//Initializing the 2D Array
	for (y = Ymax - 1; y >= 0; y--) {
		for (x = 0; x < Xmax; x++) {
			snake[x][y] = '±';
		}
	}

	//Update the 2D Array
	for (y = Ymax - 2; y >= 1; y--) {
		for (x = 1; x < Xmax - 1; x++) {
			snake[x][y] = ' ';
		}
	}


	//Snake[S1][s2] = '±';


	//Printing the 2D Array
	for (y = Ymax - 1; y >= 0; y--) {
		for (x = 0; x < Xmax; x++) {
			cout << snake[x][y];
		}
		cout << endl;
	}


	cout << "\n\nWhere would you like to move (a, s, d, w)?";


	Cursor(S1, (Ymax - 1) - S2);
	cout << 's';
	Cursor(43, 24);


	
	do {

		move = _getch();
		

	
		
	if(move == 'a' && snake[S1 - 1][S2] == ' ') {
			Cursor(S1, (Ymax - 1) - S2);
			cout << ' ';
			S1--;
			Cursor(S1, (Ymax - 1) - S2);
			cout << 's';
		}


		else if (move == 's' && snake[S1][S2 - 1] == ' ') {
			Cursor(S1, (Ymax - 1) - S2);
			cout << ' ';
			S2--;
			Cursor(S1, (Ymax - 1) - S2);
			cout << 's';
		}

		else if (move == 'd' && snake[S1 + 1][S2] == ' ') {
			Cursor(S1, (Ymax - 1) - S2);
			cout << ' ';
			S1++;
			Cursor(S1, (Ymax - 1) - S2);
			cout << 's';
		}


		else if (move == 'w' && snake[S1][S2 + 1] == ' ') {
			Cursor(S1, (Ymax - 1) - S2);
			cout << ' ';
			S2++;
			Cursor(S1, (Ymax - 1) - S2);
			cout << 's';
		}


		Cursor(43, 24);






	} while (move == 'a' || move == 's' || move == 'd' || move == 'w');


	char exit;

	cout << "\n\nPlease press a key and <enter> to exit";
	cin >> exit;

	return 0;

}

#include <Windows.h>

void Cursor(int x, int y) {
	COORD Cord;
	Cord.X = x;
	Cord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Cord);

}
Last edited on
Here is a way to get two random numbers. It ought to get you started, or at least give you the building blocks you need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <time.h>

void GetRand(int &a1, int &a2, int xMax, int yMax)
{
    a1 = rand() % xMax;
    a2 = rand() % yMax;
}

int main()
{
  srand(time(0));
  int a1, a2;
  GetRand(a1,a2,50,50);
  std::cout << "a1: " << a1 << " a2: " << a2 << std::endl;
}
and if I then insert a1 and a2 into snake like snake[a1][a2] = '*'; and then I cout it, it will appear in the 2d array
Yes, but here are a few things to remember.
1. you must include <time.h>
2. you must call srand(time(0)) once in your main() function or the random numbers will always be the same.
3. in the function GetRand(), xMax and yMax will be your Xmax and Ymax variables. a1, for example will be a number between 0 and Xmax - 1. you may need to adjust that by 1 or 2 to fit into your board.
Here's an example:

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
#include <iostream>
#include <time.h>

#define XMAX 10
#define YMAX 10

void GetRand(int &a1, int &a2, int xMax, int yMax)
{
    a1 = rand() % xMax;
    a2 = rand() % yMax;
}

void PrintBoard(const char board[YMAX][XMAX], int xMax, int yMax)
{
    for(int i = 0; i < yMax; i++)
    {
        for(int j = 0; j < xMax; j++)
        {
            std::cout << board[i][j];
        }
        std::cout << std::endl;
    }
}

int main()
{
  srand(time(0));
  int a1, a2;
  char board[YMAX][XMAX];
  // fill the board with
  for(int i = 0; i < YMAX; i++)
    {
        for(int j = 0; j < XMAX; j++)
        {
            board[i][j] = '-';
        }
    }
  // get and place the '*'
  GetRand(a1,a2,XMAX,YMAX);
  board[a1][a2] = '*';
  //print the board
  PrintBoard(board, XMAX, YMAX);
  std::getchar();	//remove this, it just keeps visual studio from closing the console
}
Last edited on
Topic archived. No new replies allowed.