Original Post
Can you help me debug this code, it has a problem that if it writes to the last cell in the array, it writes also to the first cell in the next line:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int const MAP_SIZE = 8;
int map[MAP_SIZE][MAP_SIZE];
void reset_map();
void generate_bombs();
void print_map();
void main()
{
srand((unsigned)time( NULL ));
reset_map();
print_map();
generate_bombs();
print_map();
}
void reset_map()
{
for (int i=0; i <= MAP_SIZE; i++)
{
for (int j=0; j <= MAP_SIZE; j++)
{
map[j] = 0;
}
}
}
void generate_bombs()
{
int x, y;
for (int i=0; i <= 9; i++)
{
x = rand()%9; //choose the bomb''s x and y
y = rand()%9;
if (x > 8)
cout << "\nx>8\n";
if (y > 8)
cout << "\ny>8\n";
cout << "\ni: "<< i << endl;
if (map[x][y] == 1)
{
i--;
cout << "equals to 1, x: "<< x << endl << "y: " << y << endl;
print_map();
}
else
{
map[x][y] = 1;
cout << "x: "<< x << endl << "y: " << y << endl;
print_map();
}
}
}
void print_map()
{
for (int i=0; i <= MAP_SIZE; i++)
{
for (int j=0; j <= MAP_SIZE; j++)
{
cout << map[j][i];
}
cout << endl;
}
}