2d grid using array c++

Pages: 12
In your boolean conditions (note that I, embarassingly, screwed up can_move_left earlier on in this posting sequence - sorry!):

line 43 should be (noting the right-hand boundary):
if ( j < N-1 && grid[i][j+1]= '.') // 0 should be N-1

Similarly, line 51 should be (noting the bottom boundary - high i in this case)
if ( i < M-1 && grid[i+1][j] == '.') // 0 should be M-1
The order of your i and j is definitely confusing me!

currentChar should be initialised in main to 'A'. Since i and j have both been set to 0, you can then set
grid[i][j]=currentChar;
once, immediately after this.


Where you presently have line 27, the call to set_current_position you need a loop. It is easy enough to do this in main. e.g. in pseudocode:

while ( condition to keep on looping )
{
    generate a random number - say direction = 0,1,2 or 3
    switch( direction )
    { 
     case 0:   - e.g. if this is move left
          if  ( can_move_left(...)  )
          { update j, grid and currentChar }
          else
          { do nothing - i.e. you don't really need the else block }
         break;
    case 1:
          similar for different direction
    case 2:
          similar for different direction
    case 3:
           similar for different direction
}

Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
// #include <vector> // <--- ???
#include <ctime>
// #include <cstdlib> // <--- NOT NEEDED

using namespace std;

const int M = 10;
const int N = 6;

void initialize_grid(char grid[][N]);
void set_current_position(int i, int j, char grid[][N], char CurrentChar);
void print_grid(char grid[][N]);

bool can_go_left(int i, int j);
bool can_go_right(int i, int j);
bool can_go_up(int i, int j);

bool can_go_down(int i, int j, char grid[][N]); // REPAIRED

int main()
{
    srand(time(NULL));
    char grid[M][N];
    
    int i = 0;
    int j = 0;
    
    char CurrentChar = 'A'; // <---
    
    bool possible = false;
    
    initialize_grid(grid);
    set_current_position(i, j, grid, CurrentChar);
    
    for( int k = 0; k < 12; k++) // MAKE A LOOP FOR 10 DOWN MOVES
    {// NOW TRY A DOWN MOVE
        possible = can_go_down(i, j, grid);
        if(possible == true)
        {
            cout << "Can move down\n";
            i++;
            CurrentChar++;
            set_current_position(i, j, grid, CurrentChar);
        }
        else
            cout << "Can't move down\n";
    }
    
    return 0;
    
}


bool can_go_left(int i, int j, char grid[M][N])
{
    if ( j > 0 && grid[j-1][i] == '.')
        return true;
    else
        return false;
}

bool can_go_right(int i, int j, char grid[M][N])
{
    if ( j < 0 && grid[j+1][i] == '.')
        return true;
    else
        return false;
}

// REPAIRED
bool can_go_down(int i, int j, char grid[M][N])
{
    if ( i < M && grid[i + 1][j] == '.')
        return true;
    else
        return false;
}

bool can_go_up(int i, int j, char grid[M][N])
{
    if ( i > 0 && grid[j][i-1] == '.')
        return true;
    else
        return false;
}

void set_current_position(int i, int j, char grid[M][N], char CurrentChar)
{
    grid[i][j] = CurrentChar;
    print_grid(grid); // <---
}

void initialize_grid(char grid[M][N])
{
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
            grid[i][j] = '.';
        }
    }
    print_grid(grid); // <---
}

void print_grid(char grid[M][N])
{
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
            cout << grid[i][j];
        }
        cout << endl;
    }
    cout << endl;
}
okay so now the letters are not going in order && it's only showing a few before getting stuck
and also the symbol { is in the grid as well

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const int M = 10;
const int N = 6;

void initialize_grid(char grid[][N]);
void set_current_position(int i, int j, char grid[][N], char CurrentChar);
void print_grid(char grid[][N]);

bool can_go_left(int i, int j,char grid[][N]);
bool can_go_right(int i, int j, char grid[][N]);
bool can_go_up(int i, int j, char grid [][N]);

bool can_go_down(int i, int j, char grid[][N]); // REPAIRED

int main()
{
    srand(time(NULL));
    char grid[M][N];

    int i = 0;
    int j = 0;

    char CurrentChar = 'A'; // <---

    initialize_grid(grid);
    set_current_position(i, j, grid, CurrentChar);

     while(CurrentChar <= 'Z' && i < M
          && j < N)
    { int direction = rand() % 4;
        switch (direction)
    {
        case 0:
            if(can_go_down(i, j, grid) == true)
        {
            set_current_position(++i, j, grid, ++CurrentChar);
        }
        case 1:
            if (can_go_up(i, j, grid) == true)
        {
            set_current_position(--i, j, grid, ++CurrentChar);
        }
        case 2:
            if(can_go_left(i, j, grid) == true)
        {
            set_current_position(i, --j, grid, ++CurrentChar);
        }
        case 3:
            if(can_go_right(i, j, grid) == true)
        {
            set_current_position(i, ++j, grid, ++CurrentChar);
        }

    }
    }
print_grid(grid);
    return 0;

}


bool can_go_left(int i, int j, char grid[M][N])
{
    if ( j > 0 && grid[j-1][i] == '.')
        return true;
    else
        return false;
}

bool can_go_right(int i, int j, char grid[M][N])
{
    if ( j < N-1 && grid[j+1][i] == '.')
        return true;
    else
        return false;
}

bool can_go_down(int i, int j, char grid[M][N])
{
    if ( i < M-1 && grid[i + 1][j] == '.')
        return true;
    else
        return false;
}

bool can_go_up(int i, int j, char grid[M][N])
{
    if ( i > 0 && grid[j][i-1] == '.')
        return true;
    else
        return false;
}

void set_current_position(int i, int j, char grid[M][N], char CurrentChar)
{
    grid[i][j] = CurrentChar;
}

void initialize_grid(char grid[M][N])
{
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
            grid[i][j] = '.';
        }
    }
}

