Printing One Asterick in a 2D array


Why isn't my code working the way I want it to?

I am having a problem with my code. I have to create a snake game and just right now(at the moment) I only need help printing ONE asterick'*' onto my 2d array titled snake where there is a border and display. Anyways, you don't understand how many times I have tried to accomplish this. I keep getting it wrong where the asterick is outside of the 2d array or it is in the same place every time I run the program so can someone please please help me to produce ONE (*)asterick on my 2d array that has my border already like [_] this. I'm thinking I need to create two integers and make them have a random number stored where I then would go like snake[int][int] = '*'; and then I would cout it. However like I said I'm having trouble and I've been on a lot of websites trying to get help. One last point. I was wondering if I need a for loop and if char a1 and a2 need to be integers, not characters. literally all I need is one asterick at a random place on the 2d array.

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


using namespace std;


void Cursor(int, int);


int main() {

srand(time(0));

char snake[100][100];
char a1, a2;

char move;


int x, y, Xmax, Ymax, S1, S2, 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] = ' ';
}
}



for (a = 1; a < 1; a++) {
a1 = rand() % Xmax;
a2 = rand() & Ymax;
snake[a1][a2] = '*';


}

cout << snake[a1][a2];


//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 are some things.

1. a1 and a2 need to be int, not char.

2. Look at your for loop on line 55. You set a equal to one, then test if a is less than one. So, the code inside the loop never runs. You don't need that loop anyway, just delete lines 55 and 61.

3. You have a typo on line 57, the & should be a %

That should fix most of the problems you are having. However, I will say again, lines 56 and 57 will give you a random number between 0 and Ymax/Xmax. Since you are drawing your border around the outside within your array, you'll have to make an adjustment or occasionally, your * will end up on the border.

4. On line 18 you do this: char snake[100][100]; Why? when Xmax/Ymax are 50 and 22. You aren't filling the array thus wasting memory. I recommend you declare Xmax and Ymax as const int the do this: char snake[XMax][YMax];

Hope this helps.
thank you so much. This helped a lot!
You're welcome, but in the future, don't double post here and in the general forum.
I am fairly new to this website and I do not know if it will email you my reply but I hope it does. I have done everything you asked and now my only problem is printing the snake[a1][a2]. To clarify, I don't know where to place cout << snake[a1][a2] in my code. If I put it in the do while loop or before my for loop that prints it, it won't work. Can you explain to me where I place this and if I need to add something because I can't print the asterick because I don't know where it should be on my code
You shouldn't have to add in a separate line for cout << snake[a1][a2] It should print to the screen whey you print your whole snake array on lines 70-75 in the code above. If it's not working, post your current code.
I'm sorry, I worked on it today and it works now. I must have done something wrong before I replied. Thank you for all your help
Topic archived. No new replies allowed.