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.

code [

#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);

}


code ]

Last edited on
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
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>

const int NROWS = 5, NCOLS = 10 ;
using snake_type = char[NROWS][NCOLS] ;

void print( const snake_type& snake )
{
    // move to COORD {0,0} (top left)
    ::SetConsoleCursorPosition( ::GetStdHandle(STD_OUTPUT_HANDLE), {0,0} );
    std::cout << '\n' ;

    for( const auto& row : snake )
    {
        for( char c : row ) std::cout << c << ' ' ;
        std::cout << '\n' ;
    }
}

int main()
{
    std::srand( std::time(nullptr) ) ;

    const char ASTERISK = '*' ;
    const char DOT = '.' ;

    snake_type snake ;
    int curr_row = std::rand() % NROWS ;
    int curr_col = std::rand() % NCOLS ; ;

    for( auto& row : snake ) for( char& c : row ) c = DOT ;
    snake[curr_row][curr_col] = ASTERISK ;
    print(snake) ;

    // move north
    if( curr_row > 0 )
    {
        std::cout << "\npress enter to move north: " ;
        std::cin.get() ;

        // snake[curr_row][curr_col] = DOT ;
        --curr_row ;
        snake[curr_row][curr_col] = ASTERISK ;
        print(snake) ;
    }

    // move east
    if( curr_col < (NCOLS-1)  )
    {
        std::cout << "\npress enter to move east: " ;
        std::cin.get() ;

        // snake[curr_row][curr_col] = DOT ;
        ++curr_col ;
        snake[curr_row][curr_col] = ASTERISK ;
        print(snake) ;
    }

    // etc.
}

http://rextester.com/DMGFQ79554
Topic archived. No new replies allowed.