I could not edit the first post (dont know why). Here is the updated code:
Code:
#include <vector>
#include <cstdlib>
class MineField
{
private:
std::vector<std::vector<char> > field;
int width;
int height;
int numMines;
public:
MineField() { width = 0; height = 0; }
~MineField() {}
void GenerateField(int w, int h, int m);
int GetWidth() { return width; }
int GetHeight() { return height; }
int GetNumMines() { return numMines; }
void SetPosition(int x, int y)
{
field[x][y] = -2;
}
char GetPosition(int x, int y)
{
return field[x][y];
}
};
void MineField::GenerateField(int w, int h, int m)
{
width = w;
height = h;
numMines = m;
int x;
int y;
int i;
field.clear();
field.resize(width);
for(i=0; i<width; i++)
field[i].resize(height, 0);
for(i=0; i<numMines; i++)
{
x = std::rand()%width;
y = std::rand()%height;
if(field[x][y] == -1)
{
i--;
continue;
}
field[x][y] = -1;
}
for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
if(field[x][y] == -1)
continue;
int left = x-1;
int right = x+1;
int up = y-1;
int down = y+1;
if(left < 0)
left = 0;
if(right >= width)
right = width-1;
if(up < 0)
up = 0;
if(down >= height)
down = height-1;
if(field[left][down] == -1 && down != y)
field[x][y]++;
if(field[left][y] == -1)
field[x][y]++;
if(field[left][up] == -1 && up != y)
field[x][y]++;
if(field[x][down] == -1 && left != x && right != x && down != y)
field[x][y]++;
if(field[x][up] == -1 && left != x && right != x && up != y)
field[x][y]++;
if(field[right][down] == -1 && down != y)
field[x][y]++;
if(field[right][y] == -1)
field[x][y]++;
if(field[right][up] == -1 && up != y)
field[x][y]++;
}
}
}