void print_grid(char grid[M][N])
{
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
            cout << grid[i][j];
        }
        cout << endl;
    }
}
closed account (48T7M4Gy)
You'll have to debug it very carefully and here are a couple of tips that help because I can get it to make sense on my computer.

1. Include the print_grid function in the initialize and set_current_position functions as I did above.

2. CAREFULLY look at, test and repair each of the can_go functions. You have indexes around the wrong way in at least 2 cases. [i ] always goes before [j] in the brackets [j etc][k etc] is a major reason why it doesn't work.

3. while(CurrentChar < 'Z') is all you need at this stage.

4. modify the print_grid function so a blank separator line is printed after the matrix is displayed. It's one additional line.

5. Once you get that done you need to run it numerous times and examine the output you get and debug/modify until it makes sense. Believe me, it will come together with a bit of hard work for an hour or so.

6. Remember that the program as it stands will keep running if it hasn't got to Z and there is no true move. Fix that later. It's a lower priority.

Let us know how you get on :)
Last edited on
I got it to work now. However sometimes when I run it, a black screen appears.
is that also a debug issue?
closed account (48T7M4Gy)
Maybe/Probably the current position is stuck.

Just include a debug cout linein the loop and see if it is still running.
It sometimes runs and sometimes doesnt.
it sometimes prints the statement & the grid but sometimes only the statement
closed account (48T7M4Gy)
shift the statement around inside the loop so it always prints and put another line in before the final return in main to say the program is ended.
So i put every statement like cout<< "can go right " << endl;
to every direction. However when i click run it just repeats those statements

& it does not print out the program is over statement before the last return 0;
closed account (48T7M4Gy)
That's because it's still running because there is no available empty space to move to. It's stuck!

To see what I'm talking about limit the number of moves to just 5 or 6 and see if the program ends.
I see it gets stuck after M
However, I don't know how to say if the program is stuck end the program and output grid

1
2
3
4
5
6
7
case 4:
            if(can_go_right(i, j, grid) == false && can_go_left(i, j, grid) == false && can_go_up(i, j, grid) == false && can_go_down(i, j, grid) == false)
            {
                set_current_position(i, j, grid, CurrentChar);
                print_grid(grid);
                return 0;
            }


does it have to be outside the switch or the loop?

Nevermind, I got it. It had to be outside the loop.
Thanks guys for all your help!
Ah, well done @xx123 - you persevered for a long time.

And thanks for all the help, @kemort - I seemingly missed all the final flurries due to timezone differences (unless there are some serious night owls out there!)

Just for the record, here's my test program: written at the start - and then corrected half way through when I realised I couldn't tell my "left" from my "up".
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const int M = 10;
const int N = 6;
const char DOT = '.';
char grid[M][N];


bool can_go_left ( int i, int j ) { return ( j > 0   && grid[i][j-1] == DOT ); }
bool can_go_right( int i, int j ) { return ( j < N-1 && grid[i][j+1] == DOT ); }
bool can_go_up   ( int i, int j ) { return ( i > 0   && grid[i-1][j] == DOT ); }
bool can_go_down ( int i, int j ) { return ( i < M-1 && grid[i+1][j] == DOT ); }


void initialize_grid()
{
   for ( int i = 0; i < M; i++ )
   { 
      for ( int j = 0; j < N; j++ ) grid[i][j] = DOT;
   }
}


void print_grid()
{
   for ( int i = 0; i < M; i++ )
   { 
      for ( int j = 0; j < N; j++ ) cout << grid[i][j];
      cout << endl;
   }
}


int main()
{
   char CURRENT;
   int i, j;
   int direction;
   bool moved;

   initialize_grid();

   i = 0;   j = 0;
   CURRENT = 'A';
   grid[i][j] = CURRENT;

   srand( time( 0 ) );
   while( ( can_go_left( i, j ) || can_go_right( i, j ) || can_go_up( i, j ) || can_go_down( i, j ) ) && CURRENT < 'Z' )
   {
      direction = rand() % 4;
      moved = false;
      switch( direction )
      {
      case 0:
         if ( can_go_right( i, j ) )
         {
            j++;
            moved = true;
         }
         break;
      case 1:
         if ( can_go_up( i, j ) )
         {
            i--;
            moved = true;
         }
         break;
      case 2:
         if ( can_go_left( i, j ) )
         {
            j--;
            moved = true;
         }
         break;
      case 3:
         if ( can_go_down( i, j ) )
         {
            i++;
            moved = true;
         }
         break;
      }
      if ( moved ) grid[i][j] = ++CURRENT;
   }

   print_grid();
}
Topic archived. No new replies allowed.
Pages: 12